javascript
SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)
文章目錄
- 消息總線(Bus)介紹
- 項(xiàng)目示例
- config-client-bus
代碼地址:github-spring-cloud地址
前言:前面文章講了Spring Cloud Config配置中心如何使用,當(dāng)我們?cè)诟耮it上面的配置,想要獲取最新的配置,需要手動(dòng)刷新或者利用webhook的機(jī)制每次提交代碼發(fā)送請(qǐng)求來刷新客戶端,客戶端越來越多的時(shí)候,需要每個(gè)客戶端都執(zhí)行一遍,這種方案就不太適合了。使用Spring Cloud Bus可以完美解決這一問題。使用消息總線需要依賴mq,本章文章將采用RabbitMq,具體怎么安裝RabbitMq可以查看此文章:安裝RabbitMq
消息總線(Bus)介紹
Spring cloud bus通過輕量消息代理連接各個(gè)分布的節(jié)點(diǎn)。這會(huì)用在廣播狀態(tài)的變化(例如配置變化)或者其他的消息指令。Spring bus的一個(gè)核心思想是通過分布式的啟動(dòng)器對(duì)spring boot應(yīng)用進(jìn)行擴(kuò)展,也可以用來建立一個(gè)多個(gè)應(yīng)用之間的通信頻道。目前唯一實(shí)現(xiàn)的方式是用AMQP消息代理作為通道,同樣特性的設(shè)置(有些取決于通道的設(shè)置)在更多通道的文檔中。
項(xiàng)目示例
本章節(jié)消息總線集成在客戶端,本章節(jié)在前幾張SpringCloud基礎(chǔ)上改進(jìn)。
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啟動(dòng)類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;} }-
創(chuàng)建配置文件注意兩點(diǎn):
rabbitmq的默認(rèn)端口為:5672 -
監(jiān)控 actuator 一定要配下面這幾個(gè),不然訪問http://localhost:8097/actuator會(huì)報(bào)404
management.endpoints.web.exposure.include= *
management.endpoint.health.show-details= always
management.endpoint.shutdown.enabled= true
客戶端分別啟動(dòng)兩個(gè)端口8097,8098
分別訪問地址:http://localhost:8098/hi和http://localhost:8097/hi
返回結(jié)果為:1233
我們對(duì)端口為8098的服務(wù)發(fā)送一個(gè)/actuator/bus-refresh的POST請(qǐng)求,在win10下使用下面命令來模擬webhook。curl -v -X POST http://localhost:8098/actuator/bus-refresh
返回結(jié)果為可以看到已經(jīng)把最新的結(jié)果返回:
注意一點(diǎn)springboot2.0訪問地址為:actuator/bus-refresh
本篇文章的消息總線在配置中心的使用機(jī)制用圖表示(圖片來源于網(wǎng)上)
總結(jié)
以上是生活随笔為你收集整理的SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Common常用jar包
- 下一篇: SpringCloud教程-消息总线Bu