1. 程式人生 > >springcloud(八):配置中心服務化和高可用

springcloud(八):配置中心服務化和高可用

artifact 會有 mage set color config 客戶 prop XML

在前兩篇的介紹中,客戶端都是直接調用配置中心的server端來獲取配置文件信息。這樣就存在了一個問題,客戶端和服務端的耦合性太高,如果server端要做集群,客戶端只能通過原始的方式來路由,server端改變IP地址的時候,客戶端也需要修改配置,不符合springcloud服務治理的理念。springcloud提供了這樣的解決方案,我們只需要將server端當做一個服務註冊到eureka中,client端去eureka中去獲取配置中心server端的服務既可。

這篇文章我們基於配置中心git版本的內容來改造

server端改造

1、添加依賴

 1 <dependencies>
 2
<dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-config-server</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-eureka</artifactId> 9
</dependency> 10 </dependencies>

需要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。

2、配置文件

 1 server:
 2 server:
 3   port: 8001
 4 spring:
 5   application:
 6     name: spring-cloud-config-server
 7   cloud:
 8     config:
 9       server:
10         git:
11           uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git倉庫的地址
12 search-paths: config-repo # git倉庫地址下的相對地址,可以配置多個,用,分割。 13 username: username # git倉庫的賬號 14 password: password # git倉庫的密碼 15 eureka: 16 client: 17 serviceUrl: 18 defaultZone: http://localhost:8000/eureka/ ## 註冊中心eurka地址

增加了eureka註冊中心的配置

3、啟動類

啟動類添加@EnableDiscoveryClient激活對配置中心的支持

1 @EnableDiscoveryClient
2 @EnableConfigServer
3 @SpringBootApplication
4 public class ConfigServerApplication {
5 
6     public static void main(String[] args) {
7         SpringApplication.run(ConfigServerApplication.class, args);
8     }
9 }

這樣server端的改造就完成了。先啟動eureka註冊中心,在啟動server端,在瀏覽器中訪問:http://localhost:8000/就會看到server端已經註冊了到註冊中心了。

技術分享

按照上篇的測試步驟對server端進行測試服務正常。

客戶端改造

1、添加依賴

 1 <dependencies>
 2     <dependency>
 3         <groupId>org.springframework.cloud</groupId>
 4         <artifactId>spring-cloud-starter-config</artifactId>
 5     </dependency>
 6     <dependency>
 7         <groupId>org.springframework.boot</groupId>
 8         <artifactId>spring-boot-starter-web</artifactId>
 9     </dependency>
10     <dependency>
11         <groupId>org.springframework.cloud</groupId>
12         <artifactId>spring-cloud-starter-eureka</artifactId>
13     </dependency>
14     <dependency>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-test</artifactId>
17         <scope>test</scope>
18     </dependency>
19 </dependencies>

需要多引入spring-cloud-starter-eureka包,來添加對eureka的支持。

2、配置文件

 1 spring.application.name=spring-cloud-config-client
 2 server.port=8002
 3 
 4 spring.cloud.config.name=neo-config
 5 spring.cloud.config.profile=dev
 6 spring.cloud.config.label=master
 7 spring.cloud.config.discovery.enabled=true
 8 spring.cloud.config.discovery.serviceId=spring-cloud-config-server
 9 
10 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的配置中

3、啟動類

啟動類添加@EnableDiscoveryClient激活對配置中心的支持

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class ConfigClientApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(ConfigClientApplication.class, args);
7     }
8 }

啟動client端,在瀏覽器中訪問:http://localhost:8000/ 就會看到server端和client端都已經註冊了到註冊中心了。

技術分享

高可用

為了模擬生產集群環境,我們改動server端的端口為8003,再啟動一個server端來做服務的負載,提供高可用的server端支持。

技術分享

如上圖就可發現會有兩個server端同時提供配置中心的服務,防止某一臺down掉之後影響整個系統的使用。

我們先單獨測試服務端,分別訪問:http://localhost:8001/neo-config/devhttp://localhost:8003/neo-config/dev返回信息:

 1 {
 2     "name": "neo-config", 
 3     "profiles": [
 4         "dev"
 5     ], 
 6     "label": null, 
 7     "version": null, 
 8     "state": null, 
 9     "propertySources": [
10         {
11             "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 
12             "source": {
13                 "neo.hello": "hello im dev"
14             }
15         }
16     ]
17 }

說明兩個server端都正常讀取到了配置信息。

再次訪問:http://localhost:8002/hello,返回:hello im dev update。說明客戶端已經讀取到了server端的內容,我們隨機停掉一臺server端的服務,再次訪問http://localhost:8002/hello,返回:hello im dev update,說明達到了高可用的目的。

示例代碼

springcloud(八):配置中心服務化和高可用