Spring Cloud Bus 將分布式的節(jié)點(diǎn)用輕量的消息代理連接起來(lái)。它可以用于廣播配置文件的更改或者服務(wù)之間的通訊,也可以用于監(jiān)控。本文要講述的是用Spring Cloud Bus實(shí)現(xiàn)通知微服務(wù)架構(gòu)的配置文件的更改。
一、準(zhǔn)備工作 本文還是基于上一篇文章來(lái)實(shí)現(xiàn)。按照官方文檔,我們只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;這就是說(shuō)我們需要裝rabbitMq,點(diǎn)擊rabbitmq下載。至于怎么使用 rabbitmq,搜索引擎下。
二、改造config-client 在pom文件加上起步依賴(lài)spring-cloud-starter-bus-amqp,完整的配置文件如下:
<?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"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-client</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</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-aop</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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-actuator</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>
在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用戶(hù)名、密碼,代碼如下:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
# spring.rabbitmq.username=
# spring.rabbitmq.password=
如果rabbitmq有用戶(hù)名密碼,輸入即可。
依次啟動(dòng)eureka-server、confg-cserver,啟動(dòng)兩個(gè)config-client,端口為:8881、8882。
訪(fǎng)問(wèn)http://localhost:8881/hi 或者h(yuǎn)ttp://localhost:8882/hi 瀏覽器顯示:
foo version 3
這時(shí)我們?nèi)ゴa倉(cāng)庫(kù)將foo的值改為“foo version 4”,即改變配置文件foo的值。如果是傳統(tǒng)的做法,需要重啟服務(wù),才能達(dá)到配置文件的更新。此時(shí),我們只需要發(fā)送post請(qǐng)求:http://localhost:8881/bus/refresh,你會(huì)發(fā)現(xiàn)config-client會(huì)重新讀取配置文件
重新讀取配置文件:
這時(shí)我們?cè)僭L(fǎng)問(wèn)http://localhost:8881/hi 或者h(yuǎn)ttp://localhost:8882/hi 瀏覽器顯示: 架構(gòu)代碼如下:
總結(jié)
以上是生活随笔 為你收集整理的SpringCloud微服务云架构构建B2B2C电子商务平台之-(八)消息总线(Spring Cloud Bus)... 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。