通過優銳課核心java學習筆記中,我們可以看到,更改微服務的屬性可能會導致一個複雜的問題。 在本文中,我們將看到Spring Cloud Config Server和微服務如何相處。碼了很多專業的相關知識, 分享給大家參考學習。
在分佈式系統中管理微服務的配置是一項繁瑣且耗時的任務,尤其是當我們談論的是由大量微服務組成的大型系統時。
每次需要更改微服務的配置時,都將轉到相應的項目,更改其配置,然後重新啓動應用程序以使更改生效。
通過引入Spring Cloud config實用程序項目解決了這一挑戰,該項目爲分佈式系統中的外部化配置提供支持,並提供以下功能:
1.在一箇中央存儲庫中管理分佈式系統的所有模塊的配置。
2.動態刷新配置,無需在更改配置後重新啓動每個服務。
在本教程中,我們提供了一個設置Spring Cloud配置服務的分步指南,除了構建一個客戶端,該客戶端在啓動時使用中央配置,然後在不重新啓動的情況下刷新其配置。
1-創建配置服務項目
打開Eclipse,然後創建一個新的Maven項目並將其命名爲SpringCloudConfigServer。
2- 添加依賴關係
添加Spring Cloud Config服務器依賴項:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
然後添加以下依賴項管理屬性:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
僅此而已,只需添加Spring boot starter父級,你就可以開始了。
以下是完整的pom.xml供參考:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.programmer.gate</groupId>
<artifactId>SpringCloudConfigServer</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RC1</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-創建配置庫
下一步是創建存儲所有配置文件的存儲庫,你可以選擇任何存儲庫系統,例如GIT,SVN等。在本教程中,我們使用GitHub。
在桌面上創建一個名爲spring-cloud-repo的新文件夾。
分別創建2個屬性文件,分別命名爲client-A.properties和client-B.properties。
這兩個屬性文件都僅具有以下簡單屬性:client-A.properties:hello.message =來自客戶端A的Hello
client-B.properties:hello.message =來自客戶端B的Hello
每個屬性文件都對應一個微服務模塊,應命名爲<微服務名稱> .properties。
在客戶端項目中使用“ spring.application.name”定義微服務的名稱。
我們還可以添加一個名爲“ application.properties”的通用文件,以存儲所有客戶端的通用配置。
提交併推送到GIT文件夾。
4- 配置 application.properties
在src / main / resources下創建application.properties並添加以下配置:
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/husseinterek/spring-cloud-repo.git
在這裏我們爲配置服務器定義了一個自定義端口,並且還定義了存儲配置文件的存儲庫的路徑,該存儲庫的路徑是從我們先前創建的GIT項目中複製的。
PS:如果我們沒有定義自定義端口,Spring Cloud配置服務器會自動在localhost:8888上運行
5- Application.java
創建啓動器類並使用@EnableConfigServer對其進行註釋,這是將我們的項目標記爲配置服務器的註釋。
package com.programmer.gate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6-啓動配置服務器
以可執行jar形式啓動服務器,並通過運行以下url來確保其充當配置服務器並公開客戶端的配置文件:
http:// localhost:8888 / <客戶端名稱> / default
該URL應該以JSON格式返回在請求的配置文件下定義的配置屬性。
測試客戶端的配置後,我們得到以下結果:
Spring Cloud Configuration Server上的完整代碼
7-創建Spring Cloud Client
現在,我們將創建一個使用配置服務器公開的中央配置的客戶端。
再次進入eclipse並創建一個新的Maven項目,並將其命名爲SpringCloudConfigClientA。
8-添加客戶端依賴項
轉到topom.xml並添加以下依賴項:
<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>
在這裏,我們將Spring Cloud配置庫導入到我們的項目中,並將其定義爲Web應用程序。
“ spring-boot-starter-actuator”依賴項提供了實用程序方法,這些方法可檢查配置服務器的運行狀況並在更改後刷新配置。
9- bootstrap.properties
在src / main / resources下創建bootstrap.properties。
spring.application.name=client-A
spring.cloud.config.uri=http://localhost:8888
該文件在客戶端應用程序啓動時運行,它爲客戶端應用程序定義了唯一名稱,除了配置服務器的路徑。
在“ spring.application.name”下定義的名稱應類似於存儲在存儲庫下的配置文件的名稱。
附言:配置服務器的默認URL是localhost:8888,因此,如果我們從此處刪除它,我們仍然可以連接到配置服務。
10- application.properties
在src / main / resources下創建application.properties。
server.port:9095
在此文件中,我們定義了每次更改後都需要重新啓動的配置,這裏我們只爲應用程序定義了一個自定義端口。
11-創建REST控制器
在com.programmer.gate下創建Application.java:
package com.programmer.gate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RefreshScope
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Value("${hello.message}")
private String message;
@RequestMapping("/message")
String getMessage() {
return this.message;
}
}
我們將我們的類定義爲一個控制器,該控制器公開一個簡單的方法/ message,該方法打印出hello.message屬性的值,以確保我們的客戶端已成功從配置服務讀取。
12- 刷新配置
我們返回到存儲庫並在client-A.properties下將“ hello.message”屬性的值修改爲:
hello.message=Hello from Client A updated
爲了將此更改反映給Client A應用程序,你應該運行以下POST URL:
http:// localhost:9095 / actuator / refresh
現在,當你運行http:// localhost:9095 / message時,將得到以下結果:
Hello from Client A updated
Spring Cloud Configuration Client上的完整代碼
喜歡這篇文章的可以點個贊,歡迎大家留言評論,記得關注我,每天持續更新技術乾貨、職場趣事、海量面試資料等等
如果你對java技術很感興趣也可以加入我的java學習羣 V–(ddmsiqi)來交流學習,裏面都是同行,驗證【CSDN2】有資源共享。
不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代