前言
在這篇文章介紹并記錄如何實現(xiàn)SpringCloud config 配置中心,實現(xiàn)使用github來進行管理配置。
此篇文章SpringCloud快速入門學(xué)習(xí)(一)之服務(wù)注冊與發(fā)現(xiàn)——Netflix Eureka的第二篇
根據(jù)springcloud官方文檔描述
Spring Cloud配置為分布式系統(tǒng)中的外部化配置提供服務(wù)器端和客戶端支持。使用配置服務(wù)器,您可以在中心位置管理所有環(huán)境中應(yīng)用程序的外部屬性??蛻魴C和服務(wù)器上的概念與Spring環(huán)境和PropertySource抽象完全相同,因此它們非常適合Spring應(yīng)用程序,但可以用于任何語言中運行的任何應(yīng)用程序。隨著應(yīng)用程序從開發(fā)人員到測試人員再到生產(chǎn)人員的部署管道中移動,您可以管理這些環(huán)境之間的配置,并確保應(yīng)用程序在遷移時擁有運行所需的一切。服務(wù)器存儲后端的默認(rèn)實現(xiàn)使用git,因此它很容易支持有標(biāo)簽的配置環(huán)境版本,并且可以使用各種工具來管理內(nèi)容。添加替代實現(xiàn)并用Spring配置將其插入是很容易的。
講了一大堆,確實有點抽象,有點看不懂。直接上手好了。
統(tǒng)一配置中心 config
記住上面第一句話就好了
Spring Cloud配置為分布式系統(tǒng)中的外部化配置提供服務(wù)器端和客戶端支持。使用配置服務(wù)器,您可以在中心位置管理所有環(huán)境中應(yīng)用程序的外部屬性。
配置統(tǒng)一管理的好處是在日后大規(guī)模集群部署服務(wù)應(yīng)用時相同的服務(wù)配置一致,
日后再修改配置只需要統(tǒng)一修改全部同步,不需要一個一個服務(wù)手動維護

配置中心的客戶端和服務(wù)端
@EnableConfigServer 注解 以及依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
作為服務(wù)端配置中心
添加此依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
作為客戶端配置中心拉取服務(wù)端的配置
下面會用代碼講解
一、配置中心實現(xiàn)
在原來上篇的項目里在創(chuàng)建子工程命名為mi-config
1.1 子工程 mi-config包目錄

1.2 添加pom 依賴
<?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">
<parent>
<artifactId>Spring-Coud-Example</artifactId>
<groupId>com.mi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mi</groupId>
<artifactId>mi-config</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- 引入 Web 功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
Eureka 客戶端, 客戶端向 Eureka Server 注冊的時候會提供一系列的元數(shù)據(jù)信息, 例如: 主機, 端口, 健康檢查url等
Eureka Server 接受每個客戶端發(fā)送的心跳信息, 如果在某個配置的超時時間內(nèi)未接收到心跳信息, 實例會被從注冊列表中移除
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--WebHooks需要的依賴包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<!--amqp bus 總線 為了實現(xiàn)動態(tài)刷新-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 監(jiān)控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
1.3 application.yml
server:
port: 8090
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/mi499938150/config-repo.git # github的地址
username: #你的賬號
password: #你的密碼
basedir: D:\Program Files\config\basefir #存放的目錄
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka/
management:
endpoints:
web:
exposure:
include: "*"
1.4 ConfigApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
二、github 創(chuàng)建 config Repositories
訪問github 官方地址 https://github.com/
2.1 創(chuàng)建 config-repo Repositories

2.1.1 接著輸入名字點擊Create

2.1.2 在點擊進去創(chuàng)建File
創(chuàng)建appoint-dev.yml

2.1.3 貼上配置代碼

2.1.4 開啟Application啟動類
第一步開啟:快速入門學(xué)習(xí)(一)里eurekaApplication
第二步開啟:本篇文寫的ConfigApplication
訪問localhost:8090/appoint-dev.yml
然后會從github上拉取到本地地址,從日志就可以看出

2 .1.5 訪問地址
訪問localhost:8090/appoint-dev.yml

2.1.6對于Config名命介紹
/{name}-{profiles}.yml 例如 :appoint-dev.yml
1.name 服務(wù)器 =>就是yml里面的applicatoin:name
2.profiles 環(huán)境 =>就是dev,test,
/{label}/{name}-{profiles}.yml
1.label 是創(chuàng)建分支目錄,把配置文件放在里面
2和上同上
http://localhost:8080/release/order-dev.yml 例如:創(chuàng)建了release
3 2 1
三、Spring Cloud Bus 實現(xiàn)動態(tài)刷新
在這介紹一下如何使用Spring Cloud Bus實現(xiàn)自動刷新配置,Bus在這里是總線的意思。

Spring Cloud Bus會向外提供一個http接口,即圖中的/actuator/bus-refresh。我們將這個接口配置到遠(yuǎn)程的git上,當(dāng)git上的文件內(nèi)容發(fā)生變動時,就會自動調(diào)用/bus-refresh接口。Bus就會通知config-server,config-server會發(fā)布更新消息到消息隊列中,其他服務(wù)訂閱到該消息就會信息刷新,從而實現(xiàn)整個微服務(wù)進行自動刷新。
3.1 RabbitMQ
在這里采用的是Win10,會有多個版本,選擇win版下載后安裝
在官網(wǎng)上下載:地址鏈接為:https://www.rabbitmq.com/download.html

3.1.1 開啟RabbitMQ
安裝后啟用web管理UI,進入RabbitMQ Server\rabbitmq_server-3.6.6\sbin,輸入命令rabbitmq-plugins enable rabbitmq_management

3.1.2 訪問RabbitMQ
http://localhost:15672
登錄賬號:guest,密碼:guest

3.1.3 重新啟動configApplication
rabbitMQ 控制臺會出現(xiàn)總線隊列 有連接config地址的項目啟動以此類推

3.2 集成Github WebHooks實現(xiàn)動態(tài)更新
3.2.1 申請一個免費內(nèi)網(wǎng)穿透隧道
https://natapp.cn/ 進行申請
由于是申請的是免費,每重啟一次都會刷一下地址

3.2.2 在安裝后目錄下開啟穿透隧道
打開cmd 輸入 natapp -authtoken=c33cc0697bfb76a6

開啟成功

3.2.3 進入github之前創(chuàng)建目錄config-repo的倉庫管理后選擇WebHook
輸入地址穿透王地址/monitor

4.開始測試
4.1 開始訪問沒有修改的過的
http://localhost:8090/appoint-dev.yml

4.2 appoint-dev.yml 進行修改后配置后提交
從日志看到已經(jīng)再刷新從github拉取到本地

接著訪問
http://localhost:8090/appoint-dev.yml
更新成功

github項目地址:https://github.com/mi499938150/SpringCloud-Example.git