一、簡介
基於Spring Cloud Config實現統一配置中心,將配置文件存放於Git(GitHub)上,通過Spring Cloud Bus消息總線&RabbitMQ消息中間件進行服務間消息通信。
涉及項目
- exureka-server
- config-server
- config-client
整體架構圖大致如下,使用GitHub Webhooks 觸發配置中心刷新配置
上圖來源:http://blog.didispace.com/springcloud7/
二、配置中心服務端實現
依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lxt</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.lxt</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--集成Git Webhooks之後,使用/monitor即可實現配置更新,通知其他服務-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
配置文件
server:
port: 8080
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/hdlxt/springcloud # 配置git倉庫的地址
search-paths: config-repo # git倉庫地址下的相對地址,可以配置多個,用,分割。
username: # git倉庫的賬號
password: # git倉庫的密碼
rabbitmq:
host: 111.231.xxxx.xx
port: 5672
username: guest
password: guest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ #註冊中心eurka地址
management:
endpoints:
web:
exposure:
# 2.x手動開啓 這個是用來暴露 endpoints 的。由於 endpoints 中會包含很多敏感信息,除了 health 和 info 兩個支持 web 訪問外,其他的默認不支持 web 訪問
include: bus-refresh
- 主要內容
- 添加配置中心文件存放於github
- 配置rabbitmq連接信息
- 暴露bus-refresh
- github配置文件內容
- 倉庫中的配置文件會被轉換成 Web 接口,訪問可以參照以下的規則:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
- 上面的 URL 會映射 {application}-{profile}.yml 對應的配置文件,其中 {label} 對應 Git 上不同的分支,默認爲 master。
啓動類比較簡單,聲明是服務、配置中心服務端
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
到此配置中心服務端完畢。
二、配置中心客戶端實現
- 依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.lxt</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.lxt</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies> </project>
- 配置文件
- 關於
bootstrap.yml
和application.yml
- bootstrap.yml(bootstrap.properties) 用來程序引導時執行,應用於更加早期配置信息讀取,如可以使用來配置application.yml中使用到參數等
- application.yml(application.properties) 應用程序特有配置信息,可以用來配置後續各個模塊中需使用的公共參數等。
- bootstrap.yml 先於 application.yml 加載
- bootstrap.yml
server: port: 8003 spring: cloud: config: name: lxt-config profile: dev label: master discovery: #開啓Config服務發現支持 enabled: true #指定server端的name,也就是server端spring.application.name的值 service-id: spring-cloud-config-server eureka: client: service-url: defaultZone: http://localhost:8000/eureka/
- application.yml
spring: application: name: spring-cloud-config-client cloud: bus: trace: # 開啓消息跟蹤事件 enabled: true rabbitmq: host: 111.231.29.249 port: 5672 username: guest password: guest management: endpoints: web: exposure: include: refresh
- 關於
- 主要代碼
- 核心
- @EnableDiscoveryClient
- @RefreshScope:使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中
- 啓動類
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
- 測試
/** * @author lxt * @Copy Right Information: lxt * @Project: spring cloud * @CreateDate: 2018/12/16 20:04 * @history Sr Date Modified By Why & What is modified * 1.2018/12/16 lxt & new */ @RestController @RefreshScope // 使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中 public class HelloController { @Value("${lxt.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
- 核心
三、整合
以安裝包或者docker方式安裝RabbitMQ,啓動
配置GitHub Webhooks
- (可選,雲服務器可跳過)內網穿透工具下載natapp安裝和配置(有免費通道),目的可給本機電腦映射一個外網域名,用於配置Webhooks
- 配置Webhooks,配置中心服務端需引入
spring-cloud-config-monitor
依賴
四、測試
- 分別啓動註冊中心、配置中心服務端
- 啓動多個客戶端
- 可通過打jar包形式啓動
- 也可通過idea添加啓動類,指定不同端口啓動
- 修改配置文件,push到github上觸發回調
http://t23b4j.natappfree.cc/monitor
,通知配置中心服務端更新配置,服務端以mq形式通過消息總線通知客戶端更新配置。