SpringCloud Config Client實際上就是連接到Config Server的普通應用,前面一篇文章 SpringCloud Config Server搭建 已經介紹瞭如何搭建一個Config Server,本文介紹如何讓一個Spring應用連上Config Server並使用Config Server上的配置信息。
創建一個Spring應用
不知道爲什麼,在https://start.spring.io/創建一個Config Client應用跑起來會有問題,所以我們創建一個普通的Spring Web應用,再自己配置一下即可。打開https://start.spring.io/,添加Spring Web依賴,填寫相關包信息後點擊GENERATE
,生成一個Spring Web項目的壓縮包:
將該項目文件解壓後導入Idea(或其他IDE)。
配置
首先需要在pom.xml
文件中加入以下依賴用於連接Config Server(完整的pom.xml文件見附錄):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在src/main/resources/bootstrap.properties
中加入以下配置(沒有該文件要手動創建一個):
spring.application.name=a-bootiful-client
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888
bootstrap.properties
文件中的配置會先於其它配置文件被Spring應用加載(在bootstrap階段),其中:
- spring.application.name:指定了應用的名稱,也決定了應用去Config Server讀取的是哪一個應用的配置,這裏指定的是我們上一篇文章搭建的Config Server中創建的
a-bootiful-client.properties
中的配置信息 - spring.cloud.config.uri:指定的是Config Server的地址,在這裏不指定也可以,因爲默認地址就是
http://localhost:8888
爲應用屬性注入Config Server中的配置
將Configclient1Application.java
改成類似以下的代碼:
package com.example.configclient1;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@SpringBootApplication
@RestController
public class Configclient1Application {
@Value("${message:Hello default}")
private String message;
public static void main(String[] args) {
SpringApplication.run(Configclient1Application.class, args);
}
@GetMapping("/message")
String getMessage() {
return this.message;
}
}
其中,打上@Value
註解的屬性會動態從Config Server讀取,如果讀取不到,則使用默認值Hello Defalut
完成以上步驟以後,不出意外的話run一下就可以把應用成功運行起來了。
訪問Config Client
打開瀏覽器,訪問http://localhost:8080/message
,出現類似以下界面:
可以看到默認值Hello default
已經被替換成了Config Server配置的屬性值Hello world
。可以修改Config Server中相應的配置項來查看效果,注意修改後要使用git commit
配置纔會生效。
附錄
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>configclient1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configclient1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.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-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>
</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>
</build>
</project>
參考資料
https://spring.io/guides/gs/centralized-configuration/