前言
在上一篇zuul的介绍中已经提到了配置中心,配置中心的作用就是将各个服务的配置文件统一管理。它就是Spring Cloud Config。
一、简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:
二、搭建Config Server工程
在之前的父工程上继续创建子module工程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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.cxlsky.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:
@SpringBootApplication(scanBasePackages = {"com.cxlsky"})
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
增加application.yml文件,并做如下配置:
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/longlogn/config
default-label: master
search-paths: '{profile}'
eureka:
client:
serviceUrl:
defaultZone: http://10.1.81.33:8000/eureka/
这里的配置含义如下:
spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,所以没有配置用户名和密码。
在配置的远程仓库https://github.com/longlogn/config中新增一个eureka-client-a-dev.yml的配置文件,配置文件的命名规则为-.yml,applicationName为服务名,profile为运行环境,因此这里的配置文件是eureka-client-a的配置文件,其配置内容为:
test: 123
启动程序:访问http://localhost:8888/test/dev,会得到如下结果:
证明配置服务中心可以从远程程序获取配置信息。
http请求地址和资源文件映射如下:
1、/{application}/{profile}[/{label}]
3、/{application}-{profile}.yml
4、/{label}/{application}-{profile}.yml
5、/{application}-{profile}.properties
6、/{label}/{application}-{profile}.properties
三、改造现有服务使用配置中心配置文件
改造eureka-client-a服务,增加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.cxlsky.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client-a</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
修改配置文件application.yml,如下:
server:
port: 8001
spring:
application:
name: eureka-client-a
cloud:
config:
profile: dev
discovery:
enabled: true #开启服务发现,从注册中心找config-server服务
service-id: config-server #注册到注册中心的配置中心服务名
eureka:
client:
serviceUrl:
defaultZone: http://10.1.81.33:8000/eureka/
在controller里将test的配置值注入,并在接口中输出值:
@Value("${test}")
private String test;
@GetMapping("/hello")
public String hello() {
return "applicationName is: " + applicationName + ", port is: " + port + " config server test: " + test;
}
四、测试
启动eureka-client-a项目,并本地postman调用接口测试得到结果:
可以从上图看到,注入的值取了配置中心的配置。这样我们的配置中心服务就搭建完成了。