1 场景导入
在分布式系统中,每个项目都具有各种各样的配置文件,随着我们的不断开发,服务增加,配置文件也在不断增加。每一次对服务信息的修改,都会导致更新与重启。为解决这个问题,我们急需新的解决方案,配置中心就这样进入了我们的视野。
2 配置中心 Spring Cloud Config
Spring Cloud Config 是一个解决分布式系统的配置管理方案。它包含客户端和服务端两个部分,服务端可以提供配置文件的存储,以接口的方式将配置文件的内容发布出去,而客户端通过接口获取数据。Spring Cloud Config 默认情况下使用 git 来存储配置文件。
3 构建一个简单的配置中心
我们启动一个服务作为配置中心的服务端,之后各个需要获取配置的服务作为客户端来这个服务端获取配置。
在自己的 git 仓库创建两个配置文件,一个为 consumer-dev.properties,另一个为 consumer-pro.properties
文件内容就是设置一个 consumer.saying 属性,两个文件的值分别为 Hello World/I want to do something。
这里需要注意的是,文件名可不能乱取(正常来说也没有人乱取吧)。consumer-dev.properties 和 consumer-pro.properties 是同一个项目的不同版本,其中项目名为 consumer,dev 对应开发环境,pro 对应生产环境。
我们这就开始构建配置中心服务端,首先引进 Config 服务端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
然后配置文件也是必须的,每个新配置的含义我都写在注解里了,大家不懂的可以认真看看注解。
server.port=8087
spring.application.name=config
# git仓库的地址
spring.cloud.config.server.git.uri=https://github.com/LingHaoYuan/config
# git仓库的账号
spring.cloud.config.server.git.username=
# git仓库的密码
spring.cloud.config.server.git.password=
# 配置文件分支
spring.cloud.config.server.git.default-label=master
# 配置文件相对地址
#spring.cloud.config.server.git.search-paths=
最后我们需要在启动类上添加 @EnableConfigServer 注解来添加对 Config 服务端的支持。
package com.example.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
接下来我们先测试一下配置中心的服务端是否可以读取 github 的配置文件,在浏览器上访问 http://localhost:8087/consumer/dev,结果如下:
{“name”:“consumer”,“profiles”:[“dev”],“label”:null,“version”:“07719761462c62db3333a050d8c8a8a7be96bb21”,“state”:null,“propertySources”:[{“name”:“https://github.com/LingHaoYuan/config/consumer-dev.properties”,“source”:{“consumer.saying”:"“Hello World”"}}]}
我们可以发现,这个返回信息包含了配置文件的各类信息,即说明配置中心的服务端是可以获取 git 仓库的配置文件信息。如果我们想直接查看配置文件的信息,可以在浏览器直接输入 http://localhost:8087/consumer-dev.properties,发现其确实返回了配置信息
consumer.saying: “Hello World”
事实上,Spring Cloud Config 是具有一套访问规则的,我们可以通过访问规则直接访问。
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application} 表示配置文件的名称,{profile} 表示配置文件的版本,例如开发版本就是 dev,{label} 表示 git 分支,默认是 master 分支。
配置中心客户端实战
我们之前创建了配置中心服务端,现在我们继续构建一个配置中心的客户端,并尝试在客户端中去获取服务端的配置信息。
我们使用之前的 consumer 项目,首先添加 Config 客户端的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后我们增加一个 bootstrap.properties 配置文件
# 对应{application}部分
spring.cloud.config.name=consumer
# 对应{profile}部分
spring.cloud.config.profile=dev
# 配置中心的地址
spring.cloud.config.uri=http://localhost:8087/
# 对应git的分支
spring.cloud.config.label=master
注意,只有与 spring-cloud 相关的属性配置在 bootstrap.properties 中,config 部分内容才能被正确加载。因为 config 的相关配置先于 application.properties,而 bootstrap.properties 也先于application.properties 加载。
启动类不需要改变,然后我们再重写控制器
package com.example.EurekaConsumer.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConfigTestController {
@Value("${consumer.saying}")
private String saying;
@RequestMapping("/test")
@ResponseBody
public String test() {
return saying;
}
}
我们启动配置中心服务端与客户端,访问 http://localhost:8082/test,发现其返回 “Hello World”,说明客户端可以正确从服务端获取配置信息,我们的配置中心客户端也实现成功。
参考:springcloud(六):配置中心git示例
Spring Cloud Config 实现配置中心,看这一篇就够了