Config学习
Config学习
为什么需要配置中心?
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的颗粒度较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的!
Config是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置
Config官网学习地址
官网:https://spring.io/projects/spring-cloud-config
3.0.4版本文档:https://docs.spring.io/spring-cloud-config/docs/current/reference/html/
Config能干嘛?
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署,比如dev/test/prod/beta/release
- 运行期间动态调整配置,不需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
Config实操
gitee搭建文件仓库
创建gitee仓库
-
登录网址https://gitee.com/lwenpu/dashboard/projects
-
创建仓库test-config
-
本地拉取 git clone https://gitee.com/lwenpu/test-config.git
-
创建config目录添加testConfig-dev.yml、testConfig-prod.yml、testConfig-test.yml三个版本文件,编写以下内容进行测试
config: username: xifeng-dev version: 1.0 -
提交git
git add . git commit -m "create test file" git push origin master
创建配置中心服务端
依赖添加
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 服务端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
yaml编写
application.yml
server:
port: 3344
spring:
application:
name: config-server # 应用名称
cloud:
config:
server:
git:
uri: https://gitee.com/lwenpu/test-config.git #配置文件所在仓库git地址
username: xifeng #登录账号
password: 12345678 #登录密码
default-label: master #配置文件分支
search-paths: config #配置文件所在根目录
启动
/**
* @author liuwenpu
* Description
* @version 1.0
* @date 2021/8/8 22:41
*/
@SpringBootApplication
@EnableConfigServer //启动config服务
public class ConfigServerMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigServerMain3344.class,args);
}
}
Config的访问规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。
{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。
{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
访问实测
http://localhost:3344/testConfig-dev.yml
http://localhost:3344/testConfig-test.yml
搭建客户端访问服务端
依赖添加
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring cloud config 客户端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
application.yml编写
server:
port: 3355
#暴露监控端点,用于刷新config配置,从而起到不需要重启系统可更新配置文件
management:
endpoints:
web:
exposure:
include: "*"
bootstrap.yml编写
#该配置必须写在bootstrap.yml,因为他的优先级比application.yml要高,属于系统及的配置
#具体原因参考博客:https://blog.csdn.net/wenyiCodeDog/article/details/93500604
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:3344 #配置中心服务端地址
name: testConfig #配置文件名称,必须和仓库配置文件的前缀名称一致
label: master #分支指定
profile: dev #仓库配置文件指定
添加Controller对外测试配置文件注入
/**
* @author liuwenpu
* Description
* @version 1.0
* @date 2021/8/8 23:23
*/
@RestController
@RefreshScope //用于自动刷新配置文件
public class TestConfigController {
/**
* 注入config配置文件中的version属性
*/
@Value("${config.version}")
private String version;
@GetMapping("/test")
public String test(){
return version;
}
}
启动
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
解决启动报错No spring.config.import set问题
springcloud2020 版本 把Bootstrap被默认禁用,
同时spring.config.import加入了对解密的支持。对于Config Client、Consul、Vault和Zookeeper的配置导入,
如果需要使用原来的配置引导功能,
那么需要将org.springframework.cloud:spring-cloud-starter-bootstrap依赖引入到工程中
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
访问测试
得到配置文件的注入内容:xifeng-dev
仓库配置文件修改客户端不更新问题
根据以上环境,我们修改gitee中的配置文件,可以看到Config服务端是同步请求git的,所以配置文件实时更新,但是我们客户端直接去访问Config服务端是不会进行实时更新的,这时候我们需要发送一个post请求来通知客户端更新!
操作步骤
- 控制层类添加@RefreshScope注解
- 发送请求更新:curl -X POST http://127.0.0.1:3355/actuator/refresh

浙公网安备 33010602011771号