SpringCloud——Config
什么是Config
-
微服务意味着要将单体应用中的业务拆分成一个个子服务, 每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
-
SpringCloud Config是一个提供外部集中式配置管理的设施,配置服务器为各种不同的微服务应用提供了一个中心化的外部配置
Config可以干什么
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,让服务中心统一为服务拉取配置文件
- 当配置发生变动时,服务不需要重启即可感知配置变化并应用
- 将配置信息以REST接口形式暴露
Config实例
-
新建model
-
改pom
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <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> </dependencies>
-
改yml
server: port: 3344 spring: application: name: cloud-config-server cloud: config: server: git: uri: https://gitee.com/JD_Magi/config_server.git search-paths: /** username: xxxxxxx password: xxxxxxx default-label: master eureka: client: service-url: defaultZone: http://localhost:7001/eureka
测试结果
客户端使用Config
-
建model
-
改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> </dependencies>
-
新建bootstrap.yml
bootstrap.yml是什么
与application.yml的区别
- application.yml配置文件是用户级的资源配置项
- bootstrap.yml是系统级的资源配置项,它的优先级更高。
详解
- SpringCloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。初始化的时候,BootstrapContext负责从外部资源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap Context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml。
server:
port: 3355
spring:
application:
name: config-client
cloud:
# config客户端配置
config:
# 分支名称
label: master
# 配置文件名称
name: config
# 读取后缀名称
profile: dev
# 配置中心地址
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
- 写业务
- 测试
手动刷新Config
如果说我们想要对某个配置文件的内容进行修改,那么修改之后刷新Config的服务端,配置中心会立即响应,但是它的客户端没有任何响应,除非客户端自己重启或者重新加载。这就是Config配置中心的分布式配置的刷新问题。
- 改pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- 在controller添加注解
3.启动测试
修改gitee上的文件后刷新
curl -X POST “http://localhost:3355/actuator/refresh”
更新成功
缺点:每个微服务都要执行—次post请求,手动刷新