javascript
SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)
文章目錄
- 消息總線(Bus)介紹
- 項目示例
- config-client-bus
代碼地址:github-spring-cloud地址
前言:前面文章講了Spring Cloud Config配置中心如何使用,當我們在更新git上面的配置,想要獲取最新的配置,需要手動刷新或者利用webhook的機制每次提交代碼發送請求來刷新客戶端,客戶端越來越多的時候,需要每個客戶端都執行一遍,這種方案就不太適合了。使用Spring Cloud Bus可以完美解決這一問題。使用消息總線需要依賴mq,本章文章將采用RabbitMq,具體怎么安裝RabbitMq可以查看此文章:安裝RabbitMq
消息總線(Bus)介紹
Spring cloud bus通過輕量消息代理連接各個分布的節點。這會用在廣播狀態的變化(例如配置變化)或者其他的消息指令。Spring bus的一個核心思想是通過分布式的啟動器對spring boot應用進行擴展,也可以用來建立一個多個應用之間的通信頻道。目前唯一實現的方式是用AMQP消息代理作為通道,同樣特性的設置(有些取決于通道的設置)在更多通道的文檔中。
項目示例
本章節消息總線集成在客戶端,本章節在前幾張SpringCloud基礎上改進。
config-client-bus
添加pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>config-client-bus</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency></dependencies> </project>添加配置文件bootstrap.properties
spring.application.name=config-client-bus spring.cloud.config.label=master spring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:8088/ server.port=8098eureka.client.service-url.defaultZone=http://localhost:8080/eureka/management.endpoints.web.exposure.include= * management.endpoint.health.show-details= always management.endpoint.shutdown.enabled= true info.app.name = spring-boot-actuator info.app.version = 1.0.0 info.app.test = test## 開啟消息跟蹤 spring.cloud.bus.trace.enabled=truespring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest啟動類Application
@SpringBootApplication @RestController @RefreshScope @EnableEurekaClient @EnableDiscoveryClient public class ConfigClientBusApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientBusApplication.class,args);}@Value("${helloConfig}")private String helloConfig;@RequestMapping(value = "/hi")public String helloConfig() {return helloConfig;} }-
創建配置文件注意兩點:
rabbitmq的默認端口為:5672 -
監控 actuator 一定要配下面這幾個,不然訪問http://localhost:8097/actuator會報404
management.endpoints.web.exposure.include= *
management.endpoint.health.show-details= always
management.endpoint.shutdown.enabled= true
客戶端分別啟動兩個端口8097,8098
分別訪問地址:http://localhost:8098/hi和http://localhost:8097/hi
返回結果為:1233
我們對端口為8098的服務發送一個/actuator/bus-refresh的POST請求,在win10下使用下面命令來模擬webhook。curl -v -X POST http://localhost:8098/actuator/bus-refresh
返回結果為可以看到已經把最新的結果返回:
注意一點springboot2.0訪問地址為:actuator/bus-refresh
本篇文章的消息總線在配置中心的使用機制用圖表示(圖片來源于網上)
總結
以上是生活随笔為你收集整理的SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Common常用jar包
- 下一篇: SpringCloud教程-消息总线Bu