SpringCloud(五)Config分布式配置中心


Config 分布式配置中心

概述

微服務意味着要將單體應用中的業務拆分成個個子服務,每個服務的粒度相對較小因此系統中會出現大量的服務
由於每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態的配置管理設施是必不可少的
Spring Cloud提供了 ConfigServer 來解決這個問題,我們每個微服務自己帶着一個 application.yml,上百個配置文件的管理會導致膨脹
官方地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/

Spring Cloud Config為微服務架構中的微服務提供集中化的外部配置攴持,配置服務器為各個不同微服務應用的所有環境提供了一個中心化的配置
Spring Cloud Config分為服務端客戶端兩部分

  • 服務端也稱為分布式配置中心,它是獨立的微服務應用,用來連接配置服務器並為客戶端提供獲取配置信息,加密解密信息等訪問接口
  • 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啟動的時候從配置中心獲取和加載配置信息,配置服務器默認采用 git 來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過 git 客戶端工具來方便的管理和訪問配置內容

主要分支

  • 集中管理配置文件
  • 不同環境不同配置,動態化的配置更新,分環境部署比如 dev/test/prod/beta/release
  • 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息
  • 當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置
  • 將配置信息以REST接口的形式暴露
  • Github整合配置:由於 Spring Cloud Config默認使用Git來存儲配置文件(也有其它方式比如支持SVN和本地文件),但最推薦的還是Git,而且使用的是http/https訪問的形式

服務端配置

首先要在 Github 上創建一個 Config 倉庫,來配置環境

  1. 新建一個module
  2. 修改 pom 依賴
<!-- config-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 配置 yml
server:
  port: 3344

spring:
  application:
    name: Config-center
  cloud:
    config:
      server:
        git:
          # Github 上倉庫的名字
          uri: https://github.com/odousnag/SpringCloudStudy.git
          # 搜索目錄
          search-paths:
            - springcloud-config
      # 讀取分支
      label: master

# Eureka
eureka:
  client:
    service-url:
      # 單機版
      # defaultZone: http://localhost:7001/eureka/
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 主啟動類開啟注解
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain.class,args);
    }
}
  1. windows 下更改 hosts 增加映射
  2. 測試是否能從 Github 上獲取配置內容

    啟動配置中心:http://config3344.com:3344/master/config-dev.yml

    7.配置讀寫規則
    官網上的配置讀寫規則
常用的三種:
/{label}/{application}-{profile}.properties
  master 分支:http://config3344.com:3344/master/config-xxx.yml
  dev 分支:http://config3344.com:3344/dev/config-xxx.yml

/{application}/{profile}[/{label}]
  http://config3344.com:3344/config-xxx.yml

/{application}-{profile}.yml
  http://config3344.com:3344/config/xxx/master

客戶端配置

  1. 新建一個module
  2. 修改 pom 依賴
<!-- spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 新建一個 bootstrap.yml
    applicaiton.yml 是用戶級的資源配置項,bootstrap.yml是系統級的,優先級更加高
    Spring Cloud會創建一個 "Bootstrap Context",作為 Spring應用的 Application Context的父上下文
    初始化的時候, Bootstrap Context 負責從外部源加載配置屬性並解析配置,這兩個上下文共享—個從外部獲取的 Environment
    將 Client 模塊下的 application.yml 文件改為 bootstrap.yml 這是很關鍵的,因為 bootstrap.yml是比 application.yml 先加載的
    bootstrap.yml 優先級高於 application.yml
server:
  port: 3355

spring:
  application:
    name: Config-client
  cloud:
    # Config 客戶端配置
    config:
      # 讀取分支
      label: master
      # 配置文件名稱
      name: config
      # 讀取名稱后綴
      profile: dev
      # 配置中心地址
      uri: http://localhost:3344

# Eureka
eureka:
  client:
    service-url:
      # 單機版
      # defaultZone: http://localhost:7001/eureka/
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
  1. 修改 application-dev.yml 配置並提交到 Github 中,比如加個變量 age 或者版本號 version
  2. 業務類測試
@RestController
public class ClientController {

    /**
     * 獲取application-dev的信息
     */
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}


實現了客戶端訪問SpringCloudConfig通過GitHub獲取配置信息

客戶端動態刷新

動態刷新問題

Linux運維修改GitHub上的配置文件內容做調整,刷新服務端,發現ConfigServer配置中心立刻響應,刷新客戶端,發現ConfigClient客戶端沒有任何響應
修改客戶端

  1. 引入 actuator 監控依賴
<!-- spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 修改 yml 配置,暴露監控端口
# 暴露監控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"
  1. 業務類修改,增加注解 @RefreshScope
  2. 發送Post請求刷新 客戶端
curl -X POST "http://localhost:3355/actuator/refresh"

出現的問題:
現在每次更改,都要手動發動請求,這不合適,實現廣播,一次通知,處處修改
下一章到消息總線會講到


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM