1. 程式人生 > 程式設計 >spring cloud 2.x版本 Config配置中心教程

spring cloud 2.x版本 Config配置中心教程

前言

本文采用Spring cloud本文為2.1.8RELEASE,version=Greenwich.SR3

本文基於前面的文章eureka-server的實現。 參考

概述

在分散式系統中,由於服務數量巨多,為了方便服務配置檔案統一管理,所以需要分散式配置中心元件。Spring Cloud Config為分散式系統中的外部化配置提供伺服器和客戶端支援。

本篇涉及專案的結構為一個Config Server單機模式和適用於連結Git倉庫,一個Config Client通過server來展示配置檔案資料。 同時使用兩個bus來模擬配置檔案重新整理。

建立config-server工程

Config-server功能

  • 用於外部配置的http,基於資源的api
  • 加密和解密屬性
  • 使用可嵌入spring boot的應用程式

1.1 建立配置中心server:config-server

1.2 新增config-server的pom.xml相關依賴

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency
>
<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 複製程式碼

1.3 新增config-server的application新增配置資訊

#載入本地檔案配置
spring:
  application:
    name: config-server
  profiles:
    active: native #載入本地配置
cloud: config: server: native: #載入本地目錄檔案 search-locations: /Users/xxxx/config-server server: port: 13081 eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/ 複製程式碼

1.4 啟動類ConfigServerApplication增加註解

package spring.cloud.demo.configserver;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}
複製程式碼

至此,一個單機本地config-server就搭建完成

1.5 建立配置中心client:config-client

1.6 新增config-client的pom相關依賴

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
複製程式碼

1.7新增config-client的bootstrap.yml相關配置(注意這裡不是application.yml)

bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      uri: http://localhost:13081 #通過域名訪問配置中心服務端
      discovery:
        enabled: true
eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka3.server.com:8703/eureka/
複製程式碼

1.8新建從服務端請求的配置檔案config-client-dev.yml

客戶端從服務端獲取資源配置的路徑規則:

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

本文采用第二種規則。

配置內容:

spring:
  application:
    name: config-client

server:
  port: 52601

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka3.server.com:8703/eureka/

info: local-config-client-dev
複製程式碼

將新建的config-client-dev.yml放到config-server配置的/Users/xxxx/config-server目錄下,如果config-server沒有配置目錄,預設使用resources目錄下。

1.9 ConfigClientApplication增加註解

package spring.cloud.demo.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

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

}

複製程式碼

2.0 建立測試類

在config-client建立測試Controller:ConfigClientController

package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試是否能獲取到配置資訊的controller
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@RestController
public class ConfigClientController {

    @Value("${info}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}
複製程式碼

2.1 啟動相關服務

分別啟動eureka-server、config-server、config-client。訪問:http://localhost:52601/config/info,顯示如下

證明本地配置檔案已經生效。

2.2 從遠端git載入資源配置檔案

修改config-server的application.yml配置檔案:

##載入本地檔案配置
#spring:
#  application:
#    name: config-server
#  profiles:
#    active: native #載入本地配置
#  cloud:
#    config:
#      server:
#        native: #載入本地目錄檔案
#          search-locations: /Users/fengfujie/config-server

#載入遠端git倉庫資原始檔
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # 配置git倉庫的地址
          uri: https://github.com/fengfujie25/sping-cloud-config
          # git倉庫的賬號
          username: xxxxxx
          # git倉庫的密碼
          password: xxxxxx

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka3.server.com:8703/eureka/
複製程式碼

重新啟動config-server服務。然後重新整理http://localhost:52601/config/info地址,顯示如下

證明已經成功獲取了遠端git資源的配置資訊。

2.3 通過服務名稱訪問config-server

以上配置都是通過域名訪問的config-server,為了保證系統的高可用,因為生產環境的配置服務中心都是叢集配置,所有客戶端才通過服務名稱來訪問。

修改config-client的bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      #uri: http://localhost:13081 #通過域名訪問配置中心服務端
      discovery:
        enabled: true
        service-id: config-server #通過服務訪問配置中心服務端

eureka:
  instance:
    hostname: eureka1.client.com

    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka3.server.com:8703/eureka/
複製程式碼

spring.cloud.config.discovery.service-id:通過服務名稱訪問配置中心服務端

重新啟動config-client.並訪問http://localhost:5301/config/info,顯示結果同【2.2】則代表配置資訊已生效,證明是通過服務名稱訪問的config-server.

至此,spring cloud整合config的配置就全部完成。但是存在一個問題,如果修改遠端git倉庫的資源配置,專案並不會重新整理,所以配置資訊是不生效的。

2.4 動態重新整理config-server配置

動態重新整理config-serve配置方式

  • 基於RabbitMQ動態重新整理
  • 原生重新整理(偽動態重新整理)

本文采用比較簡單的原生重新整理方式。

2.4.1增加相關pom.xml依賴
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
複製程式碼
2.4.2 在ConfigClientController增加@RefreshScope註解
package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info:error}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}
複製程式碼

此方式同時還要修改@Value註解內容為@Value("${info:error}"),因為重新整理的時候需要配置資訊有預設值,否則會報錯。

2.4.3 重新啟動config-client

訪問http://localhost:5301/config/info,看服務是否可以正常訪問。

然後可以修改git資源倉庫中的配置資訊。

  • 重新整理http://localhost:5301/config/info,結果顯示:

證明refresh已經生效。

此方式每次都需要手動重新整理一下才行,比較麻煩。GitHub提供一種Webhooks方法可以實現不用每次手動重新整理。

Payload URL: 觸發後回撥的URL

Content type: 資料格式,兩種一般使用json

Secret: 用作給POST的Body加密的字串,採用HMAC演演算法

Events: 觸發的事件列表

事件型別 描述
Just the push event 倉庫有push的時候觸發,預設事件
Send me everything 派我來一切
Let me select individual events 選擇個別事件

這樣我們就可以利用Webhook的機制去觸發客戶端的更新,但是當客戶端越來越多的時候,Webhook機制也不夠優雅,每次增加客戶端都需要改動Webhook也不現實。

其實,Spring cloud給了我們更好的解決方案-spring cloud bus。

spring cloud bus後續更新。

總結

本文簡單的實現了config-server和config-client的單機和遠端git倉庫的配置的呼叫以及配置資訊的簡單的動態更新。

程式碼地址


《Srping Cloud 2.X小白教程》目錄

轉載請註明出處,