Spring Cloud Bus訊息匯流排+rabbitmq+Gradle(Greenwich.RELEASE版本)
阿新 • • 發佈:2020-12-02
準備工作
環境以及版本:
-
SpringBoot 2.1.3.RELEASE
-
SpringCloud Greenwich.RELEASE
-
Jdk 1.8
-
Gradle 5.2.1+
說明
以下改造均在子專案進行操作,父專案的github地址,下述需下載父專案用來管理公共依賴: https://github.com/cuifuan/springcloud-tools
1.改造你的 config-server
1.1 構建配置檔案 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: 倉庫檔案下資料夾 default-label: master username: git賬號 password: git密碼 bus: trace: enabled: true rabbitmq: host: rabbitmq地址 port: 5672 username: rabbit賬號【預設:guest】 password: rabbit密碼【預設:guest】 virtual-host: / eureka: # 修改在服務中心的地址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 啟動類 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 (客戶端)
用來讀取配置檔案的
2.1 構建配置檔案 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 #對應{application}部分
profile: dev #對應{profile}部分
#uri: http://localhost:8888/ #配置中心的具體地址
label: master #對應git的分支。如果配置中心使用的是本地儲存,則該引數無用
discovery:
enabled: true #開啟Config服務發現支援
service-id: config-server #指定配置中心的service-id,便於擴充套件為高可用配置叢集。
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:
#配置重試機制
retry:
initial-interval: 2000
max-attempts: 2000
max-interval: 2000
multiplier: 1.2
fail-fast: true
bus:
#動態重新整理配置
refresh:
enabled: true
#跟蹤匯流排事件
trace:
enabled: true
rabbitmq:
host: rabbitmq地址
port: 5672
username: rabbit賬號【預設:guest】
password: rabbit密碼【預設:guest】
#配置actuator
# 1.X版本的springboot 配置: management.security.enabled=false 已經作廢
#關閉安全認證
management:
endpoint:
bus-refresh:
enabled: true
#refresh接入點顯式暴露出來
endpoints:
web:
exposure:
include: refresh,bus-refresh
2.3 啟動類 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.遠端配置檔案
4.操作流程
-
啟動config-server
-
啟動config-client
-
訪問
http://127.0.0.1:7003/hello
-
之後更改遠端倉庫配置檔案,提交
-
訪問
http://127.0.0.1:7003/hello
發現無變化 -
用postman做post請求
http://10.0.0.82:7001/actuator/bus-refresh
或者用curl -X POST http://10.0.0.82:7001/actuator/bus-refresh
-
再次訪問http://127.0.0.1:7003/hello
這個時候的讀取的配置檔案已發生變化
5.需要注意的坑
-
請確定你的rabbitmq是通的,guest賬戶無法通過遠端ip訪問
-
注意上面的ip地址,我是這邊測試使用,請更改成個人正確或者本地
-
springboot2.x之後bus重新整理地址更改為
/actuator/bus-refresh
-
springboot和springcloud版本要一致,其他版本沒試過