咱們使用spring cloud分佈式微服務雲架構作了b2b2c的電子商務系統,除了架構自己自帶的系統服務外,咱們將b2b2c的業務服務進行了細粒度拆分,作成了不一樣的業務微服務。html
當咱們的業務系統愈來愈龐大複雜的時候,各類配置也會隨之增多。配置文件只要一修改,會對commonservice-config配置中心先中止服務,而後再從新啓動,最後使配置生效。java
若是服務少,咱們能夠手動方式來啓動,可是對業務和系統的穩定性確定有必定的影響。linux
若是是成百上千的服務都靠手動操做,我估計運維人員或技術人員會瘋掉的。git
針對以上問題,commonservice-config服務端和業務微服務分別作了相關的配置,服務端負責將git(svn或本地文件系統)中存儲的配置文件進行配置化(咱們使用的是本地配置方案,方便直接將配置文件更新到linux上),spring
業務微服務經過配置從服務端配置中心獲取相關配置,若是配置文件變更了,經過刷新業務微服務的方式,將最新的配置信息獲取。架構
spring cloud Bus經過一個輕量級消息代理鏈接分佈式系統的節點。這能夠用於廣播狀態更改(如配置更改)或其餘管理指令。app
接下來,咱們就來實施經過spring cloud Bus方案,動態刷新服務端配置,具體步驟以下:框架
1. commonservice-config服務配置運維
2. 業務微服務配置(以honghu-member-servcie會員服務爲例):分佈式
pom文件配置:
<span style="font-size: 16px;"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId> </dependency></span>
yml文件配置:
<span style="font-size: 16px;">server: 2. port: 5012 3.spring: 4. application: 5. name: honghu-member-client 6. profiles: 7. active: dev,discoveryClient 8. cloud: 9. config: 10. discovery: 11. enabled: true 12. service-id: commonservice-config-server 13. <span style="font-size: 16px;"><strong>name: honghu-member 14. profile: dev 15. bus: 16. trace: 17. enabled: true #開啓消息跟蹤 </strong> 18. <strong>rabbitmq: 19. host: 192.168.1.254 20. port: 5672 21. username: honghu 22. password: honghu</strong> </span> 23.eureka: 24. client: 25. serviceUrl: 26. defaultZone: http://honghu:123456@localhost:8761/eureka/ 27. instance: 28. prefer-ip-address: true 29.logging: 30. level: 31. root: INFO 32. org.springframework.security: INFO 33.management: 34. security: 35. enabled: false 36.security: 37. basic: 38. enabled: false</span>
3. 查看註冊中心,commonservice-config、honghu-member-service服務是否已經註冊成功
4. 訪問一下profile,獲取profile對應的配置信息(原配置):
訪問http://localhost:7071/profile ==》 訪問結果:123456
5. 修改config配置中心的配置文件,將profile=123456修改成honghu123456
再次訪問http://localhost:7071/profile ==》 訪問結果:123456
6. 使用spring cloud bus 刷新方案(使用post man測試工具進行測試)
http://localhost:7071/bus/refresh
再次訪問http://localhost:7071/profile ==》 訪問結果:honghu123456
到此,整個commonservice-config配置中心動態刷新方案整理完畢
歡迎你們和我一塊兒學習spring cloud構建微服務雲架構,我這邊會將近期研發的spring cloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud框架的朋友,你們來一塊兒探討spring cloud架構的搭建過程及如何運用於企業項目。完整項目的源碼來源