第八篇:SpringCloud之高可用的分佈式配置中心(Spring Cloud Config)

前一篇的介紹中,客戶端都是直接調用配置中心的server端來獲取配置文件信息。這樣就存在了一個問題,客戶端和服務端的耦合性太高,如果server端要做集羣,客戶端只能通過原始的方式來路由,server端改變IP地址的時候,客戶端也需要修改配置,不符合Spring Cloud服務治理的理念。Spring Cloud提供了這樣的解決方案,我們可以考慮將配置中心做成一個微服務,將其集羣化,從而達到高可用,架構圖如下:
在這裏插入圖片描述

準備工作

繼續使用上一篇文章的工程,創建一個eureka-server工程,用作服務註冊中心。在其pom.xml文件引入Eureka的起步依賴spring-cloud-starter-eureka-server

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

在配置文件application.yml上,指定服務端口爲8889,加上作爲服務註冊中心的基本配置,代碼如下:

server:
  port: 8088

eureka:
  instance:
    hostname: localhost
  client:
    #表明自己是一個eureka server
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

入口類:

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

改造Config-Server

在其pom.xml文件加上EurekaClient的起步依賴spring-cloud-starter-eureka

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

配置文件application.yml,指定服務註冊地址爲http://localhost:8088/eureka/,其他配置同上一篇文章,完整的配置如下:

server:
  port: 8090

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8088/eureka/

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenjary/SpringCloud/
          searchPaths: config-repo
          username:
          password:
      label: master

最後需要在程序的啓動類Application加上@EnableEurekaClient的註解。

改造Config-Client

將其註冊微到服務註冊中心,作爲Eureka客戶端,需要pom文件加上起步依賴spring-cloud-starter-eureka,代碼如下:

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

配置文件

spring.cloud.config.name=config
spring.cloud.config.profile=test
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.label=master

spring.application.name=config-client
server.port=8089

eureka.client.serviceUrl.defaultZone=http://localhost:8088/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的配置中

最後在啓動類添加@EnableEurekaClient激活對配置中心的支持

依次啓動sso-servr,config-server,config-client,在瀏覽器中訪問:http://localhost:8088/ 就會看到server端和client端都已經註冊了到註冊中心了。
在這裏插入圖片描述
訪問http://localhost:8089/hello,瀏覽器顯示:
在這裏插入圖片描述

高可用

爲了模擬生產集羣環境,我們改動server端的端口爲8091,再啓動一個server端來做服務的負載,提供高可用的server端支持。
在這裏插入圖片描述
如上圖就可發現會有兩個server端同時提供配置中心的服務,防止某一臺down掉之後影響整個系統的使用。
我們先單獨測試服務端,分別訪問:http://localhost:8090/config/tset、http://localhost:8091/neo-config/test返回信息:
在這裏插入圖片描述
在這裏插入圖片描述
說明兩個server端都正常讀取到了配置信息。

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

源碼下載:https://github.com/chenjary/SpringCloud/tree/master/springcloud-config-service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章