概述
分布式系统面临的问题-配置问题
微服务 意味着 要将单体应用中的业务拆成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务;
由于每个微服务 都需要相应的配置 才能正常运行,所以 一套 集中式、动态的配置管理 必不可少;
SpringCloud提供了ConfigServer解决这个问题;
what
Spring Cloud Config 为 微服务架构中的微服务 提供 集中式的外部配置支持;
配置服务器 为各个不同的微服务应用 的所有环境提供一个中心化的外部配置;
Spring Cloud Config 由服务端、客户端组成;
服务端:
称为分布式配置中心;
一个独立的微服务应用;
用来连接配置服务器 并 为客户端提供 获取配置信息、加密/解密等访问接口;
配置服务器默认采用Git存储配置信息;
有利于对环境配置进行版本管理;
可以通过Git客户端方便管理、访问配置内容;
客户端:
一个个微服务应用;
通过指定的分布式配置中心 来管理应用资源 及 业务相关的配置内容;
在启动的时候 从配置中心 获取/加载配置信息;
功能
集中管理配置文件;
不同环境不同配置、动态的配置更新、分环境部署(eg: dev/test/prod...);
运行期间动态调整配置,不需要在每个微服务编写配置文件,微服务会从配置中心统一拉取配置信息;
配置变化时,无需重启即可感知 并 使用新的配置;
将配置信息以REST接口形式暴露;
How
ConfigServer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
server:
port: 3344
spring:
application:
name: config
cloud:
config:
server:
git:
uri: #github地址
search-paths: #搜索github的路径
- sprin
label: master #读取github分支
eureka:
client:
register-with-eureka: true #是否向注册中心注册自己
fetchRegistry: true #是否从注册中心抓取已有的注册信息 默认true,集群必须设置为true
service-url:
defaultZone: http://localhost:7001/eureka/ #单机版
@EnableConfigServer // 开启ConfigServer
@SpringBootApplication
public class Config3344 {
public static void main(String[] args) {
SpringApplication.run(Config3344.class, args);
}
}
http://localhost:3344/config/application-prod.yml
配置读取规则
/{label}/{application}-{profile}.yml
/{application}-{profile}.yml
/{application}/{profile}/[{label}].yml
label:分支、application:应用名、profile:环境
仓库配置
1、Git仓库配置
2、SVN仓库配置
3、本地仓库
4、本地文件系统
a,Spring Cloud Config提供一种不使用Git仓库、SVN仓库的存储方式,使用本地文件系统的存储方式来保存信息;
b,实现方式:
spring.profiles.active=native;
Config Server 会 默认从应用的src/main/resource目录下搜索配置文件;
如果需要明确指定路径,可以使用spring.cloud.config.server.native.searchLocations=“”指定;
ConfigClient
bootstrap.yml
applcation.yml是用户级资源配置,bootstrap.yml是系统级(优先级高);
SpringCloud会创建一个"Bootstrap Context",作为Spring应用的"Application Context"的父上下文;
初始化的时候,Bootstrap Context负责从外部加载配置属性并解析配置;
Bootstrap Context 与 Application Context共享一个从外部获取的Environment;
Bootstrap Context有高优先级,默认情况下,不会被本地配置覆盖;
Bootstrap Context 与 Application Context有着不同的约定,所以新增一个bootstrap.yml,保证Bootstrap Context 与 Application Context配置分离;
要将ConfigClient的application.yml改为bootstrap.ym,很关键;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
server:
port: 8001
spring:
application:
name: eureka-payment-service
cloud:
config:
# label: #分支
name: application #配置文件名称
profile: dev #环境
uri: http://localhost:3344 #配置中心地址
@Slf4j
@RestController
public class PaymentController {
@Value("${config.info}")
private String config;
@GetMapping(value = "/getFromConfigServer")
public String getFromConfigServer(){
return config;
}
}
http://localhost:8001/getFromConfigServer
ConfigClient动态刷新配置
1、
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3、添加@RefreshScope
@RefreshScope
@RestController
public class PaymentController {
@Value("${config.info}")
private String config;
@GetMapping(value = "/getFromConfigServer")
public String getFromConfigServer(){
return config;
}
}
4、手动刷新
curl -X POST "http://localhost:8001/actuator/refresh"
存在的问题
如果客户端很多,大量的客户端手动刷新,怎么处理?
想要有选择去手动刷新,该如何处理?