天天看點

SpringCloud微服務(06):Config元件,實作配置統一管理

本文源碼:GitHub·點這裡 || GitEE·點這裡

一、Config簡介

在微服務系統中,服務較多,相同的配置:如資料庫資訊、緩存、參數等,會出現在不同的服務上,如果一個配置發生變化,需要修改很多的服務配置。spring cloud提供配置中心,來解決這個場景問題。

系統中的通用配置存儲在相同的位址:GitHub,Gitee,本地配置服務等,然後配置中心讀取配置以restful釋出出來,其它服務可以調用接口擷取配置資訊。

二、配置服務端

1、項目結構

SpringCloud微服務(06):Config元件,實作配置統一管理
  • 核心注解:@EnableConfigServer

2、核心依賴

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

3、核心配置檔案

這裡注意讀取檔案的配置

  • active :native,讀取本地配置;
  • active :git,讀網絡倉庫配置;
server:
  port: 9001
spring:
  application:
    name: config-server-9001
  profiles:
    # 讀取本地
    # active: native
    # 讀取Git
    active: git
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config
        git:
          # 讀取的倉庫位址
          uri: https://gitee.com/cicadasmile/spring-cloud-config.git
          # 讀取倉庫指定檔案夾下
          search-paths: /cloudbaseconfig
          # 非公開需要的登入賬号
          username:
          password:
      label: master
           

4、讀取配置内容

不同的環境讀取的結果不同。

info:
  date: 20190814
  author: cicada
  sign: develop
  version: V1.0
           

三、配置用戶端

1、核心依賴

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

2、核心配置檔案

在上面的配置中心,配置讀取Git資源,是以這裡的配置也就是讀取Git資源。

server:
  port: 8001
spring:
  application:
    name: config-client-8001
  profiles:
    active: dev
  cloud:
    config:
      # 讀取本地配置 ---------------------------
      #uri: http://localhost:9001
      ## 讀取政策:快速失敗
      #fail-fast: true
      ## 讀取的檔案名:無字尾
      #name: client-8001
      ## 讀取的配置環境
      #profile: dev  # client-8001-dev.yml
      # ----------------------------------------

      # github上的資源名稱 -----------------------
      name: client-8001
      # 讀取的配置環境
      profile: dev
      label: master
      # 本微服務啟動後,通過配置中心6001服務,擷取GitHub的配置檔案
      uri: http://localhost:9001
      # ----------------------------------------
           

3、測試接口

@RestController
public class ClientController {
    @Value("${info.date}")
    private String date ;
    @Value("${info.author}")
    private String author ;
    @Value("${info.sign}")
    private String sign ;
    @Value("${info.version}")
    private String version ;
    /**
     * 擷取配置資訊
     */
    @RequestMapping("/getConfigInfo")
    public String getConfigInfo (){
        return date+"-"+author+"-"+sign+"-"+version ;
    }
}
           

四、基于Eureka配置

上面的模式,通過服務中心,直接擷取配置。下面把注冊中心Eureka加進來。

啟動順序也是如下:

node06-eureka-7001
config-server-9001
config-client-8001
           

2、修改配置項

  • 将config-server-9001添加到注冊中心;
  • 配置config-client-8001讀取注冊中心;

完成後Eureka注冊中心效果圖,啟動順序如下:

SpringCloud微服務(06):Config元件,實作配置統一管理

3、修改用戶端配置

通過注冊中心擷取服務,避免使用URI位址。

SpringCloud微服務(06):Config元件,實作配置統一管理

經過測試後,正确無誤。

  • 提醒:國内如果讀取git的配置,可能經常出去無法加載的問題,該案例使用的是Gitee的位址。

五、源代碼位址

GitHub·位址
https://github.com/cicadasmile/spring-cloud-base
GitEE·位址
https://gitee.com/cicadasmile/spring-cloud-base
           
SpringCloud微服務(06):Config元件,實作配置統一管理

繼續閱讀