上一章最後講了,更新配置之後須要重啓客戶端才能生效,這在實際的場景中是不可取的。因爲目前Steeltoe配置的重載只能由客戶端發起,沒有實現處理程序偵聽服務器更改事件,因此還沒辦法實現完全實現這一功能。這一章的例子,客戶端的部分咱們採用Java來實現。Steeltoe更新之後我會及時把 .Net Core的實現方式補全。html
實際上也並不須要重啓,客戶端調用IConfigurationRoot.Reload()方法也能夠實現這個功能,可是去請求客戶端也不是一個好辦法,由於N節點的配置中心客戶端你沒辦法一一去調用。java
[HttpGet("/reload")] public string Reload() { _config?.Reload();//刷新配置 return _config?["name"]; }
上一章咱們一共建立了2個應用程序,一個配置中心服務端和一個配置中心客戶端。在分佈式場景中,任何單點都是有問題的,因此咱們首先對其優化,兩個配置中心服務端,三個配置中心客戶端,並所有註冊到服務中心。git
首先仍是建立一個服務中心的應用程序,參考第一章內容。
訪問http://localhost:5000
打開網站返回表示建立服務中心成功。
而後建立配置中心服務端,參考第四章內容,咱們經過修改配置文件來實現兩個服務端。端口分別爲5100,5200。
建立兩個配置文件application-s1.properties,application-s2.propertiesgithub
server.port=5100 spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/longxianghui/configs.git #git用戶名和密碼 #spring.cloud.config.server.git.username=xxx #spring.cloud.config.server.git.password=xxx #git倉庫目錄 #spring.cloud.config.server.git.search-paths=xxx,xxx,xxx eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/
server.port=5200 spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/longxianghui/configs.git #git用戶名和密碼 #spring.cloud.config.server.git.username=xxx #spring.cloud.config.server.git.password=xxx #git倉庫目錄 #spring.cloud.config.server.git.search-paths=xxx,xxx,xxx eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/
使用maven打包
而後在終端分別執行命令行web
java -jar target/config-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s1
java -jar target/config-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=s2
能夠開多終端或者直接在Intellij IDEA裏面運行
啓動完成後分別執行
http://localhost:5100/demo/prod,
http://localhost:5200/demo/prod,
返回數據啓動成功spring
使用IDEA建立spring boot程序bootstrap
<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.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
@SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
@Component @ConfigurationProperties() public class Demo { private String name; private Integer age; private String env; //get and set ... }
@RestController public class DemoController { @Autowired private Demo demo; @RequestMapping("demo") public Demo demo() { return demo; } }
爲了模擬集羣效果咱們建立3個配置文件服務器
spring.application.name=config-client server.port=5101
spring.application.name=config-client server.port=5102
spring.application.name=config-client server.port=5103
同時要很是注意的是,建立一個bootstrap.properties,這個配置在application配置以前啓動,相關的spring cloud config的配置都須要加到這個配置文件裏面,加到application配置文件裏面無效架構
spring.cloud.config.name=demo spring.cloud.config.profile=dev #開啓服務發現功能 spring.cloud.config.discovery.enabled=true #服務id spring.cloud.config.discovery.serviceId=config-server #服務中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/
打包代碼分別執行下面3行命令app
java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c1
java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c2
java -jar target/config-client-0.0.1-SNAPSHOT.jar --spring.profiles.active=c2
先訪問http://localhost:5000/,服務都註冊成功。
分別訪問
http://localhost:5101/demo
http://localhost:5102/demo
http://localhost:5103/demo
可以正常的訪問數據了,可是若是咱們修改git的配置信息,配置中心客戶端並不會主動獲取新的配置信息。咱們想一下有沒有辦法當咱們提交配置信息後通知給全部客戶端呢?說到通知你們立刻就想到了消息隊列,通知多客戶端的模式,是否是就是消息隊列裏面的廣播模式?想明白這點咱們繼續看下面的內容,繼續改造咱們的程序。spring cloud config提供了消息隊列模式,經過調用提供的REST接口來通知到客戶端來更新配置。
首先安裝rabbitmq
咱們繼續改造配置中心的服務端和客戶端
服務端和客戶端的配置同樣
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest management.security.enabled=false
分別啓動這5個服務(2個服務端,3個客戶端)
修改demo-dev.yml
name: mickey2017
並提交到github
rabbitmq提供了一個REST地址來刷新mq通知客戶端。咱們經過postman或者命令行來模擬post提交:http://localhost:5100/bus/refresh
這裏調用5200也是能夠的,調用客戶端的3個端口同樣能夠 (http://localhost:5101/bus/refresh) ,前面說了調用客戶端是不可取的,因此咱們調用服務端的地址。
最後訪問http://localhost:5101/demo
經過這一章內容咱們咱們成功的改造了配置中心,實際的開發場景,咱們總不能更新了配置就手動去模擬post提交吧?因此咱們能夠藉助git的webhook功能,當提交代碼之後就給配置中心服務端發送請求。再想深刻一點,配置中心服務端不對外網暴露呀?那麼咱們能夠用過經過Api網關來訪問,同時使用服務發現的方式,又解決了要指定具體配置中心地址的問題。
最近羣裏面總有同窗問受權的問題,那麼咱們下一章講微服務下的Api受權。
全部代碼均上傳github。代碼按照章節的順序上傳,例如第一章demo1,第二章demo2以此類推。
求推薦,大家的支持是我寫做最大的動力,個人QQ羣:328438252,交流微服務。
java部分
.net部分