Config 配置中心
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的管理设施式必不可少的。Spring Cloud 提供了 Config Server 来解决这个问题,我们每个微服务自己带着一个
application.yml
是十分臃肿的,且不宜于修改。Spring Cloud Config 为每个微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
作用
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如:dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会相配置中心统一拉取自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
- 将配置信息以REST接口的形式暴露
构建
git 仓库
新建 git 仓库并 clone。
创建服务
依赖坐标
<!-- config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
配置
server:
port: 3344
spring:
application:
name: config-center
cloud:
config:
server:
git:
username: keyboardWithDream
password: Hhn004460
force-pull: true
skip-ssl-validation: true
# git 地址
uri: https://github.com/keyboardWithDream/spring-cloud-config.git
# 搜索目录
search-paths:
- spring-cloud-config
# 读取分支
label: main
eureka:
instance:
appname: config-center-3344
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url: htt://localhsot:7001/eureka,htt://localhsot:7002/eureka
启动类
使用@EnableConfigServer
注解开启配置中心
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterApplication3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication3344.class, args);
}
}
客户端
bootstrap.yml
是系统级配置,application.yml
是用户级资源配置,Spring Cloud 会创建一个Bootstrap Context
作为 Spring 应用的Application Context
的父上下文。初始化的时候,Bootstrap Context
负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
。
Bootstrap
属性有高级优先,默认情况下,它们不会被本地覆盖。Bootstrap Context
和Application Context
有着不同的约定,所有新增了一个bootstrap.yml
文件,保证Bootstarp Context
和Application Context
配置的分离。
依赖坐标
<!-- config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置文件
bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
#config 客户端配置
config:
# 分支
label: main
# 配置文件名
name: config
# 文件名后缀 main/config-dev.yml
profile: dev
# 配置中心地址
uri: http://localhost:3344
eureka:
instance:
instance-id: config-client
prefer-ip-address: true
client:
fetch-registry: true
register-with-eureka: true
defaultZone: htt://localhsot:7001/eureka,htt://localhsot:7002/eureka
启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication3355.class, args);
}
}
动态刷新
客户端配置与 git 上的配置实现同步更新
客户端
依赖坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置
# 暴露监控端点
management:
endpoint:
web:
exposure:
include: "*"
controller
添加注解@RefreshScope
@RestController
@RefreshScope
@RequestMapping("/config")
public class ConfigController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/info")
public String info() {
return configInfo;
}
}
发送刷新请求
curl -X POST "http://localhost:3355/actuator/refresh"
刷新成功:
避免了重启刷新。