技术标签: springcloud 架构 java 开发框架 springboot
要会用,首先要了解。图懒得画,借鉴网上大牛的图吧,springcloud组建架构如图:
微服务架构的应用场景:
1、系统拆分,多个子系统
2、每个子系统可部署多个应用,应用之间负载均衡实现
3、需要一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。
4、所有的客户端都通过同一个网关地址访问后台的服务,通过路由配置,网关来判断一个URL请求由哪个服务处理。请求转发到服务上的时候也使用负载均衡。
5、服务之间有时候也需要相互访问。例如有一个用户模块,其他服务在处理一些业务的时候,要获取用户服务的用户数据。
6、需要一个断路器,及时处理服务调用时的超时和错误,防止由于其中一个服务的问题而导致整体系统的瘫痪。
7、还需要一个监控功能,监控每个服务调用花费的时间等。
项目架构:
一、discovery服务注册发现
①、pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gt</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>discovery</artifactId>
<name>discovery</name>
<url>http://www.popumusic</url>
<properties>
<start-class>com.wisely.discovery.DiscoveryApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.wisely.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //1
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
server:
port: 8761
endpoints:
shutdown:
enabled: true
sensitive: false
eureka:
instance:
prefer-ip-address: true #启用IP方式
ip-address: 127.0.0.1
client:
register-with-eureka: false #指向其他注册中心地址
fetch-registry: false
service-url:
defualtZone: http://127.0.0.1:8762/eureka/
①、pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gt</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.poputar</groupId>
<artifactId>popumonitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>popumonitor</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.poputar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
* 监控服务
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class PopumonitorApplication
{
public static void main( String[] args )
{
SpringApplication.run(PopumonitorApplication.class, args);
}
}
server:
port: 8989
spring:
application:
name: monitor
eureka:
instance:
nonSecurePort: ${server.port:8989}
client:
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
①、pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gt</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.poputar</groupId>
<artifactId>popuconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>popuconfig</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
②、PopuconfigApplication
package org.popuconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 配置服务
*
*/
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class PopuconfigApplication
{
public static void main( String[] args )
{
SpringApplication.run(PopuconfigApplication.class, args);
}
}
spring:
cloud:
config:
server:
native:
search-locations: classpath:/config
server:
port: 8762
spring:
application:
name: config #1
profiles:
active: native #2
eureka:
instance:
non-secure-port: ${server.port:8762} #3
metadata-map:
instanceId: ${spring.application.name}
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/ #5
四、用户服务
①、pom.xml 需要引入外部jar包时,我已做注释,如下:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gt</groupId>
<artifactId>popuserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>popuman</artifactId>
<name>popuman</name>
<url>http://www.popumusic</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>aliyun-java-sdk-dysmsapit</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aliyun-java-sdk-dysmsapi-1.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>aliyun-java-sdk-core</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aliyun-java-sdk-core-3.3.1.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 将外部包打入jar docker部署时需要打开,否则报错,找不到jar -->
<!-- <resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources> -->
</build>
</project>
package com.gt;
import javax.validation.Validator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@SpringBootApplication
@EnableEurekaClient
public class PopumanApplication {
public static void main(String[] args) {
SpringApplication.run(PopumanApplication.class, args);
}
public ResourceBundleMessageSource getMessageSource() throws Exception {
ResourceBundleMessageSource rbms = new ResourceBundleMessageSource();
rbms.setDefaultEncoding("UTF-8");
rbms.setBasenames("i18n/ValidationMessages");
return rbms;
}
@Bean
public Validator getValidator() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(getMessageSource());
return validator;
}
}
package com.gt;
import java.util.Locale;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
④、MessageManager 读取国际化文件内容
package com.gt;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
@Component
public class MessageManager {
private static MessageSource messageSource;
public static String getMsg(String key) {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(key, null, locale);
}
public static String getMsg(String key, String... arg) {
Locale locale = LocaleContextHolder.getLocale();
Object[] args = new Object[arg.length];
for (int i = 0; i < arg.length; i++) {
args[i] = arg[i];
}
return messageSource.getMessage(key, args, locale);
}
@Autowired(required = true)
public void setMessageSource(MessageSource messageSource) {
MessageManager.messageSource = messageSource;
}
}
⑤、application.yml
debug: true
server:
port: 8781
spring:
application:
name: popuman
cloud:
config:
enabled: true
discovery: #配置服务发现,获取配置信息 配置文件命名要按照springcloud config配置文件命名规则命名
enabled: true
service-id: config
eureka:
instance:
appname: popuman
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
#docker环境下需指定ip才能访问
#eureka:
# instance:
# appname: popuman
# prefer-ip-address: true #启用IP方式
# ip-address: 192.168.*.**
# client:
# service-url:
# defaultZone: http://192.168.*.**:8761/eureka/
<configuration>
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %p (%file:%line\)- %m%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="popuman"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>/Users/david/Documents/Poputar/logs/popuman.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/david/Documents/Poputar/logs/popuman.%d.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 64 MB -->
<maxFileSize>64 MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>
%d %p (%file:%line\)- %m%n
</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤错误日志 -->
<level>ERROR</level>
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
</appender>
<appender name="popuman_err"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>/Users/david/Documents/Poputar/logs/popuman_err.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/david/Documents/Poputar/logs/popuman_err.%d.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 64 MB -->
<maxFileSize>64 MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>
%d %p (%file:%line\)- %m%n
</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<!-- 输出日志 -->
<logger name="com.gt" level="DEBUG">
<appender-ref ref="popuman" />
<appender-ref ref="popuman_err" />
</logger>
</configuration>
部署到docker时,切记端口映射好,否则调不通。
六、应用之间服务的调用是通过springcloud的FeignClient调用,这种调用方式同样也是基于http协议,好处是不用我们再去封装httpclient手写post,get请求,通过调用方法的方式就可以调用其他服务接口
本架构采用springboot推荐的JPA方式来处理数据层,缓存采用redis,如果您要问,redis挂掉怎么办?那就要考虑redis的分布式,主从等,这里不做赘述。
spriingcloud是近两年新兴的微服务技术,目前我也是在学习中,如有觉得我写的有不对的地方,还请批评指正,共同交流,学习一门新技术是枯燥的,难免走很多弯路,但是当你突破难关时,那样的轻松是何等畅快!在这里也感谢CSDN上大牛的技术文章分享,有分享才会有进步。希望本文对学习springcloud的同学有所帮助。
推荐几篇springcloud总结比较全的博客,也是本文项目搭建过程借鉴的技术文章
方志鹏大牛博客地址:http://blog.csdn.net/forezp/article/category/6830968/1
司青博客:http://blog.csdn.net/neosmith/article/details/52449921
http://blog.csdn.net/f1576813783/article/details/76805195
文章浏览阅读1.9k次。https://www.luxand.com/https://www.zhihu.com/question/19561362_luxand 获取faceid
文章浏览阅读2.3k次。1、挂在光驱,原始的centos镜像2、按F11或者其他提示的按键进入修复模式3、使用以下的命令进行修复;_centos系统linux 重启后lvm未激活
文章浏览阅读2.6k次。Qt5利用QProcess执行cmd命令把文件从一个目录移动到另一个目录,即执行cmd里的copy命令:copy 原文件 目标位置代码需要在头文件中加入 #include<QProcess>void MainWindow::on_ptn_clicked(){ QProcess p(0); //这个会报错 p.start("copy C:\\Users\..._qt qprocess使用cmd切换目录
文章浏览阅读573次。转载公众号 | 知识管理就在夏博自从ChatGPT成功推出以来,像大型语言模型LLM这样的人工智能系统引起了全球的关注,尽管LLM存在的时间要长得多。这些系统现在支持从聊天机器人、内容生成到头脑风暴和脚本代码的很多场景。然而,随着这些模型变得越来越复杂,它们产生错误的可能性也越来越大。最近,像ChatGPT这样的大型语言模型产生了不准确的报告,争论了不正确的事实,并在他们的答案中描述了现实世界的偏..._知识图谱chatgpy幻觉
文章浏览阅读549次,点赞17次,收藏11次。分支-条件覆盖可以使程序中的判断语句以及判断语句中的条件的真、假分支都得到覆盖,但是分支-条件覆盖达到 100% 仍然强度不够,程序中的某些逻辑运算等错误仍然可能不会被发现。_白盒测试测试中的分支测试
文章浏览阅读172次。PHP版本也是一样的 都是7.4。就很迷惑 一模一样的代码 怎么上线就会报错。但是又不能不用 因为两个表有相同的字段 去掉别名更是报错。奇怪的是 线下环境大写小写都可以用 不报错。最终解决手段:别名首字母改小写就好了。线上环境只能小写 大写就报错了。代码上线之后莫名报错。_fastadmin关联搜索 出错
文章浏览阅读268次。苹果手机自带的app中虽然没有便签,但是可以下载安装使用敬业签。因为这是一款支持备忘内容云端同步并提醒的跨平台(Windows电脑﹑安卓手机﹑苹果iPhone手机﹑iPad﹑苹果电脑Mac端以及网页Web端)桌面便签软件工具,非常实用!另外,软件还很人性化:支持用户创建分类记录备忘内容,并且还支持用户对便签分类设置密码进行锁定,以便保护自己的隐私!那么,怎么对便签分类设置密码锁定呢?下面..._苹果 nfc 改了密码
文章浏览阅读543次。一、业务场景前端本地开发的场景中,我们需要频繁的改动代码,并需要实时看到效果,并且在一些开发场景中,我们需要将特定的请求代理到特定的IP、本地文件等,所以使用fiddler或whistle等本地、真机抓包调试工具是非常必要的。二、为什么使用whistle在历史的长河中,我们是使用fiddler+willow再搭配小米wifi 进行本地和真机抓包调试的,无可厚非,..._whistlejs与fillder
文章浏览阅读1k次,点赞22次,收藏20次。原文:1 Building Abstractions with Functions译者:飞龙协议:CC BY-NC-SA 4.0心灵的行为,其中它对简单的想法施加其力量,主要有以下三种:1.将几个简单的想法组合成一个复合的想法,从而形成所有复杂的想法。2.第二个是将两个想法,无论是简单的还是复杂的,放在一起,并将它们放在一起,以便一次看到它们,而不将它们合并成一个,从而获得它们所有的关系想法。3.第三个是将它们与实际存在的所有其他想法分开:这被称为抽象,从而形成所有的一般想法。——约翰·洛克,
文章浏览阅读62次。Sql2005 全文索引详解1.前言14.1 全文索引的介绍14.2 全文索引中常用的术语14.3 全文索引的体系结构14.4 全文目录管理14.4.1 创建全文目录14.4.2 查看与修改全文目录14.4.3 删除全文目录14.5 全文索引管理14.5.1 创建全文索引的注意事项14.5.2 创建全文索引14.5.3..._sqlsever全文索引断字符中文选择那个
文章浏览阅读7.3k次。背景:在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录时,就更新(Update),不存在数据时,就插入(Insert)。比如现在有张J_USER这张表,T_USER表用 J 表 的数据 来更新 T 表的数据MERGE INTO t_user t using (select * from j_user j) b on ..._merge into t_b_info_bb b
文章浏览阅读141次。Metro(米雀)是微软在Windows Phone中正式引入的一种界面设计语言,也是Windows 8的主要界面显示风格。在Windows Phone之前,微软已经在Zune Player和XBox 360主机中尝试采用过类似的界面风格,并得到了用户的广泛认可。于是,微软在新发布的Windows Phone、已经发布的Windows 8预览版以及Office 15中也采用了Metro设计,今后的..._html metro