简介
在分布式系统中,通常子系统会非常多,为了方便配置文件的统一管理,实时更新,所以需要分布式配置中心。在Spring Cloud中,配置中心组件是Spring Cloud Config,它支持配置文件放到本地,同时也支持放到远程GIT或SVN仓库中(这种模式下,Spring Cloud Config会同步文件到本地缓存)。在Spring Cloud Config组件中,主要分为Config Server和Config Client两个角色,配置文件存放在Config Server中,Config Client需要从Config Server同步配置文件。
使用配置中心的优点:
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新
- 运行期间,不需要去服务器修改配置文件,服务会从配置中心拉取自己的信息
- 配置信息改变时,不需要重启即可更新配置信息到服务
- 配置信息以 Rest接口暴露
搭建Config Server
新建一个Config Server项目,POM文件如下
<?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.dothwinds</groupId>
<artifactId>spring-cloud-study-config</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.dothwinds</groupId>
<artifactId>spring-cloud-study-service-config</artifactId>
<version>1.0.0</version>
<name>spring-cloud-study-service-config</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
修改启动类,加入Config注解及Eureka Client的注解
package org.dothwinds.springcloudstudyserviceconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringCloudStudyServiceConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStudyServiceConfigApplication.class, args);
}
}
本篇介绍配置文件放到GIT上,所以我们在码云上建立一个仓库,https://gitee.com/dothwinds/spring-cloud-config-server ,把其他服务所需配置文件放到GIT仓库中
修改Config Server配置文件如下
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/dothwinds/spring-cloud-config-server #git服务器地址,这里我们在gitee上建立一个仓库
username: #访问此GIT地址所用用户名
password: #访问此GIT地址所用密码
search-paths: configs #git目录下存放的文件夹
label: master #GIT仓库所用分支名称
server:
port: 12000
本例当中使用的是GIT的公共仓库,无需用户名和密码访问,所以配置文件中此处为空值
搭建Config-Client
接下来,我们来搭建一下Config-Client工程,新建项目,编辑POM文件如下:
<?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.dothwinds</groupId>
<artifactId>spring-cloud-study-config</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.dothwinds</groupId>
<artifactId>spring-cloud-study-config-client</artifactId>
<version>1.0.0</version>
<name>spring-cloud-study-config-client</name>
<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>
</dependencies>
</project>
新建bootstrap.yml配置文件,内容如下,这里说一下bootstrap.yml在这里是一个典型的应用场景,此配置文件的加载早于application.yml,当Config-Client服务启动后,默认会先访问bootstrap.yml,然后绑定Config-Server,然后获取配置文件信息。
server:
port: 12001
spring:
cloud:
config:
uri: http://localhost:12000/ #config server
profile: dev #读取的配置文件后缀名
application:
name: config-client
profiles:
active: dev
然后我们创建Config-Client的配置文件application-dev.yml文件,并把文件传到gitee服务器上,内容比较简单,只有一name=dothwinds的值
在Config-Client中写一个访问地址接口,读取配置文件中name的值
package org.dothwinds.springcloudstudyconfigclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SpringCloudStudyConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStudyConfigClientApplication.class, args);
}
@Value("${name}")
private String name;
@GetMapping("/getName")
public String getName(){
return name;
}
}
启动服务
然后我们启动Config-Server,先测试一下Config-Server访问配置文件的值people.name
可以访问到,证明配置中心可以远程访问仓库中的配置文件。
请求地址和资源文件关系映射如下
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
通过映射可以访问到配置文件,如下图:
然后我们在启动Config-Client服务,访问getName接口,可以成功访问到,证明我们的Config-Client可以访问到Config-Server从Git同步下来的配置文件。
参考资料:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/single/spring-cloud.html
代码地址:https://gitee.com/dothwinds/Spring-Cloud-Study/tree/master/spring-cloud-study-config