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://eureka2.server.com:8702/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://eureka2.server.com:8702/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://eureka2.server.com:8702/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://eureka2.server.com:8702/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小白教程》目錄

轉載請注明出處,

  • 聯系方式:4272231@163.com


免責聲明!

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



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