我們使用spring cloud分布式微服務(wù)云架構(gòu)做了b2b2c的電子商務(wù)系統(tǒng)余素,除了架構(gòu)本身自帶的系統(tǒng)服務(wù)外翔脱,我們將b2b2c的業(yè)務(wù)服務(wù)進(jìn)行了細(xì)粒度拆分讯壶,做成了不同的業(yè)務(wù)微服務(wù)霹菊。
當(dāng)我們的業(yè)務(wù)系統(tǒng)越來(lái)越龐大復(fù)雜的時(shí)候椭住,各種配置也會(huì)隨之增多趁窃。配置文件只要一修改牧挣,會(huì)對(duì)commonservice-config配置中心先停止服務(wù),然后再重新啟動(dòng)醒陆,最后使配置生效瀑构。
如果服務(wù)少,我們可以手動(dòng)方式來(lái)啟動(dòng)刨摩,但是對(duì)業(yè)務(wù)和系統(tǒng)的穩(wěn)定性肯定有一定的影響寺晌。
如果是成百上千的服務(wù)都靠手動(dòng)操作,我估計(jì)運(yùn)維人員或技術(shù)人員會(huì)瘋掉的澡刹。
針對(duì)以上問題呻征,commonservice-config服務(wù)端和業(yè)務(wù)微服務(wù)分別做了相關(guān)的配置,服務(wù)端負(fù)責(zé)將git(svn或本地文件系統(tǒng))中存儲(chǔ)的配置文件進(jìn)行配置化(我們使用的是本地配置方案罢浇,方便直接將配置文件更新到linux上)陆赋,
業(yè)務(wù)微服務(wù)通過配置從服務(wù)端配置中心獲取相關(guān)配置,如果配置文件變動(dòng)了嚷闭,通過刷新業(yè)務(wù)微服務(wù)的方式攒岛,將最新的配置信息獲取。
spring cloud Bus通過一個(gè)輕量級(jí)消息代理連接分布式系統(tǒng)的節(jié)點(diǎn)胞锰。這可以用于廣播狀態(tài)更改(如配置更改)或其他管理指令灾锯。
接下來(lái),我們就來(lái)實(shí)施通過spring cloud Bus方案嗅榕,動(dòng)態(tài)刷新服務(wù)端配置顺饮,具體步驟如下:
1. commonservice-config服務(wù)配置可以參考之前的鏈接:
http://2147775633.iteye.com/admin/blogs/2396692
2. 業(yè)務(wù)微服務(wù)配置(以honghu-member-servcie會(huì)員服務(wù)為例):
? ?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:??
port:5012??
spring:???
??application:??
????name:?honghu-member-client??
??profiles:??
????active:?dev,discoveryClient??
??cloud:??
????config:??
??????discovery:???
enabled:true??
????????service-id:?commonservice-config-server??
??????profile:?dev??
????bus:??
??????trace:??
enabled:true??#開啟消息跟蹤??</strong>??????????
??<strong>rabbitmq:??
host:192.168.1.254??
port:5672??
????username:?honghu??
????password:?honghu</strong>??</span>???
eureka:??
??client:??
????serviceUrl:??
defaultZone:?http://honghu:123456@localhost:8761/eureka/??
??instance:??
prefer-ip-address:true??
logging:??
??level:??
????root:?INFO??
????org.springframework.security:?INFO??
management:??
??security:??
enabled:false??
security:??
??basic:??
enabled:false</span>??
? ? 編寫一個(gè)測(cè)試類(MemberController.java),用來(lái)獲取配置項(xiàng)
<span?style="font-size:?16px;">package?com.honghu.cloud.controller;??
import?org.springframework.beans.factory.annotation.Value;??
import?org.springframework.cloud.context.config.annotation.RefreshScope;??
import?org.springframework.web.bind.annotation.GetMapping;??
import?org.springframework.web.bind.annotation.RestController;??
@RefreshScope</strong>??
@RestController??
public?class?MemberController?{??
@Value("${profile}")??
private?String?profile;??
@GetMapping("/profile")??
public?String?getProfile()?{??
return?this.profile;??
????}??
}</span>??
3. 查看注冊(cè)中心凌那,commonservice-config兼雄、honghu-member-service服務(wù)是否已經(jīng)注冊(cè)成功
4. 訪問一下profile,獲取profile對(duì)應(yīng)的配置信息(原配置):
訪問http://localhost:7071/profile? ==》 訪問結(jié)果:123456
5. 修改config配置中心的配置文件案怯,將profile=123456修改為honghu123456
再次訪問http://localhost:7071/profile? ==》 訪問結(jié)果:123456
6. 使用spring cloud bus 刷新方案(使用post man測(cè)試工具進(jìn)行測(cè)試)
http://localhost:7071/bus/refresh
再次訪問http://localhost:7071/profile? ==》 訪問結(jié)果:honghu123456
到此君旦,整個(gè)commonservice-config配置中心動(dòng)態(tài)刷新方案整理完畢(企業(yè)架構(gòu)源碼可以加求球:三五三六二四七二五九)
歡迎大家和我一起學(xué)習(xí)spring cloud構(gòu)建微服務(wù)云架構(gòu),我這邊會(huì)將近期研發(fā)的spring cloud微服務(wù)云架構(gòu)的搭建過程和精髓記錄下來(lái)嘲碱,幫助更多有興趣研發(fā)spring cloud框架的朋友金砍,大家來(lái)一起探討spring cloud架構(gòu)的搭建過程及如何運(yùn)用于企業(yè)項(xiàng)目。