spring-cloud-config使用、基本配置及拓展

SpringCloudConfig 使用文檔

一、簡介

1.1. 配置中心

配置中心是一種爲方便運維人員統一管理多應用多環境多配置文件的分佈式服務。沒有配置中心的時候需要人工手動逐個修改每個服務的配置,而有配置中心後,在需要修改某配置時,只要一處修改,全局生效。

1.2. SpringCloudConfig

SpringCloudConfig是Spring對分佈式配置中心的一種實現方式,具有高可用性,並支持配置文件的多種存儲方式(如git/svn/本地磁盤/vault等)。

二、SpringCloudConfig依賴引入及其配置

2.1. 服務器端config-server

2.1.1. 引入相關jar包到工程

        <!-- config-server依賴包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- config-server需要註冊到服務註冊與發現中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 如依賴的eureka-client版本較新,則需要引入hystrix否則拋出無法找到類HystrixCommandAspect的異常,具體可觀察spring-cloud-starter-netflix-eureka-client的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency> 

2.1.2. 啓動類上添加@EnableConfigServer

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  • 由於SpringCloud基於SpringBoot,@SpringBootApplication註解是必須的,具體作用不贅述,亦可用@SpringCloudApplication代替。
  • @EnableDiscoveryClient註解是爲了能夠讓註冊中心發現,掃描到本服務,從Spring Cloud Edgware開始,可省略。
  • @EnableConfigServer用來啓動配置中心服務器端的相關功能

2.1.3. 配置文件添加配置

spring-cloud-config-server提供了4種配置,可以通過不同名字來激活:

  1. git:默認值,表示去Git倉庫讀取配置文件;
  2. subversion:表示去SVN倉庫讀取配置文件;
  3. native:表示去本地文件系統讀取配置文件;
  4. vault:表示去Vault(一種資源控制工具)中讀取配置文件;

在基本SpringBoot的配置基礎上添加配置中心的相關配置:

#本服務端口 默認8080 一般都需要重新定義
server:
  port: 8080
#eureka服務註冊 服務發現於註冊端口一般需要用戶自定義
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        bootstrap: true
        #本地存儲文件路徑
        native:
          search-locations: E:\workspace\personal\system-stage\config
        #git存儲配置文件,本地存儲時也需要配置
        git:
          basedir: E:\workspace\personal\system-stage\config
          uri: E:\workspace\personal\system-stage\config
          #也可以爲git服務器地址,如uri: http://localhost:18080/root/temp.git
          #不在根目錄時則需要配置目錄
          search-paths: /temp
          #git用戶信息
          username: root
          password: 12345678

2.1.4. 啓動程序

啓動程序後可以使用Config Server的端點獲取配置文件的內容,端點與配置文件的映射規則如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

{application} 是應用名稱,對應配置文件的名稱部分。

{profile} 是配置文件的版本。

{label} 表示分支,如果是git則默認是master分支。

2.2. 客戶端config-client

2.2.1. 引入相關jar包到工程

        <!-- config-client依賴包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- config-client需要註冊到服務註冊與發現中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.2.2. 配置文件相關配置

在bootstrap配置文件中配置,其他所有配置均寫在application文件中,且由配置中心管理。

spring:
  application:
    name: project-app
  cloud:
    config:
      name: project
      uri:  http://localhost:21000/
      username: user
      password: admin
      label: master
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          auto-add-partitions: true
      bindings:
        springCloudBusInput:
          destination: springCloudBus

2.2.3. @RefreshScope註解

修改配置文件後需要運行時刷新配置的bean需要用@RefreshScope註解進行修飾,內部實際上是註解@Scope("refresh"),主要作用是 將修飾的Spring管理的Bean配置爲refresh的作用域,可在運行時刷新配置,實際刷新邏輯爲銷燬修飾對的bean後重新創建。

三、拓展使用

3.1. 文件commit後自動熱更新應用配置

文件變更且提交後,Jenkins Hook觸發,用Bus事件通過Stream協調消息中間件廣播到每個應用,從而運行時全局修改配置 (目前暫未驗證成功,一直沒有成功發送消息,後續成功後更新本部分)

        <!--接收config-server相關的刷新請求-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
        <!-- bus 通過kafka消息 實時刷新所有應用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
        <!-- bus 通過rabbitMQ消息 以amqp協議形式 實時刷新所有應用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

配置文件需要暴露bus-refresh端點以及配置bus和stream相關的值。

服務器端需要綁定生產消息到哪個destination,具體配置如下::

#暴露所有端點(主要是bus-refresh端點)用於bus的刷新
management:
  endpoints:
    web:
      exposure:
        include: "*"
spring:
  cloud:
    #配置bus的destination
    bus:
      trace:
        enabled: true
      destination: springCloudBus
    #配置stream的綁定關係
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          auto-create-topics: true
          replication-factor: 2
          required-acks: -1
        bindings:
          springCloudBusOutput:
            destination: springCloudBus
            content-type: text/plain

客戶端需要綁定消息監聽的destination,具體配置如下:

spring:
  cloud:
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          auto-add-partitions: true
      bindings:
        springCloudBusInput:
          destination: springCloudBus

3.2. 安全性及相關加密

首先需要引入spring-boot-starter-security的依賴:

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

添加如下配置可以校驗用戶身份驗證:

spring:
  #訪問配置中心的身份驗證 配置後客戶端也需要對應的用戶密碼來訪問
  security:
    user:
      name: user
      password: admin

添加如下配置可以加密需要加密的密鑰,屬性值前使用{cipher}前綴來標註該內容是一個加密值,通過encrypt.key屬性在配置文件中直接指定密鑰信息:

spring:
  cloud:
    config:
      server:
        #加密功能
        encrypt:
          enabled: true
encrypt:
  key: secret

四、注意事項

  1. 配置中心僅支持properties和yml文件的熱加載,不支持xml格式
  2. ...

五、總結

5.1. 服務器配置文件

#本服務端口 默認8080 一般都需要重新定義
server:
  port: 21000
#eureka服務註冊 服務發現於註冊端口一般需要用戶自定義
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        bootstrap: true
        #本地存儲文件路徑
        native:
          search-locations: /config
        #git存儲配置文件,本地存儲時也需要配置
        git:
          basedir: /config
          uri: /config
          #也可以爲git服務器地址,如uri: http://localhost:18080/config-files.git
          #不在根目錄時則需要配置目錄
          search-paths: /config
          #git用戶驗證
          username: root
          password: 12345678
        #加密功能
        encrypt:
          enabled: true
        #覆蓋指定參數
        #overrides: 
    bus:
      trace:
        enabled: true
      destination: springCloudBus
    stream:
      kafka:
        binder:
          brokers: kafkaHost:9092
          auto-create-topics: true
          replication-factor: 2
          required-acks: -1
        bindings:
          springCloudBusOutput:
            destination: springCloudBus
            content-type: text/plain
    #訪問配置中心的身份驗證 配置後客戶端也需要對應的用戶密碼來訪問
    security:
      user:
        name: user
        password: admin
#暴露所有端點(主要是bus-refresh端點)用於bus的刷新
management:
  endpoints:
    web:
      exposure:
        include: "*"

5.2. 客戶端配置文件

spring:
  application:
    name: project-app
  cloud:
    config:
      name: project
      uri:  http://localhost:21000/
      username: user
      password: admin
      label: master
      #開啓所有參數覆蓋
      #override-none: true 
    bus:
      trace:
        enabled: true
    stream:
      kafka:
        binder:
          brokers: kafkaHost:9092
          auto-add-partitions: true
      bindings:
        springCloudBusInput:
          destination: springCloudBus
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章