Spring Cloud Bus消息總線+rabbitmq+Gradle【Greenwich.RELEASE版本】

準(zhǔn)備工作

環(huán)境以及版本:
tools vision
IDEA 2018.3
SpringBoot 2.1.3 RELEASE
Gradle 5.2.1+
JDK 1.8
Spring Cloud Greenwich.RELEASE
說(shuō)明

以下改造均在子項(xiàng)目進(jìn)行操作,父項(xiàng)目的github地址贞间,下述需下載父項(xiàng)目用來(lái)管理公共依賴:
https://github.com/cuifuan/springcloud-tools

1.改造你的 config-server

1.1 構(gòu)建配置文件 build.gradle
dependencies {
    implementation "org.springframework.cloud:spring-cloud-config-server"
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
1.2 配置文件 application.yml
server:
  port: 7001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: git地址 #例如https://github.com/cuifuan/springcloud-configuration.git
          search-paths: 倉(cāng)庫(kù)文件下文件夾
          default-label: master
          username: git賬號(hào)
          password: git密碼
    bus:
      trace:
        enabled: true
  rabbitmq:
    host: rabbitmq地址
    port: 5672
    username: rabbit賬號(hào)【默認(rèn):guest】
    password: rabbit密碼【默認(rèn):guest】
    virtual-host: /
eureka:
# 修改在服務(wù)中心的地址status為 ip+端口 【例如:10.0.0.100:88】
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://springcloud-tools:8761/eureka/
info:
  app:
    description: This is Spring Cloud remote Registration center.
    name: tools-config-server
    version: 0.0
management:
  endpoint:
    bus-refresh:
      enabled: true
  endpoints:
    web:
      exposure:
        include: refresh,bus-refresh
1.3 啟動(dòng)類(lèi) ToolsConfigServerAppliaction.java
package store.zabbix.config;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableConfigServer
@SpringCloudApplication
@RestController
public class ToolsConfigServerAppliaction {

    public static void main(String[] args) {
        SpringApplication.run(ToolsConfigServerAppliaction.class, args);
    }

    @RequestMapping("/")
    public String home() {
        return "Hello World! My name is configserver.";
    }

}

2. 改造你的 config-client (客戶端)

用來(lái)讀取配置文件的

2.1 構(gòu)建配置文件 build.gradle
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
2.2 配置文件 bootstrap.yml
spring:
  cloud:
    config:
      name: tools-config-client     #對(duì)應(yīng){application}部分
      profile: dev                  #對(duì)應(yīng){profile}部分
      #uri: http://localhost:8888/  #配置中心的具體地址
      label: master                 #對(duì)應(yīng)git的分支贿条。如果配置中心使用的是本地存儲(chǔ),則該參數(shù)無(wú)用
      discovery:
        enabled: true                 #開(kāi)啟Config服務(wù)發(fā)現(xiàn)支持
        service-id: config-server   #指定配置中心的service-id增热,便于擴(kuò)展為高可用配置集群整以。
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://springcloud-tools:8761/eureka/
2.2 配置文件 application.yml
server:
  port: 7003
spring:
  application:
    name: tools-config-client
  cloud:
    config:
  #配置重試機(jī)制
      retry:
        initial-interval: 2000
        max-attempts: 2000
        max-interval: 2000
        multiplier: 1.2
      fail-fast: true
    bus:
      #動(dòng)態(tài)刷新配置
      refresh:
        enabled: true
      #跟蹤總線事件
      trace:
        enabled: true
  rabbitmq:
    host: rabbitmq地址
    port: 5672
    username: rabbit賬號(hào)【默認(rèn):guest】
    password: rabbit密碼【默認(rèn):guest】
#配置actuator
# 1.X版本的springboot 配置: management.security.enabled=false  已經(jīng)作廢
#關(guān)閉安全認(rèn)證
management:
  endpoint:
    bus-refresh:
      enabled: true
  #refresh接入點(diǎn)顯式暴露出來(lái)
  endpoints:
    web:
      exposure:
        include: refresh,bus-refresh
2.3 啟動(dòng)類(lèi) ConfigClientApplication.java

注意:一定不要忘了加@RefreshScop注解

package store.zabbix.configreader;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
@EnableDiscoveryClient
public class ConfigClientApplication {

    /**     * http://localhost:8881/actuator/bus-refresh     */

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${message}")
    String message;

    @GetMapping("/hello")
    public String getMessage(){
        return message;
    }

}

3.遠(yuǎn)程配置文件

image

4.操作流程

或者用

curl -X POST http://10.0.0.82:7001/actuator/bus-refresh

這個(gè)時(shí)候的讀取的配置文件已發(fā)生變化

5.需要注意的坑

  • 請(qǐng)確定你的rabbitmq是通的峻仇,guest賬戶無(wú)法通過(guò)遠(yuǎn)程ip訪問(wèn)
  • 注意上面的ip地址公黑,我是這邊測(cè)試使用,請(qǐng)更改成個(gè)人正確或者本地
  • springboot2.x之后bus刷新地址更改為 /actuator/bus-refresh
  • springboot和springcloud版本要一致摄咆,其他版本沒(méi)試過(guò)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凡蚜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子吭从,更是在濱河造成了極大的恐慌朝蜘,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件影锈,死亡現(xiàn)場(chǎng)離奇詭異芹务,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)鸭廷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)枣抱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人辆床,你說(shuō)我怎么就攤上這事佳晶。” “怎么了讼载?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵轿秧,是天一觀的道長(zhǎng)中跌。 經(jīng)常有香客問(wèn)我,道長(zhǎng)菇篡,這世上最難降的妖魔是什么漩符? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮驱还,結(jié)果婚禮上嗜暴,老公的妹妹穿的比我還像新娘。我一直安慰自己议蟆,他們只是感情好闷沥,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著咐容,像睡著了一般舆逃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上戳粒,一...
    開(kāi)封第一講書(shū)人閱讀 49,950評(píng)論 1 291
  • 那天路狮,我揣著相機(jī)與錄音,去河邊找鬼享郊。 笑死览祖,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的炊琉。 我是一名探鬼主播展蒂,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼苔咪!你這毒婦竟也來(lái)了锰悼?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤团赏,失蹤者是張志新(化名)和其女友劉穎箕般,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體舔清,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡丝里,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了体谒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片杯聚。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖抒痒,靈堂內(nèi)的尸體忽然破棺而出幌绍,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布傀广,位于F島的核電站颁独,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏伪冰。R本人自食惡果不足惜誓酒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望糜值。 院中可真熱鬧丰捷,春花似錦坯墨、人聲如沸寂汇。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)骄瓣。三九已至,卻和暖如春耍攘,著一層夾襖步出監(jiān)牢的瞬間榕栏,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工蕾各, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留扒磁,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓式曲,卻偏偏與公主長(zhǎng)得像妨托,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吝羞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容