目录
配置中心组件Spring Cloud Config
1. 简介
在微服务架构中,通常都会有很多个微服务,当某些配置发生改变时,我们不可能⼀个⼀个去修改配置然后重启⽣效,在大多的场景下我们还需要在运⾏期间动态调整配置信息,⽐如:根据各个微服务的负载情况,动态调整数据源连接池⼤⼩,我们希望
配置内容发⽣变化的时候,微服务可以⾃动更新。因此,我们就需要对配置⽂件进⾏集中式管理,这就是分布式配置中⼼的作⽤。
Spring Cloud Config是⼀个分布式配置管理⽅案,分为Server端和 Client端两个部分。
- **Server 端:**提供配置⽂件的存储、以接⼝的形式将配置⽂件的内容提供出去,通过使⽤@EnableConfigServer注解在Spring Boot 应⽤嵌⼊
- **Client 端:**通过接⼝获取配置数据并初始化⾃⼰的应⽤
2. Spring Cloud Config的使用
Spring Cloud Config Server是集中式的配置服务,⽤于集中管理应⽤程序各个环境下的配置。 默认使⽤Git存储配置⽂件内容。
2.1 建立Config Server配置中⼼微服务
2.1.1 引入依赖
<!--config配置中⼼服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.1.2 启用配置服务器
使⽤注解@EnableConfigServer开启配置中⼼服务器。
/**
* @Author 笔墨画诗
* @Version 1.0.0
* @Create 2022/10/23 21:38
* @Desc
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
2.1.3 服务配置
server:
port: 10024
eureka:
instance:
prefer‐ip‐address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
client:
service-url:
defaultZone: http://localhost1:10010/eureka,http://localhost2:10011/eureka
spring:
application:
name: config-server-mic
cloud:
config:
server:
git:
uri: https://github.com/自己的账户/config-server-mic-repo.git
username: 用自己的账号
password: 用自己的密码
search-paths:
- config-server-mic-repo
default-label: master
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
访问:http://localhost:10024/master/config-server-mic-dev.yml,查看配置⽂件内容.