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/