這是使用Spring Cloud構建分佈式應用程式的第一步。 在本文中將演示/指導您創建服務以管理其他服務的配置。
本文中討論的內容包括:
- 什麼是配置伺服器? 為什麼在分佈式應用程式中需要伺服器?
- 說明集中配置管理和版本化配置管理。
- 設置存儲庫以存儲配置資訊。
- 構建並運行Spring Cloud伺服器。
1. 為什麼需要配置伺服器?
本小節中將討論為什麼需要一個服務來管理分佈式應用程式中其他服務的配置。
下麵是分佈式應用程式的示意圖 - “計程車管理應用程式”,包括三個子應用程式(乘客管理,司機管理和旅程管理),每個子應用程式都部署在伺服器上。
每個服務(應用程式)都是由開發人員團隊開發的專案。 在專案中,除了代碼之外,它還包含配置,例如,連接到資料庫的資訊,有關數據源位置的資訊等。如果您在專案代碼中創建此信息的硬代碼(在代碼中寫死資料庫連接資訊),這是一個很不好的做法。 因此,此信息通常放在單獨的檔中,這些檔稱為配置檔。
完成應用功能開發後,專案將打包並部署到伺服器上。 通常,配置檔將與代碼一起打包並形成唯一(檔)產品。 因此,如果配置中有任何更改,則需要編譯並重新打包專案並將其重新部署到伺服器上。 這顯然是分佈式應用程式環境中的挑戰。
使用配置伺服器
解決上述問題的想法是需要服務(應用程式)來管理其他服務的配置。 它在伺服器上獨立運行。
上述做法帶來以下好處:
在Config-Server上更改配置檔時,您肯定希望將此類更改通知給客戶端。 Spring Cloud Bus提供了一種機制,用於通知客戶“存在更改”並要求客戶端更新新資訊。
2. Config Server如何存儲數據?
將所有配置檔放在配置伺服器(Config Server)上時,您將詢問配置伺服器(Config Server)如何存儲這些檔。
配置伺服器(Config Server)有兩種主要的存儲配置檔的方法:
- 將它們作為系統檔存儲在伺服器的硬碟驅動器上。
- 使用GIT或SVN(Subversion)。
在本節中,將創建一個配置伺服器(Config Server),它將配置檔存儲在GitHub上。 創建了一個GitHub存儲庫:
3. 創建Spring Boot專案
在Eclipse上,創建一個Spring Boot專案:
輸入:
- Name: SpringCloudConfigServer
- Group: com.zaixian
- Artifact: SpringCloudConfigServer
- Description: Spring Cloud Config Server
- Package: com.zaixian.scconfigserver
接著選擇創建的功能:
好的,專案已經創建完成:
檔: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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zaixian</groupId>
<artifactId>SpringCloudConfigServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloudConfigServer</name>
<description>Spring Cloud Config Server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.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>${spring-cloud.version}</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>
<defaultGoal>compile</defaultGoal>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
在檔:SpringCloudConfigServerApplication.java 中添加注解:@EnableConfigServer ,完整代碼如下所示 -
package com.zaixian.scconfigserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer // 此代碼注解必須要加上
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
4. 配置Config Server
此服務(應用程式)將在端口8888
上部署並運行,並在GitHub上存儲配置檔,因此需要在檔:application.properties 檔中進行一些配置。
檔:application.properties -
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/zaixiancom/spring-cloud-config-server.git
# For File System:
# spring.profiles.active=native
# spring.cloud.config.server.native.searchLocations=C:/Users/tran/Desktop/config
如果Git Server(或SVN Server)需要用戶名/密碼,則需要進行其他配置。
5. 配置Config Server
右鍵單擊專案名稱,選擇:Run As -> Maven install ,然後打開命令行,進入到生成的Jar的target目錄,執行以下命令 -
D:\worksp\springcloud\SpringCloudConfigServer\target> java -jar SpringCloudConfigServer-0.0.1-SNAPSHOT.jar
運行後,輸出以下結果 -
測試Spring Cloud,打開流覽器訪問URL:
也就是上面創建的Githup中的內容: