一、概述
1、分布式系统面临的问题
1、微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以需要一套集中式的、动态的配置管理设施是必不可少的。
2、是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为
各个不同微服务应用
的所有环境提供了一个中心化的外部配置
。
3、怎么操作
SpringCloud Config分为服务端和客户端两部分。
4、与GitHub整合配置
由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持svn和本地文件,但最推荐的还是Git,而且使用的是http/https访问的形式)
二、Config服务端配置
1、配置git
1、登陆自己的git账号
2、创建一个仓库(springcloud-config)
3、在本地新建一个文件夹拉取git上的项目:
git clone 项目地址
4、在项目中添加几个配置文件(application-dev.yml,application-prod.yml,application-test.yml)
5、配置文件中写点东西即可
2、新建一个Module
项目名:cloud-config-center-3344
1、pom配置
<dependencies>
<!--新引入config依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.itan</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
2、yml配置
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
# 服务端配置
server:
git:
# 如果报错invalid privatekey: [B@761e233b,请将uri地址换成https开头的即可
# 如果报错Auth fail,请添加username,password属性并写上自己git账号和密码即可
uri: https://github.com/itan-yyb/springcloud-config.git #GitHub上面的git仓库地址
# 搜索目录
search-paths:
- springcloud-config
# 读取分支
label: master
eureka:
client:
service-url:
# 注册进 eureka 服务列表内
defaultZone: http://eureka7001.com:7001/eureka
3、主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenter3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenter3344.class,args);
}
}
4、测试
1、启动注册中心7001
2、启动ConfigCenter3344
3、在浏览器中访问http://localhost:3344/master/application-dev.yml,发现可以从Github上获取配置内容
3、配置读取及规则
1、HTTP服务具有以下形式的资源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml——>
最推荐使用
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
lable:分支(branch)
application:服务名
profile:环境(dev/test/prod)
三、Config客户端配置
1、新建一个Module
项目名:cloud-config-client-3355
1、pom配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.itan</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
2、yml配置
yml优先级说明:
Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的“Application Context”的
父上下文
。初始化的时候,“Bootstrap Context”负责从外部源
加载配置属性并解析配置,这两个上下文共享一个从外部获取的“Environment”。“Bootstrap ”属性有高优先级,默认情况下,它们不会被本地配置覆盖。“Bootstrap Context”和“Application Context”有着不同的约定,所以新增了一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Context”配置的分离。
将application.yml改为bootstrap.yml并配置
server:
port: 3355
spring:
application:
name: config-client #注册进Eureka服务器的微服务名
cloud:
# config客户端配置
config:
# 分支名
label: master
# 配置文件名称
name: application
# 读取后缀名称
profile: dev
# 配置中心地址
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
3、主启动类
@SpringBootApplication
public class ConfigClient3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3355.class,args);
}
}
4、业务类
@RestController
@RequestMapping("config")
public class ConfigClientController {
@Value("${user.name}")
private String name;
@GetMapping("getOne")
public String getOne(){
return name;
}
}
5、测试
1、启动Eureka7001
2、启动Config服务端3344
3、启动Config客户端3355
4、postman访问http://localhost:3355/config/getOne,3355成功的访问3344通过GitHub获取配置信息
2、发现问题
每次更新配置3355无法获取到最新配置,3344可以获取,
就需要重启3355服务才能获取修改后的配置,针对此类问题,就需要配置动态刷新
四、动态刷新
1、客户端修改
1、pom引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、yml中暴露监控端口
server:
port: 3355
spring:
application:
name: config-client #注册进Eureka服务器的微服务名
cloud:
# config客户端配置
config:
# 分支名
label: master
# 配置文件名称
name: application
# 读取后缀名称
profile: dev
# 配置中心地址
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
# 暴露监控端口
management:
endpoints:
web:
exposure:
include: "*"
3、控制层加@RefreshScope
注解
@RefreshScope
@RestController
@RequestMapping("config")
public class ConfigClientController {
@Value("${de.name}")
private String name;
@GetMapping("getOne")
public String getOne(){
return name;
}
}
2、测试
1、重启客户端服务3355
2、github上修改配置
3、postman中测试,发现配置还是没有改变,还是之前的配置
4、解决办法:需要手动发送post请求刷新3355服务
5、再次执行步骤3中的操作,发现能够获取到最新的数据