Spring Cloud Config 使用總結


Spring Cloud Config 使用總結

源碼 https://github.com/ChangMuChen/Spring-Boot/tree/master/studies/sourcecode/spring-cloud-config

一、介紹

Spring Cloud Config

Spring Cloud Config為分布式系統中的外部化配置提供服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。客戶端和服務器上的概念映射與Spring Environment和PropertySource抽象,因此它們非常適合Spring應用程序,但可以與任何語言運行的任何應用程序一起使用。當應用程序通過部署管道從開發到測試並進入生產時,您可以管理這些環境之間的配置,並確保應用程序具有遷移時需要運行的所有內容。服務器存儲后端的默認實現使用git,因此它可以輕松支持配置環境的標簽版本,以及可用於管理內容的各種工具。添加替代實現並使用Spring配置插入它們很容易。

Spring Cloud Config Server功能

  • 用於外部配置的HTTP,基於資源的API(名稱 值對或等效的YAML內容)
  • 加密和解密屬性值(對稱或非對稱)
  • 使用可輕松嵌入Spring Boot應用程序
  • 可以輕松的結合Eureka實現高可用
  • 可以輕松的結合Spring Cloud Bus實現自動化持續集成

Config Client功能(適用於Spring應用程序)

  • 綁定到Config Server並Environment使用遠程屬性源初始化Spring
  • 加密和解密屬性值(對稱或非對稱)
  • 結合Eureka實現服務發現

二、基礎使用

1. 搭建服務端

Step1. 新建Spring Boot項目 configserver

Step2. 引入主要依賴 spring-cloud-config-server

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

Step3. 引入 spring-boot-starter-web

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

Step4. 主程序添加注釋 @EnableConfigServer

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

Step5. 設置屬性

這里使用 .properties 的方式進行配置。你可以自行更換為同等效果的 .yml 方式。配置信息需要放在 bootstrap.properties 中。

1)Git方式存儲配置文件示例

#配置文件存放在Git的情況
spring.cloud.config.server.git.uri=http://172.16.50.98:9999/changdaohang/demo_configs.git
spring.cloud.config.label=master
spring.cloud.config.server.git.search-paths=/**
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

2)本地存儲配置文件

#配置文件存放在本地的情況
#注意:文件夾要有訪問權限
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=C:/IdeaProjets/demo_configs/

Step6. 啟動

備注

我們可以直接通過restful api 的方式進行訪問配置。路由如下:

#映射說明如下
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties

例如:http://localhost:8080/testapp/devhttp://localhost:8080/testapp-dev.properties
對應的配置為 /testapp-dev.properties
當你啟動服務端,你可以在Mappings中看到接口開放情況,部分如下:

接口開放情況

2. 業務服務配置

Step1. 新建Spring Boot項目 servera

Step2. 引入主要依賴 spring-cloud-starter-config

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

Step3. 引入 spring-boot-starter-web

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

Step4. 主程序添加注釋 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class ServeraApplication {

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

}

Step5. 創建配置文件

以Git為例:
在git項目中創建servera-dev.properties文件,寫入以下示例內容:

name=David

Step6. 配置參數

這里使用 .properties 的方式進行配置。你可以自行更換為同等效果的 .yml 方式。配置信息需要放在 bootstrap.properties 中。

spring.application.name=servera
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8071/

注意:配置中 servera值與 dev 結合對應到 servera-dev.properties文件,並且使用 master 對應到Git中的 master 分支。http://localhost:8080/ 為 配置中心服務地址。

Step7. 新建測試api

新建 TestController 控制器,代碼如下:

@RestController
@RequestMapping("api")
@RefreshScope
public class TestController {
   
    @Value("${name}")
    private String confignameValue;

    @GetMapping("/confignamevalue")
    @ResponseBody
    public String returnConfignameValue(){
        return confignameValue;
    }
}

說明:其中 @RefreshScope 注解是以后為了自動更新配置用的。這里可以不加。
通過 @Value("${name}") 注解,可以對應到 servera-dev.properties 中的 name=David 鍵值對,並將值 David 賦給 confignameValue

Step8. 啟動程序

訪問 http://localhost:8081/api/confignamevalue 顯示結果為:David

到這里最基本的使用就完成了

三、自動更新

這里介紹使用 Monitor + Actuator + Bus + RabbitMQ + GitLab Webhooks 來實現當提交配置后,通知服務更新配置。

請自行搭建 RabbitMQ 服務環境。

1. 配置中心服務端

Step1. 在pom.xml中添加依賴:

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

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

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

其中 spring-boot-starter-actuator 整合了一些 /bus-fresh 的接口,可以用於自主觸發刷新服務配置。
spring-cloud-config-monitor 用於對接 gitlab 的webhooks,用於觸發刷新服務配置。
spring-cloud-starter-bus-amqp用於對接rabbitMQ來廣播服務更新事件,通知相關的服務進行配置更新。

Step2.配置參數

bootstrap.properties 中加入以下配置:

#配置消息中心-rabbitmq

spring.rabbitmq.host=192.168.1.119
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password

#開放actuator下的所有功能api(你也可以選擇性開放)
management.endpoints.web.exposure.include=*

2. 配置客戶端

Step1. 在pom.xml中添加依賴:

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

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

其中 spring-boot-starter-actuator 配合服務端,處理服務端關播發來的配置更新事件。也可以用於自主觸發刷新服務配置。
spring-cloud-starter-bus-amqp用於接收rabbitMQ消息,通知相關的服務進行配置更新。

Step2.配置參數

bootstrap.properties 中加入以下配置:

#配置消息中心-rabbitmq

spring.rabbitmq.host=192.168.1.119
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
#修復BUG-github webhook 只能刷新config server 無法刷新config client的問題
spring.cloud.bus.id=${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}


#開放actuator下的所有功能api(你也可以選擇性開放)
management.endpoints.web.exposure.include=*

Step3. 在控制器中添加 @RefreshScope 注解,用於實時刷新

@RestController
@RequestMapping("api")
@RefreshScope
public class TestController {
	...
}

3. GitLab Webhooks配置

Step1. 打開GitLab項目主頁,依次進入【設置】-【集成】

Step2. 添加WebHook.設置調用時機和回調地址:http://192.168.1.28:8080/monitor?path=*

Step3. 保存

path用於定位哪些服務應該更新配置。這里使用 * 來通知所有服務。

4. 測試

Step1. 依次啟動 configserverservera

Step2. 訪問 http://localhost:8081/api/confignamevalue 顯示結果為:David

Step3. 修改gitlab 中的 servera-dev.propertiesname=David Mu 並Commit

Step4. 訪問 http://localhost:8081/api/confignamevalue 顯示結果為:David Mu

這種方式服務有可能不會立馬刷新配置,屬於正常情況。

四、高可用

這里介紹使用 Config + Eureka 來實現高可用。

請自行搭建 Eureka 服務環境。

1. 服務端配置

Step1. 引入 spring-cloud-starter-netflix-eureka-client

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Step2. 配置Eureka相關參數

#設置程序基礎配置
server.port=8080
spring.application.name=configserver

#eureka 配置
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
eureka.client.service-url.defaultZone=http://127.0.0.1:8061/eureka/,http://127.0.0.1:8062/eureka/,http://127.0.0.1:8063/eureka/

Step3. 添加注解 @EnableEurekaClient

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

啟動程序,在eureka注冊中就可以看到服務已注冊。

2. 客戶端配置

Step1. 引入 spring-cloud-starter-netflix-eureka-client

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Step2. 配置Eureka相關參數

#設置程序基礎配置
server.port=8080
spring.application.name=configserver

#eureka 配置
eureka.instance.hostname=127.0.0.1
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${eureka.instance.hostname}:${server.port}
eureka.client.service-url.defaultZone=http://127.0.0.1:8061/eureka/,http://127.0.0.1:8062/eureka/,http://127.0.0.1:8063/eureka/

Step3. 修改原來的參數,如下:

spring.cloud.config.label=master
spring.cloud.config.profile=dev
#啟用服務發現
spring.cloud.config.discovery.enabled=true
#指定配置服務id
spring.cloud.config.discovery.service-id=CONFIGSERVER
#撤銷原來的指向方式
#spring.cloud.config.uri=http://localhost:8071/


免責聲明!

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



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