1. 程式人生 > 實用技巧 >SpringCloud學習筆記【十二】SpringCloud Config服務配置

SpringCloud學習筆記【十二】SpringCloud Config服務配置

目錄

本篇要點

  • 介紹Spring Cloud Config是什麼,能夠解決分散式系統中的什麼問題。
  • 介紹Spring Cloud Config服務端配合Git配置,以及客戶端的配置。
  • 介紹客戶端手動動態重新整理的方法。

分散式系統面臨的問題

微服務意味著要將單體應用中的業務拆分成一個個子服務, 每個服務的粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置資訊才能執行,所以一套集中式的、動態的配置管理設施是必不可少的。

SpringCloud提供了ConfigServer來解決這個問題,我們每一個微服務自己帶著一個application.yml

, 上百個配置檔案的管理

Spring Cloud Config是什麼

https://spring.io/projects/spring-cloud-config

SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支援,配置伺服器為各個不同微服務應用的所有環境提供了一箇中心化的的外部配置

  • SpringCloud Config分為服務端和客戶端兩部分。
  • 服務端也稱為分散式配置中心,它是一個獨立的微服務應用, 用來連線配置伺服器併為客戶端提供獲取配置資訊,加密/解密資訊等訪問介面
  • 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啟動的時候從配置中心獲取和載入配置資訊配置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容

SpringCloud Config的作用

  • 集中管理配置檔案
  • 不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release
  • 執行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置檔案,服務會向配置中心統一拉取配置自己的資訊當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置
  • 將配置資訊以REST介面的形式暴露

SpringCloud Config整合Git搭建配置總控中心

在Gitee上新建倉庫,並上傳檔案

https://gitee.com/tqbx/spring-cloud-config

上傳檔案:config-dev.yml

config:
  info: "master branch,springcloud-config/config-dev.yml version=7" 

新建工程,引入依賴

新建:cloud-config-center3344,引入spring-cloud-config-server依賴。

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

配置yml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #註冊進Eureka伺服器的微服務名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/tqbx/spring-cloud-config.git #Gitee上面的git倉庫名字
          ####搜尋目錄
          search-paths:
            - SpringCloud-Config
          force-pull: true
          username: [your username]
          password: [your password]
      ####讀取分支
      label: master
#服務註冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

報錯!Auth fail

如果你按照視訊中的配置出現了Auth fail的異常,你可以嘗試以下操作解決:

  1. uri不使用SSL【需要認證】,而是用HTTPS。
  2. 配置force-pull=true。
  3. 配置username和password,可以通過以下命令檢視:
$ git config user.name
$ git config user.email

主啟動類加上註解

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

測試訪問

訪問:http://localhost:3344/master/config-dev.yml,成功獲取配置資料。

配置讀取規則

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml # 推薦使用 
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Spring Cloud Config客戶端配置與測試

新建工程,引入依賴

新建:cloud-config-client3355模組,引入依賴spring-cloud-starter-config

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

使用bootstrap.yml作為配置檔案

bootstrap.yml與application.yml的區別

  • application.yml是使用者級別的資源配置項。
  • bootstrap.yml是系統級別的資源配置,優先順序更高。

boostrap.yml機制

Spring Cloud會建立一個BootstrapContext,作為Spring應用的ApplicationContext的父上下文。初始化的時候,BootstrapContext負責從外部源載入配置屬性並解析配置。這兩個上下文共享一個從外部獲取的Environment。

Bootstrap屬性有高優先順序,預設情況下,他們不會被本地配置覆蓋。BootstrapContext和ApplicationContext有著不同的約定,所以新增了bootstrap.yml檔案,保證他們的配置分離。

如何使用?

server:
  port: 3355

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

#服務註冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

config:master分支上config-dev.yml的配置檔案被讀取http://config-3344.com:3344/master/config-dev.yml

建立主啟動類

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

建立Restful介面

@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

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

啟動測試

依次啟動7001Eureka註冊中心,3344服務端配置中心,和3355客戶端配置中心。

依次訪問:

  • http://localhost:7001/
  • http://localhost:3344/master/config-dev.yml
  • http://localhost:3355/configInfo

客戶端3355成功實現訪問服務端3344從Gitee上獲取配置config-info資訊。

客戶端手動動態重新整理

之前的測試都已成功,但是存在一個問題:當Gitee上的配置檔案發生改變,服務端3344確實能夠及時隨之改變,但是客戶端卻無法動態更新配置,除非重啟!!!

那如何實現客戶端動態重新整理呢?以下配置修改3355模組,配置手動重新整理的方式。

引入Actuator依賴

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

配置yml,暴露監控端點

# 暴露監控端點
management:
  endpoints:
    web:
      exposure:
        include: "*"

在業務類上加上@RefreshScope

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

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

@RefreshScope註解讓其能夠獲悉外部配置的改變,但還需要下一步。

傳送POST請求

$ curl -X POST "http://localhost:3355/actuator/refresh"

["config.client.version","config.info"]

此時,手動重新整理配置的方式已經配置完成,且測試成功。

手動重新整理雖然已經能夠實現我們的需求,但如果服務眾多,每個服務都需要執行一次post請求,未免過於麻煩,那是否有一種一次重新整理,處處通知的策略呢?有的,可以利用訊息匯流排,下一篇學習訊息匯流排Bus。

原始碼下載

本系列文章為《尚矽谷SpringCloud教程》的學習筆記【版本稍微有些不同,後續遇到bug再做相關說明】,主要做一個長期的記錄,為以後學習的同學提供示例,程式碼同步更新到Gitee:https://gitee.com/tqbx/spring-cloud-learning,並且以標籤的形式詳細區分每個步驟,這個系列文章也會同步更新。