javascript
Spring Cloud Config Server简介
1.概述
在本教程中,我們將回顧Spring Cloud Config Server的基礎知識。 我們將設置一個Config Server ,然后構建一個客戶端應用程序 ,該客戶端應用程序在啟動時會消耗配置 ,然后刷新配置而不重新啟動。 我們正在構建的應用程序與《 集中式配置入門指南 》中討論的“ Hello World”應用程序相同,但是在本文中,我們將更深入地介紹Spring Cloud Config Server的概念。
本教程的完整源代碼在Github上 。
2.什么是Spring Cloud Config Server?
正如文檔簡要指出的那樣,“ Spring Cloud Config為分布式系統中的外部化配置提供服務器和客戶端支持。” 服務器存儲后端的默認實現使用git ,因此它輕松支持帶標簽的配置環境版本,并且許多用于管理內容的工具都可以使用它。
Spring Cloud Config 非常適合Spring應用程序,因為它的客戶端和服務器概念都精確地映射到Spring Environment和PropertySource抽象。 但是,Spring Cloud Config可以與以任何語言運行的任何應用程序一起使用。
3.創建一個多模塊項目
我們正在創建的應用程序將具有兩個模塊:一個模塊用于配置服務,另一個模塊用于配置客戶端。 因此,我們需要創建一個父pom 。
3.1父母
在我們的IDE中,讓我們創建一個新項目。 我正在使用Spring Tool Suite,但這只是個人喜好。
在我們的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.michaelcgood</groupId><artifactId>com.michaelcgood</artifactId><version>0.0.1</version><packaging>pom</packaging><name>michaelcgood-spring-cloud-config-server</name><description>Intro to Spring Cloud Config Server</description><modules><module>mcg-configuration-client</module><module>mcg-configuration-service</module></modules></project>3.2配置服務
在我們的IDE中,讓我們為配置服務創建一個新的Maven模塊,并將其插入到pom中 :
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><groupId>com.michaelcgood</groupId><artifactId>mcg-configuration-service</artifactId><version>0.0.1</version><packaging>jar</packaging><name>mcg-configuration-service</name><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Edgware.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>3.3配置客戶端
現在,我們只需要為我們的配置客戶端創建一個模塊。 因此,讓我們制作另一個Maven模塊并將其插入到pom中 :
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><groupId>com.michaelcgood</groupId><artifactId>mcg-configuration-client</artifactId><version>0.0.1</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><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.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Edgware.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project> 我們的項目結構現在看起來像這樣:
4.配置服務器
現在,我們將創建一個Config Servic e,以充當客戶端和git存儲庫之間的中介。
4.1啟用配置服務器
我們使用Spring Cloud的@EnableConfigServer創建可以與之通信的配置服務器。 因此,這只是一個普通的Spring Boot應用程序,其中添加了一個注釋以啟用Config Server 。
@EnableConfigServer @SpringBootApplication public class ConfigServiceApplication {public static void main(String[] args) {SpringApplication.run(ConfigServiceApplication.class, args);} }4.2 application.properties
為確保Config Service和客戶端的端口之間沒有沖突,我們為Config Service指定了另一個端口:
server.port=8888spring.cloud.config.server.git.uri=${HOME}/Desktop/mcg-config第二行spring.cloud.config.server.git.uri = $ {HOME} / Desktop / mcg-config指向git存儲庫,我們將在其下創建它。
4.3 Git
在* nix系統上,我們可以在命令行上執行所有操作。
我們在桌面上創建一個文件夾:
我們使用vim創建一個名為a-bootiful-client.properties的文件:
vim a-bootiful-client.properties我們添加了消息“ Hello World”,但這可能是我們想要的。 寫(:w)之后,我們退出(:q)vim。
現在讓我們創建一個新的倉庫:
git init現在,我們添加包含消息的文件:
git add a-bootiful-client.properties讓我們提交:
git commit5.配置客戶端
現在,讓我們創建一個新的Spring Boot應用程序,該應用程序使用Config Server加載其自己的配置,并刷新其配置以按需反映對Config Server的更改,而無需重新啟動JVM。
Spring將看到配置屬性文件,就像從application.properties , application.yml或任何其他PropertySource加載的任何屬性文件一樣。
5.1反映變化
客戶端可以使用標準的Spring方法訪問Config Server中的任何值,例如@ConfigurationProperties或@Value(“ $ {…}}”) 。
考慮到這一點,我們創建一個REST控制器,該控制器返回已解析的message屬性的值:
@SpringBootApplication public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);} }@RefreshScope @RestController class MessageRestController {@Value("${message:Hello default}")private String message;@RequestMapping("/message")String getMessage() {return this.message;} }默認配置僅允許在客戶端啟動時讀取值,而不是再次讀取。 因此,使用@RefreshScope我們強制Bean刷新其配置,這意味著它將從Config Server中獲取更新的值,然后觸發刷新事件。
5.2 bootstrap.properties
在引導階段,必須先讀取配置Config Client的屬性,然后才能從Config Server中讀取應用程序的其余配置。
我們指定客戶端的spring.application.name以及配置服務器spring.cloud.config.uri的位置:
spring.application.name=a-bootiful-client spring.cloud.config.uri=http://localhost:8888 management.security.enabled=false 注意:
我們通過設置management.security.enabled = false禁用了安全性,從而使我們的測試和修補變得容易。
6.演示
首先,我們需要將目錄更改為我們的配置服務并啟動它:
mcg-configuration-service mike$ mvn spring-boot:run然后為我們的客戶做同樣的事情:
mcg-configuration-client mike$ mvn spring-boot:run當添加a-bootiful-client.properties時,我們可以在終端中看到配置服務:
INFO 5921 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/dk/48l9cm2x3vnfl5ymh6dtxpwc0000gn/T/config-repo-7195892194658362240/a-bootiful-client.properties讓我們打開瀏覽器并訪問http:// localhost:8080 / message 。 我們看到“ Hello World”。
現在,讓我們再次在a-bootiful-client.properties中更改消息,這次輸入“嗨! :-)”。
保存并提交后,我們訪問http:// localhost:8888 / a-bootiful-client / default確認更改。
現在,我們調用Spring Boot Actuator引用端點來刷新客戶端:
curl -X POST http://localhost:8080/refresh我們訪問http:// localhost:8080 / message并看到我們的消息“嗨! :-)“ 被陳列。
有關Spring Boot Actuator的更多信息,請參閱教程Building Spring Boot RESTful Service + Spring Boot Actuator 。
7.結論
我們剛剛在Spring中完成了服務的集中配置。 我們通過站起來一個Spring Cloud Config Server并創建一個客戶端來在啟動時使用配置,然后刷新配置而不重新啟動來實現此目的。
我們沒有接觸過的Spring Cloud Config Server可以完成許多其他事情,例如:
- 讓Config Server向Spring Cloud Netflix,Eureka Service Discovery或Spring Cloud Consul的Discovery Service注冊
- 提供YAML或屬性格式的配置
- 服務純文本配置文件
- 將配置服務器嵌入到應用程序中
完整的源代碼可以在Github上找到。
翻譯自: https://www.javacodegeeks.com/2017/12/intro-spring-cloud-config-server.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Spring Cloud Config Server简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java配置openjdk_Java大新
- 下一篇: 自定义MongoDB的Spring So