Spring Boot + Spring Cloud 構建微服務系統(十):配置中心(Spring Cloud Bus)
技術背景
我們在上一篇講到,Spring Boot程式只在啟動的時候載入配置檔案資訊,這樣在GIT倉庫配置修改之後,雖然配置中心伺服器能夠讀取最新的提交資訊,但是配置中心客戶端卻不會重新讀取,以至於不能及時的讀取更新後的配置資訊。這個時候就需要一種通知重新整理機制來支援了。
Refresh機制
refresh機制是Spring Cloud Config提供的一種重新整理機制,它允許客戶端通過POST方法觸發各自的/refresh,只要依賴spring-boot-starter-actuator包就擁有了/refresh的功能,下面我們為我們的客戶端加上重新整理功能,以支援更新配置的讀取。
新增依賴
修改 spring-cloud-conifg-client,新增監控依賴,監控依賴包裡攜帶了 /refresh 的功能。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
開啟更新機制
在使用配置屬性的型別加上 @RefreshScope 註解,這樣在客戶端執行 /refresh 的時候就會重新整理此類下面的配置屬性了。
package com.louis.spring.cloud.config.client.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController class HelloController { @Value("${spring.config.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
修改配置
修改配置檔案新增以下內容,開放refresh的相關介面。
bootstrap.yml
management: endpoints: web: exposure: include: "*"
這樣,以後以post請求的方式訪問 http://localhost:8552/actuator/refresh 時,就會更新修改後的配置檔案了。
特別注意:
這裡存在著版本大坑,1.x跟2.x的配置不太一樣,我們用的是2.0+版本,務必注意。
1.安全配置變更
新版本
management.endpoints.web.exposure.include="*"
老版本
management.security.enabled=false
2.訪問地址變更
新版本
http://localhost:8552/actuator/refresh
老版本
http://localhost:8552/refresh
這裡還是解釋一下上面這個配置起到了什麼具體作用,其實actuator是一個健康檢查包,它提供了一些健康檢查資料介面,refresh功能也是其中的一個介面,但是為了安全起見,它預設只開放了health和info介面(啟動資訊會包含如下圖所示資訊),而上面的配置就是設定要開放哪些介面, 我們設定成 “*”,是開放所有介面。你也可以指定開發幾個,比如: health,info,refresh,而這裡因為我們需要用的refresh功能,所以需要把refresh介面開放出來。
設定成 “*” 後,啟動資訊會包含以下資訊,而這個叫refresh的post方法,就是我們需要的,上面說的介面地址變更從這裡也可以看得出來。
測試效果
訪問 ofollow,noindex">http://localhost:8552/hello ,返回結果如下。
修改倉庫配置內容,把數字2改成5,如下圖所示。
再次訪問 http://localhost:8552/hello ,如我們所料,結果並沒有更新,因為我們還沒有調refresh方法。
通過工具或自寫程式碼傳送post請求 http://localhost:8552/actuator/refresh ,重新整理配置。
這裡通過線上測試網站傳送,地址: https://getman.cn/Mo2FX 。
注意:先讓你的Chrome支援跨域。設定方法:在快捷方式的target後加上 --disable-web-security --user-data-dir,重啟即可。
重新整理之後,再次訪問 http://localhost:8552/hello ,返回結果如下。
檢視返回結果,重新整理之後已經可以獲取最新提交的配置內容,但是每次都需要手動重新整理客戶端還是很麻煩,如果客戶端數量一多就簡直難以忍受了,有沒有什麼比較好的辦法來解決這個問題呢,那是當然的,答案就是:Spring Cloud Bus。
Spring Cloud Bus
Spring Cloud Bus,被大家稱為訊息匯流排,它通過輕量級的訊息代理來連線各個分佈的節點,可以利用像訊息佇列的廣播機制在分散式系統中進行訊息傳播,通過訊息匯流排可以實現很多業務功能,其中對於配置中心客戶端重新整理,就是一個非常典型的使用場景。
下面這張圖可以很好的解釋訊息匯流排的作用流程(圖片描述來源: 純潔的微笑:配置中心博文 )。
Spring Cloud Bus 進行配置更新步驟如下:
1、提交程式碼觸發post請求給/actuator/bus-refresh
2、server端接收到請求併發送給Spring Cloud Bus
3、Spring Cloud bus接到訊息並通知給其它客戶端
4、其它客戶端接收到通知,請求Server端獲取最新配置
5、全部客戶端均獲取到最新的配置
安裝RabbitMQ
因為我們需要用到訊息佇列,我們這裡選擇RabbitMQ,使用Docker進行安裝。
拉取映象
執行以下命令,拉取映象。
docker pull rabbitmq:management
完成之後執行以下命令檢視下載映象。
docker images
建立容器
執行以下命令,建立docker容器。
docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 rabbitmq:management
啟動成功之後,可以執行以下命令檢視啟動容器。
docker ps
登入介面
容器啟動之後就可以訪問web管理介面了,訪問 http://宿主機IP:15672。
系統提供了預設賬號。 使用者名稱:guest 密碼: guest
管理介面
客戶端實現
新增依賴
開啟客戶端 spring-cloud-conifg-client,新增相關依賴。
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
修改配置
修改配置,新增RebbitMq的相關配置,這樣客戶端程式碼就改造完成了。
bootstrap.yml
spring: application: name: spring-cloud-config-client cloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name}# 註冊到consul的服務名稱 config: discovery: enabled: true serviceId: spring-cloud-config-server # 配置中心服務名稱 name: spring-config# 對應{application}部分 profile: dev# 對應{profile}部分 label: master# 對應git的分支,如果配置中心使用的是本地儲存,則該引數無用 rabbitmq: host: localhost port: 5672 username: guest password: guest management: endpoints: web: exposure: include: "*"
服務端實現
新增依賴
修改 spring-cloud-conifg-server,新增相關依賴。
pom.xml
<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>
修改配置,新增RebbitMq的和介面開放相關配置,這樣服務端程式碼也改造完成了。
application.yml
server: port: 8551 spring: application: name: spring-cloud-config-server cloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name}# 註冊到consul的服務名稱 config: server: git: uri: https://gitee.com/liuge1988/spring-cloud-demo/# 配置git倉庫的地址 search-paths: config-repository# git倉庫地址下的相對地址,可以配置多個,用,分割。 username: username# git倉庫的賬號 password: password# git倉庫的密碼 rabbitmq: host: localhost port: 5672 username: guest password: guest management: endpoints: web: exposure: include: "*"
測試效果
1.啟動服務端,成功整合訊息匯流排後,啟動資訊中可以看到如下圖中的資訊。
2.啟動客戶端,發現居然報錯了,網上也找不到相關資料,也沒見其他人提過相關問題。猜測是網上教程多是使用Euraka,而這裡用的時Consul,瞎鼓搗了好久,反正是不想換回Euraka,2.0停止開發訊息出來以後,將來還不定什麼情況,只能硬著頭皮解決了。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'configServerRetryInterceptor' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:685) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.getDelegate(AnnotationAwareRetryOperationsInterceptor.java:180) ~[spring-retry-1.2.2.RELEASE.jar:na] at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:151) ~[spring-retry-1.2.2.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.cloud.config.client.ConfigServerInstanceProvider$$EnhancerBySpringCGLIB$$dd44720b.getConfigServerInstances(<generated>) ~[spring-cloud-config-client-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:84) [spring-cloud-config-client-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.startup(DiscoveryClientConfigServiceBootstrapConfiguration.java:69) [spring-cloud-config-client-2.0.0.RELEASE.jar:2.0.0.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
然後就跟蹤程式碼,發現是在下圖中的位置找不到相應的Bean,那麼答案就比較明顯了,要麼是程式有BUG,不過可能性不大,那應該是就是缺包了,在缺失的包裡有這個Bean。但是這個Bean是在哪個包?排查了半天也沒找到,網上也沒有想過資料,對比了一下網上訊息匯流排的配置,依賴也沒有少加什麼。
沒有辦法,最後只能自己上手了,不就是在重新整理的時候缺少一個攔截器嗎,自己給他弄一個試試唄。
使用就加了一個配置類,並在resources下新建了META-INF目錄和一個spring。factories檔案。
RetryConfiguration.java
package com.louis.spring.cloud.config.client; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.retry.interceptor.RetryInterceptorBuilder; import org.springframework.retry.interceptor.RetryOperationsInterceptor; public class RetryConfiguration { @Bean @ConditionalOnMissingBean(name = "configServerRetryInterceptor") public RetryOperationsInterceptor configServerRetryInterceptor() { return RetryInterceptorBuilder.stateless().backOffOptions(1000, 1.2, 5000).maxAttempts(10).build(); } }
spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=com.louis.spring.cloud.config.client.RetryConfiguration
在這裡指定新建的攔截器,這樣系統初始化時會載入這個Bean。
然後重啟啟動,果然沒有報錯了,還是先別高興,看看能不能用先。
4.先訪問一下 http://localhost:8552/hello ,效果如下圖所示。
5.修改倉庫配置檔案,把數字5改成15,修改完成提交。
再次訪問發現還是舊資訊。
6.再用工具傳送post請求 http://localhost:8551/actuator/bus-refresh 。
注意這次是向註冊中心服務端傳送請求,傳送成功之後服務端會通過訊息匯流排通知所有的客戶端進行重新整理。
另外開啟訊息匯流排後的請求地址是 /actuator/bus-refresh,不再是refresh了。
7.給服務端傳送重新整理請求之後,再次訪問 http://localhost:8552/hello ,結果如下。
我們愉快的發現客戶端已經能夠通過訊息匯流排獲取最新配置了,真是可喜可賀。
原始碼下載
碼雲: https://gitee.com/liuge1988/spring-cloud-demo.git
作者:朝雨憶輕塵
出處: https://www.cnblogs.com/xifengxiaoma/
版權所有,歡迎轉載,轉載請註明原文作者及出處。