1.config 服务端(即配置中心)配置(application.yml)
- 【需要spring-cloud-config-server依赖】
1.1application.yml配置文件中加入
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:DearMrJ/springcloud-config.git
search-paths:
- springcloud-config
label: master
# username: # git 仓库用户名(公开库可以不用填写)
# password: # git 仓库密码(公开库可以不用填写)
- 如果报错Cannot clone or checkout repository:
git@github.com:xxxx,则可能是未配置ssh连接,配置流程如下:
I.首先在本地创建 SSH Keys $ ssh-keygen -t rsa -C "xx@yy.com" 成功的话会在 ~/(即C:/users/[username/]) 下生成 .ssh文件夹,进去, 打开 id_rsa.pub(可以在git bash中使用cat输出),复制里面的key. II.到GitHub上添加ssh key(右上角个人头像->settings->SSH and GPG keys->new SSH keys->粘入id_rsa.pub内容) III.验证 SSH Keys 是否添加成功 $ ssh -T git@github.com
1.2主程序类@EnableConfigServer启用配置服务
1.3访问测试
http://config-3344.com:3344/master/config-prod.yml
(hosts文件配置域名解析)
2.config 客户端配置(bootstrap.yml)
- 【需要spring-cloud-starter-config依赖】,区别于服务端(配置中心)
2.1bootstrap.yml配置文件中加入
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config #配置文件名称
profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
uri: http://config-3344.com:3344 #配置中心的地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
2.2【Config客户端之动态刷新】
2.2.1半自动动态刷新
-
POM引入actuator监控
-
修改bootstrap.yml,暴露监控端口
management: endpoints: web: exposure: include: "*"
-
【业务类Controller@RefreshScope】
-
【需要运维人员发送POST请求刷新3355/3366…】
【curl -X POST "http://localhost:3355/actuator/refresh"
】
2.2.2【全自动动态刷新】
-
需要【Springcloud-bus消息总线】,springcloud-bus目前只支持rabbitmq和kafka;
-
在配置中心和各个客户端加入【spring-cloud-starter-bus-amqp】,伴随着【spring-boot-starter-actuator】,因为在暴露bus刷新配置(见yml)的端点时候,需要actuator模块的依赖。
- 配置中心application.yml如下:
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:DearMrJ/springcloud-config.git
search-paths:
- springcloud-config
label: master
# username: # git 仓库用户名(公开库可以不用填写)
# password: # git 仓库密码(公开库可以不用填写)
rabbitmq:
host: 192.168.66.66
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
# rabbitmq相关配置,暴露bus刷新配置的端点,因此需要actuator模块的依赖
management:
endpoints: # 暴露bus刷新配置的断电
web:
exposure:
include: "bus-refresh"
- 客户端bootstrap.yml配置如下:
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config #配置文件名称
profile: dev # 读取的后缀,上述三个综合,为master分支上的config-dev.yml的配置文件被读取,http://config-3344.com:3344/master/config-dev.yml
uri: http://config-3344.com:3344 #配置中心的地址
rabbitmq: #rabbitmq相关配置,15672是web管理端口,5672是mq访问端口
port: 5672
host: 192.168.66.66
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
- 全局广播
curl -X POST "http://localhost:3344/actuator/bus-refresh
即通知配置中心,进行广播。
- 定点通知
curl -X POST "http://localhost:3344/actuator/bus-refresh/{applicationName}:{port}"
如:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"