配置中心的好处坏处
好处
单体应用,配置写在配置文件中,没有什么大问题。如果要切换环境 可以切换不同的profile(2种方式),但在微服务中。
-
微服务比较多。成百上千,配置很多,需要集中管理。
-
管理不同环境的配置。
-
需要动态调整配置参数,更改配置不停服。
坏处
集中管理,修改错误影响较大
配置中心组成
分布式配置中心包括3个部分:
- 存放配置的地方:git ,本地文件 等。
- config server。从 1 读取配置。
- config client。是 config server 的客户端 消费配置。
配置都不会自己更新,都是需要触发client才去git上拉取的。或者触发 在config-server上查看配置时,才去git上拉取。
使用
- 环境部署之前,将所需的配置信息推送到配置仓库
- 启动配置中心服务端,将配置仓库的配置信息拉取到服务端,配置服务端对外提供RESTful接口
- 启动配置客户端,客户端根据 spring.cloud.config 配置的信息去服务器拉取相应的配置
config-server:
获取配置规则:根据前缀匹配
/{name}-{profiles}.properties
/{name}-{profiles}.yml
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml
name 服务名称
profile 环境名称,开发、测试、生产:dev qa prd
lable 仓库分支、默认master分支【写其他分支即可切换分支 】
匹配原则:从前缀开始。
cloud:
config:
server:
git:
uri: gitpath
username:
password:
#默认是秒,因为git慢
timeout: 15
config-client
配置bootstap.yml
#应用名称,配置文件名,此时:congif-client-dev.yml
spring:
application:
name: config-client
cloud:
config:
discovery:
enabled: true
# config server 的服务id
service-id: config-server
# 环境
profile: dev
# 分支
label: master
手动刷新
引入actuator,加上注解@RefreshScope
使用actuator/refresh,手动刷新
但是只能刷新单一端口服务。需要一个个的刷
自动刷新
//server,client pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
//server,client bootstrap.yml
spring:
application:
name: config-client
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
调用server /actuator/bus-refresh
钩子
配置git webhook