文章目录
Spring Cloud(11)——Spring Cloud Config 分布式配置中心
在Spring Cloud(10)——新一代网关Spring Cloud Gateway中,我们用新一代网关Gateway来保护、增强和控制对于 API 服务的访问。现在来解决分布式系统中配置繁杂的问题。
1、Spring Cloud Config 概述
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
Spring Cloud Config 就是一个解决分布式系统的配置管理方案。它为微服务架构中的微服务提供集中化的尾部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
在spring cloud config 组件中,分两个角色,一是config server,二是config client。
config server也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供配置信息,加密 / 解密信息等访问接口。
client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git。
一个配置中心提供的核心功能:
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署如dev/test/prod
- 运行期间动态调整配置,不需要在每个服务上编写配置,服务会向配置中心统一获取配置信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 配置信息以REST接口的形式暴露
Spring Cloud Config 中文文档 https://www.springcloud.cc/spring-cloud-config.html
2、Config 服务端配置
1、在 github 上创建一个名为 springcloud-config 的 仓库
2、克隆到本地的 E:\git-code\springcloud-config
使用git命令提交配置文件到github仓库中:
application-dev.yml
config:
info: "main branch,springcloud-config/application-dev.yml version=1"
3、新建cloud-config-center-3344模块
4、导入pom依赖
<dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.cheng.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
5、编写yml配置文件
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:Wpengcheng/springcloud-config.git
#搜索目录
search-paths:
- springcloud-config
#读取分支
label: main
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
6、创建主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
7、修改windows的hosts文件,增加映射 :127.0.0.1 config-3344.com
C:\Windows\System32\drivers\etc
8、测试
- 启动cloud-eureka-server7001模块
- 启动cloud-config-center-3344模块
测试通过cloud-config-center-3344微服务是否可以从GitHub上获取配置内容
访问请求:http://config-3344.com:3344/main/application-dev.yml
获取成功!
配置读取规则:/{分支}-{服务名}-{环境}.yml
分支:lable
服务名:name
环境:profiles
3、Config 客户端配置
1、创建cloud-config-client-3355模块
2、导入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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.cheng.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
3、编写 application.yml 配置文件
bootstrap.yml 使用户级的资源配置项
application.yml 是系统级的,优先级更高
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: main #分支名称
name: application #配置文件名称
profile: dev #读取后缀名称 上面三个拼接起来就是 main分支下的application-dev.yml配置文件
uri: http://localhost:3344 #config服务端地址 http://config-3344.com:3344/main/application-dev.yml
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
4、创建主启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
5、编写业务类
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo; //获取配置信息
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
6、测试
- 启动cloud-eureka-server7001模块
- 启动cloud-config-center-3344模块自测
访问请求:http://config-3344.com:3344/main/application-dev.yml 自测通过
- 启动cloud-config-client-3355模块
访问请求:http://localhost:3355/configInfo
成功实现了客户端3355通过访问Config 服务端3344,获取了GitHub上的配置信息
4、Config 客户端动态刷新
当运维修改了GitHub上的配置文件的内容时,ConfigServer配置中心立即响应,但ConfigClient没有响应,除非重启。但每次修改配置文件后,ConfigClient都要重新启动吗?下面就来解决这个问题。
1、在cloud-config-client-3355模块的bootstrap.yml 中添加:
#暴露监控端口
management:
endopints:
web:
exposure:
include: "*"
2、controller类上加@RefreshScope注解 ,让ConfigClient 具备刷新的功能
3、测试
-
修改GItHub上的配置文件的内容
-
启动cloud-config-center-3344模块自测
访问请求:http://config-3344.com:3344/main/application-dev.yml 自测通过
-
启动cloud-config-client-3355模块
这里需要发送post请求刷新Config:
命令行输入:curl -X POST “http://localhost:3355/actuator/refresh”
访问请求:http://localhost:3355/configInfo测试
Config 客户端成功实现动态刷新!
存在问题:
- 假如有过个微服务,每个微服务都要执行一程post请求吗?
- 能否自动刷新
这个问题留给Spring Cloud Bus 消息总线解决。