springcloud 配置中心熱更新方法

spring-cloud版本:Finchley.SR1

本文是用數(shù)據(jù)庫(kù)的方式存儲(chǔ)配置信息的,本人覺(jué)得 git 的方式實(shí)在不適合后期維護(hù)與管理

客戶端的pom 配置如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

客戶端yml配置 ,注意要配置上mq,需要用mq傳消息:

spring:
  cloud:
    config:
      discovery:
        enabled: true # 通過(guò)服務(wù)發(fā)現(xiàn)的方式去找配置中心
        serviceId: config_server # 配置中心的名字,直接配置名稱可以在配置中心集群的時(shí)候?qū)崿F(xiàn)負(fù)載均衡
      profile: dev # 對(duì)應(yīng)配置中心文件的${profile}部分
    bus:
      enabled: true
  rabbitmq:
      host: xxx.xxx.190.69
      port: 5672
      username: guest
      password: guest
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

客戶端代碼 :

@RestController
@RefreshScope
public class UserController {

    @Value("${mysql}")
    private String name;

    @RequestMapping(value = "/demo/getValue",method = RequestMethod.GET)
    public @ResponseBody  Object getBook(){
        return name;
    }

數(shù)據(jù)庫(kù)中要新增一條配置,注意application 列要寫(xiě)上服務(wù)名

image.png

調(diào)用查詢配置的接口查詢,沒(méi)有問(wèn)題ok

image.png

修改配置后,調(diào)刷新接口


image.png

再次調(diào)用查詢接口,發(fā)現(xiàn)配置文件已經(jīng)更新成功了,done.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容