spring cloud bus中的env与refresh

1. 依赖

需要引入下面三个依赖:

compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')


(1)spring-cloud-starter-config是为了实现刷新配置 
(2)spring-boot-starter-actuator是为了暴露修改/刷新配置的接口 
(3)spring-boot-starter-web是为了可以访问暴露的修改/刷新配置的接口

2. 配置暴露接口
application.properties

#使用端口9999
server.port=9999
#暴露接口
management.endpoints.web.exposure.include=env,refresh

(1)env接口,可以获取配置(GET),也可以修改配置(POST) 
(2)refresh接口,可以刷新配置(POST),使得@RefreshScope标注的value可以重新注入。

3. @RefreshScope

在需要实时刷新配置的地方加上@RefreshScope注解

4. 启动服务
5. 修改配置

访问localhost:9999/actuator/env(GET),可以获得此时的配置 
访问localhost:9999/actuator/env(POST)

{
    "name":"somekey",
    "value":"newvalue"
}


如上可以把配置中somekey对应的值改为newvalue

6. 获取配置值

不调用刷新接口,直接获取注入的配置值,发现还是旧的值

7. 刷新配置 重新获取

访问localhost:9999/actuator/refresh(POST)刷新配置 
重新获取注入的配置值,发现是新的值

 

 

tips:如果刷新的只是字段,不添加@RefreshScope注解也可以刷新

如果是bean应用,那么就不行了,比如说 bean里面有个字段是 name,程序启动后,并修改了对应的配置,你通过bean获取就不行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值