Spring Cloud Config刷新配置
原创
©著作权归作者所有:来自51CTO博客作者已注销的原创作品,请联系作者获取转载授权,否则将追究法律责任
分享知识 传递快乐
在日常开发中一般都是直接把相关配置放在单独的配置文件中,通常以 properties 或者 yml 的格式出现,但是这样的方式有个明显的问题,那就是当配置文件发生改变的时候,必须重启服务才能使得新的配置文件生效,否则配置无法生效。
Spring Cloud Config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。如图:
对于 Spring Cloud 来说就是通过 Config 来获取配置中心的配置信息来实现的。目前有一些用的比较多的开源的配置中心,比如携程的 Apollo、蚂蚁金服的 disconf 等。
配置文件文件规则
Spring Cloud Config遵循了一套文件命名规则,程序可以根据这些规则读取不同的配置文件。
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
- {application}:就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。
- {profile}:就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。
- {label}:表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
注意:配置文件的文件名不是乱起的,Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上访问。
如果配置文件的名称是 application.properties/application.yml,如果直接通过该名称访问是获取不到的,因为在配置文件名需要通过 “-” 来进行获取,如果配置文件名称没有 “-”,那么添加了 “-” 之后,会自动进行匹配搜索。
示例:
先在 github 中建立配置文件
config/config-client-dev.yml
config/config-client-prod.yml
配置文件的内容大致如下:
data:
env: config-eureka-dev
创建服务端
1、新建 Spring Boot 项目,引入 以下配置:
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringCloud 整合 eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- SpringCloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2、配置 config 相关的配置项
bootstrap.yml 文件
spring:
cloud:
config:
server:
git:
uri: https://github.com/xxxxx/springcloud-config.git # 配置文件所在仓库
username: # 登录账号
password: # 登录密码
default-label: master # 配置文件分支
search-paths: config # 配置文件所在根目录,仓库地址下的相对地址 多个用逗号","分割。
# # 读取分支
# label: master
# 服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7000/eureka
register-with-eureka: true # 是否需要将自己注册到注册中心(注册中心集群需要设置为true),默认为true。因为自己是为注册中心,所以不需要自己注册自己
fetch-registry: true # 是否需要搜索服务信息(是否启用注册服务信息),因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;因为自己是为注册中心,不需要检索服务
application.yml 文件
server:
port: 8888 # 指定服务端口
spring:
application:
name: config-server # 指定服务名称
3、在启动类中加入 @EnableConfigServer注解
创建客户端
1、新建 Spring Boot 项目,引入以下 jar
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring-cloud 整合 config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 手动刷新客户端配置依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、配置 config 相关的配置项
bootstrap.yml 文件
spring:
profiles:
active: dev # 获取配置的策略
application:
name: config-client # 指定服务名称
cloud:
config:
profile: dev # 读取后缀(环境)
discovery:
service-id: config-server # 指定配置中心的service-id,便于扩展为高可用配置集群
enabled: true # 开启配置信息发现
label: master # 获取配置文件的分支,默认是master。如果是是本地获取的话,则无用。
#显示的暴露接入点
management:
endpoints:
web:
exposure:
include: refresh,health,info
# 服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7000/eureka
register-with-eureka: true # 是否需要将自己注册到注册中心(注册中心集群需要设置为true),默认为true。因为自己是为注册中心,所以不需要自己注册自己
fetch-registry: true # 是否需要搜索服务信息(是否启用注册服务信息),因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;因为自己是为注册中心,不需要检索服务
application.yml 文件
server:
port: 8889 # 指定服务端口
另外还要在使用配置中心的类上添加 @RefreshScope 注解,没有此注解刷新是不生效的。当配置更改时,标有 @RefreshScope 的 Bean 将得到特殊处理来生效配置。
使用 POST 刷新 http://[IP]:[客户端端口]/actuat/refresh
—————————
如有不足请留言指正
相互学习,共同进步