十:Spring Cloud 之配置中心HA版-config

1. 簡介

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.

  • Spring Cloud Config 支持服務端與客戶端
  • 是一個分佈式配置中心
  • 提供配置信息多環境切換
  • 配置信息更新,實時同步
  • 藉助eureka實現高可用

2. 代碼實現

2.1 涉及的模塊及整體步驟

2.1.1 涉及的模塊

  • eureka-server-singleton:eureka服務發佈註冊中心
  • config-server-ha:配置中心服務端,通過指定不同端口啓動兩個實例模擬服務端集羣
  • config-client-use-ha:新建的通過HA版服務配置中心訪問遠程配置信息模塊,也可以使用原有模塊
  • config-repository:放置於GitHub的配置,config-client-use-ha-test.properties是對應的本次測試的保存配置信息的文件名稱

2.1.2 整體步驟

  1. GitHub創建存放配置信息的config-repository目錄與配置信息config-client-use-ha-test.properties,可通過demo中的config-repository模塊關聯GitHub上的配置。
  2. 實現eureka-server-singleton:eureka服務發佈註冊中心,與前面沒有任何區別
  3. 實現config-server-ha:關鍵是啓動Spring Cloud Config Server功能,指定配置倉庫的配置信息
  4. 實現config-client-use-ha:從配置倉庫讀取配置信息
  5. config-repository中添加config-client-use-ha-test.properties:放置4步驟的配置信息

2.2 源代碼

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 配置信息地址

配置信息在dev-20180827、與master分支都有,但是代碼中使用的是dev-20180827分支的代碼,如果想修改值驗證,記得確認客戶端bootstrap.properties中配置信息與想要訪問的倉庫地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository

2.3 eureka-server-singleton

Spring Cloud 之服務發現與調用-Ribbon#2.3 eureka-server-singleton 沒有任何區別

2.4 config-server-ha

2.4.1 整體實現

  1. pom.xml引入spring-cloud-config-server
  2. application.yml指定配置倉庫連接信息
  3. 啓動類使用註解啓動類使用註解@EnableConfigServer開啓配置服務功能

2.4.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.4.3 application-{profile}.yml

兩個配置文件除端口之外,其他配置項完一致,配置文件:application-8772.ymlapplication-8773.yml

spring:
  application:
    name: config-server-ha
  cloud:
    config:
      server:
        git:
          uri: https://github.com/andyChenHuaYing/spring-cloud-demo
          searchPaths: config-repository
          username:
          password:
          forcePull: true
      label: dev-20180827

server:
  port: 8772

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

2.4.4 ConfigServerHaApplication

package org.oscar.scd.config.server.ha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerHaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerHaApplication.class, args);
    }
}

2.5 config-client-use-ha

2.5.1 整體實現

  1. pom.xml文件中引入依賴spring-cloud-starter-configspring-cloud-starter-netflix-eureka-client
  2. bootstrap.yml中指定Config Server連接信息以及需要訪問配置中心的具體配置信息
  3. ConfigClientUseHaApplication常規Spring Boot啓動類

2.5.2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client-use-ha</artifactId>

    <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-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

2.5.3 bootstrap.yml

下方信息與配置中心的配置文件的對應關係見後續驗證部分
spring:
  application:
    name: config-client-use-ha
  cloud:
    config:
      label: dev-20180827
      profile: test
      discovery:
        enabled: true
        serviceId: config-server-ha

server:
  port: 8774
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.6 config-repository

保存配置文件

2.6.1 config-client-use-ha配置文件

config-client-use-ha-test.properties

foo=config-client-use-ha foo value.

3. 驗證

3.1 創建SpringBoot啓動類

簡單創建Spring Boot啓動類即可

3.1.1 EurekaServerSingletonApplication

最簡單的方式添加一個SpringBoot啓動類型的啓動類就行。

這裏寫圖片描述

3.1.2 ConfigServerHaApplication-8772

在這裏插入圖片描述

3.1.3 ConfigServerHaApplication-8773

參考上一節,修改Active profiles:8773

3.1.4 ConfigClientUseHaApplication

最簡單的方式添加一個SpringBoot啓動類型的啓動類就行。

3.2 啓動

  1. EurekaServerSingletonApplication
  2. ConfigServerHaApplication-8772
  3. ConfigServerHaApplication-8773
  4. ConfigClientUseHaApplication

3.3查看eureka服務信息界面

在這裏插入圖片描述

3.3 讀取遠程配置信息

3.3.1 ConfigClientUseHaApplication

  • 啓動之後可以根據指定規則訪問遠程配置完整信息,/{name}/{profile}/{label}
    • name:spring.application.name
    • profile:spring boot 的profile,即 application-{profile}.yml中的profile值,如application-dev.yml文件的profile是dev。
    • label:application.yml配置文件中spring.cloud.config.label的值,這裏是dev-20180827
  • 查看config-client-use-ha的bootstrap.yml信息可知,其對應的遠程配置文件爲https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-use-ha-test.properties.properties

訪問地址:http://localhost:8773/config-client-use-ha/test/dev-20180827 可查看config-client-use-ha完整配置信息
在這裏插入圖片描述

3.3.2 ConfigClientApplication

3.3.3 ConfigAnotherClientApplication

###3.3.4 Config Server HA驗證

  1. 重複啓動ConfigClientUseHaApplication:觀察啓動日誌中讀取遠程配置信息的server地址在http://localhost:8772/http://localhost:8773/之間切換
  2. 停止ConfigServerHaApplication-8772,再重複啓動ConfigClientUseHaApplication:觀察啓動日誌中讀取遠程配置信息的server地址只會是http://localhost:8772/
  3. 停止ConfigServerHaApplication-8773,再重複啓動ConfigClientUseHaApplication:啓動失敗

4. 思考

  • HA也涉及到負載均衡,有哪些負載均衡算法可用,如何指定
  • Spring Cloud Config有哪些常用的配置項,適用場景

5. 補充

5.1 資料

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html

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