1.前言
分布式微服務想要熱更新配置文件,還需要 消息中間件 配合使用 ,一般使用 rabbitMQ 或 Kafka ,這里不解釋 。
這篇隨筆 只講解 底層的 單機熱更新配置文件
2.環境
spring boot : 2.1.6.RELEASE
spring cloud : Greenwich.SR2
jdk 1.8

3.配置中心服務端 ,端口6001
目錄結構

pom.xml
# 設置本機服務名稱 spring.application.name=config-server-6001 # 設置本機服務端口 server.port=6001 #注冊中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/ # # ##本地配置文件,默認獲取在resources路徑下的文件 #spring.profiles.active=native #指定本地文件路徑 #spring.cloud.config.server.native.search-locations=classpath:properties/ 或者寫 D:/common/ 都是文件夾路徑,只獲取改文件夾內的文件 # # #配置的Git倉庫的地址 spring.cloud.config.server.git.uri=https://github.com/cen-xi/test #分支 spring.cloud.config.label=master #git倉庫地址下的相對地址 多個用逗號","分割。 spring.cloud.config.server.git.search-paths=/blob/master/gittest.properties,/blob/master/README.md,/blob/master/gittest2.yml #git倉庫的賬戶 spring.cloud.config.server.git.username= #git倉庫的密碼 spring.cloud.config.server.git.password=
application.properties
# 設置本機服務名稱 spring.application.name=config-server-6001 # 設置本機服務端口 server.port=6001 #注冊中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/ # # ##本地配置文件,默認獲取在resources路徑下的文件 #spring.profiles.active=native #指定本地文件路徑 #spring.cloud.config.server.native.search-locations=classpath:properties/ 或者寫 D:/common/ 都是文件夾路徑,只獲取改文件夾內的文件 # # #配置的Git倉庫的地址 spring.cloud.config.server.git.uri=https://github.com/cen-xi/test #分支 spring.cloud.config.label=master #git倉庫地址下的相對地址 多個用逗號","分割。 spring.cloud.config.server.git.search-paths=/blob/master/gittest.properties,/blob/master/README.md,/blob/master/gittest2.yml #git倉庫的賬戶 spring.cloud.config.server.git.username=cen-xi #git倉庫的密碼 spring.cloud.config.server.git.password=c853396015 #http://localhost:100/master/gittest-1.properties #http://localhost:100/master/README-1.md #http://localhost:100/master/gittest2-1.yml # ## spring cloud bus 刷新配置 #spring.rabbitmq.host=localhost ##127.0.0.1 ##默認端口 #spring.rabbitmq.port=5672 ##spring.rabbitmq.password=guest ##spring.rabbitmq.username=guest #spring.rabbitmq.password= #spring.rabbitmq.username= ## #spring.cloud.bus.enabled=true #spring.cloud.bus.trace.enabled=true ###消息總線設置 , , , ,開啟rabbit mq 暴露的服務端口 ,用於 執行 bus ,更新接口 ##management.endpoints.web.exposure.include=bus-refresh #management.endpoints.web.exposure.include="*" #management.endpoints.bus-refresh.enabled=true
啟動類
package com.example.configserver6001; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication //開啟發現服務 ,,也可以使用 EnableEurekaClient @EnableDiscoveryClient //開啟配置中心服務端 @EnableConfigServer public class ConfigServer6001Application { public static void main(String[] args) { SpringApplication.run(ConfigServer6001Application.class, args); } }
4.獲取遠程配置的客戶端 ,端口6080
目錄結構

pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>cen.cloud</groupId> <artifactId>cen-mycloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>config-client-6080</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client-6080</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--eureka 注冊中心依賴包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--配置中心-客戶端依賴包--> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
新建文件 bootstrap.properties 【 優先級 bootstrap.properties > application.properties > xxx.yml 】
#心得: #需要提前知道遠程配置中心的服務名稱 和 遠程配置文件名 # # # #工程名/項目名/應用名/服務名 spring.application.name=config-client-6080 #端口號 server.port=6080 #獲取指定配置文件名稱 ,多個則以英文符號 , 隔開,不可有空格 spring.cloud.config.name=gittest # ,configText ,gittest2 #經過測試發現,在不同文件【可能是properties或者yml】,如果其中有相同的字段,那么左邊的文件的這個字段的值會被右邊文件的覆蓋,不區分properties和yml優先級 # # #獲取配置信息,客戶端不需要管這個文件是從git來的還是在服務端的本地文件 # #獲取配置的策略 , 讀取文件:dev開發環境、test測試、pro生產 spring.cloud.config.profile=dev #spring.cloud.config.profile.active=dev #獲取配置文件的分支,默認是master。如果是是本地獲取的話,則無用, spring.cloud.config.label=master #開啟配置信息發現 spring.cloud.config.discovery.enabled=true # #指定配置中心的service-id,便於擴展為高可用配置集群,不區分大小寫 spring.cloud.config.discovery.serviceId=config-server-6001 #使用這個寫法也一樣,至於為啥有兩種寫法,還不清除 #spring.cloud.config.discovery.service-id=config-service # #是否啟動快速失敗功能,功能開啟則優先判斷config server是否正常,可開可不開 #spring.cloud.config.fail-fast=true # # #這個是設置與Eureka Server交互的地址,客戶端的查詢服務和注冊服務都需要依賴這個地址。 eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/ # #springboot 1.5.X 以上默認開通了安全認證,這里可加可不加,不影響 #management.security.enabled=false #springboot 2.x 默認只開啟了info、health的訪問接口,*代表開啟所有訪問接口 management.endpoints.web.exposure.include=* # #可以使用 window指令框 發送post請求 刷新配置文件 ,curl -X POST http://localhost:200/actuator/refresh # #
啟動類
package com.example.configclient6080; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication //開啟發現服務 ,也可以使用 EnableEurekaClient @EnableDiscoveryClient public class ConfigClient6080Application { public static void main(String[] args) { SpringApplication.run(ConfigClient6080Application.class, args); } }
Controller層接口
package com.example.configclient6080.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.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RefreshScope @RestController public class GetController { @Value("${yourname}") private String namestr; @RequestMapping(value = "/getname", method = RequestMethod.GET) public String getConfig() { String str = "獲取遠程配置文件信息:" + namestr + "===" + new Date(); System.out.println(str); return str; } // http://localhost:6080/getname }
5.其他准備
(1).提前准備配置 一個 端口為7001 的服務注冊中心

(2). GitHub倉庫准備文件

6.測試
網址訪問 端口 6080 , http://localhost:6080/getname

修改GitHub倉庫 gittest.properties 文件

再次 網址訪問 端口 6080 , http://localhost:6080/getname ,發現配置文件沒有更新

訪問請求刷新配置文件接口, ,需要以post方式請求 http://localhost:6080/actuator/refresh
在window 系統 可以 cmd 打開指令框 執行 curl -X POST http://localhost:6080/actuator/refresh
也可以使用軟件 postman ,

返回結果 顯示 更新的字段內容 ,
如果是 【】則表示遠程配置文件沒有變化 ,但是仍然刷新成功了
現在 再次 網址訪問 端口 6080 , http://localhost:6080/getname ,發現配置文件已經 更新

