SpringCloud Config 用于集中管理各个微服务中的配置信息,支持使用 Git 管理配置文件,下面就实践一下,准备一个 Git 仓库,用于存放配置文件,创建一个Config Server,连接这个Git仓库,然后修改 employee-producer,从 Config Server 读取配置文件。
准备Git仓库
在仓库的根目录下新建一个目录 config-repo,在其中创建一个配置文件 employee-producer.properties
from=git-default-1
提交推送到服务器。
创建 Config Server
新建 springboot 项目 employee-config-server。
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.example</groupId>
<artifactId>employee-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>employee-config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR5</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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</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>
启动类中开启 config server
@SpringBootApplication
@EnableConfigServer
public class EmployeeConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeConfigServerApplication.class, args);
}
}
application.properties
spring.application.name=config-server
server.port=7001
spring.cloud.config.server.git.uri=https://gitee.com/xxx/springcloud-config
spring.cloud.config.server.git.searchPaths=config-repo
spring.cloud.config.server.git.username=xxx
spring.cloud.config.server.git.password=xxx
启动,访问 http://localhost:7001/employee-producer/dev/master,正常的话就会显示一段JSON数据,包含配置文件的内容。
修改 employee-producer
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
新建 bootstrap.properties
spring.application.name=employee-producer
server.port=7002
spring.cloud.config.profile=default
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
吧 application.properties 中的 spring.application.name 注释掉。
TestController 中添加:
@Value("${from}")
private String hello;
@RequestMapping("/hello")
public String from() {
return this.hello;
}
重新启动,访问 http://localhost:8070/hello,会输出配置文件中的值 git-default-1。