在前兩篇的介紹中,客戶端都是直接調用配置中心的server端來獲取配置文件信息。這樣就存在了一個問題,客戶端和服務端的耦合性過高,若是server端要作集羣,客戶端只能經過原始的方式來路由,server端改變IP地址的時候,客戶端也須要修改配置,不符合springcloud服務治理的理念。springcloud提供了這樣的解決方案,咱們只須要將server端當作一個服務註冊到eureka中,client端去eureka中去獲取配置中心server端的服務既可。願意瞭解源碼的朋友直接求求交流分享技術:二一四七七七五六三三git
server端改造
一、添加依賴github
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
須要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。web
二、配置文件spring
server: server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git倉庫的地址 search-paths: config-repo # git倉庫地址下的相對地址,能夠配置多個,用,分割。 username: username # git倉庫的帳號 password: password # git倉庫的密碼 eureka: client: serviceUrl: defaultZone: http://localhost:8000/eureka/ ## 註冊中心eurka地址
增長了eureka註冊中心的配置bootstrap
三、啓動類
啓動類添加@EnableDiscoveryClient激活對配置中心的支持瀏覽器
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
這樣server端的改造就完成了。先啓動eureka註冊中心,在啓動server端,在瀏覽器中訪問:http://localhost:8000/就會看到server端已經註冊了到註冊中心了。app
按照上篇的測試步驟對server端進行測試服務正常。spring-boot
客戶端改造
一、添加依賴測試
<dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
須要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。code
二、配置文件
spring.application.name=spring-cloud-config-client server.port=8002 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增長了最後的三個配置:
spring.cloud.config.discovery.enabled :開啓Config服務發現支持
spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
eureka.client.serviceUrl.defaultZone :指向配置中心的地址
這三個配置文件都須要放到bootstrap.properties的配置中
三、啓動類
啓動類添加@EnableDiscoveryClient激活對配置中心的支持
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
啓動client端,在瀏覽器中訪問:http://localhost:8000/ 就會看到server端和client端都已經註冊了到註冊中心了。
高可用
爲了模擬生產集羣環境,咱們改動server端的端口爲8003,再啓動一個server端來作服務的負載,提供高可用的server端支持。
如上圖就可發現會有兩個server端同時提供配置中心的服務,防止某一臺down掉以後影響整個系統的使用。
咱們先單獨測試服務端,分別訪問:http://localhost:8001/neo-config/dev、http://localhost:8003/neo-config/dev返回信息:
{ "name": "neo-config", "profiles": [ "dev" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", "source": { "neo.hello": "hello im dev" } } ] }
說明兩個server端都正常讀取到了配置信息。
再次訪問:http://localhost:8002/hello,返回:hello im dev update。說明客戶端已經讀取到了server端的內容,咱們隨機停掉一臺server端的服務,再次訪問http://localhost:8002/hello,返回:hello im dev update,說明達到了高可用的目的。
總體代碼結構以下: