目录
2.3.3、配置中心客户端业务类 Controller 添加 @RefreshScope
2.3.4、修改远程仓库代码后,需要发送post请求,刷新客户端
1、概念
1.1、 作用
Springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置。
1.2、结构
Springcloud Config分为服务端和客户端两部分。
1、服务端:
- 分布式配置中心,是一个独立的微服务应用
- 用于连接配置服务器
- 为客户端提供获取配置信息,加密、解密信息等访问接口。
2、客户端:
- 通过制定的配置中心管理应用资源,以及与业务相关的配置内容
- 在启动的时候从配置中心获取和加载配置信息
- 配置服务器默认采用git来存储配置信息,就有助于对环境配置进行版本管理
- 可以通过git客户端工具,方便的管理和访问配置内容。
1.3、功能
-
集中管理配置文件
-
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
-
运行期间动态调整配置,不需要在每个服务器的机器上编写配置文件,服务会向配置中心统一拉取各自的配置信息
-
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
-
将配置信息以REST接口的形式暴露,post,curl访问刷新均可
1.4、使用
与git进行整合,在git仓库中修改配置信息,并利用http和https的形式访问配置信息
2、配置中心的使用
2.1、服务端
直接从配置文件远程仓库,读取配置信息。
2.1.1、新建配置中心服务端微服务
2.1.2、在POM文件添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.1.3、yaml配置文件
添加 git 仓库,从 git 仓库获取配置信息
1)配置文件:
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进eureka的微服务名称
cloud:
config:
server:
git:
uri: https://gitee.com/uclass/springcloud-config.git #git仓库名字
# 搜索目录
search-paths:
- springcloud-config
# 读取分支
label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
2)git 远程仓库
2.1.4、主启动类
添加 @EnableConfigServer 注解
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
2.1.5、 测试
获取 git 仓库中的配置项信息
label:分支名称,aoolication:微服务名称,profile:环境名称
2.2、客户端
从配置中心服务端,获取信息
2.2.1、新建配置中心客户端微服务
2.2.2、POM文件添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.2.3、添加 yaml 配置文件
1.采用 bootstrap.yaml 配置文件
- bootstrap.yaml:系统级,资源配置项
- application.yaml:用户级,资源配置项
bootstrap.yaml相比于application.yaml,具有更高优先级。默认情况下,不会被本地配置所覆盖。
2.添加配置中心(服务端)信息
从配置中心服务端,需要获取的环境名称、类型以及服务端地址等信息。
server:
port: 3355
spring:
application:
name: config-client
cloud:
# 客户端配置(通过3344获得git中的配置文件)
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述三个综合http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344/ #配置中心的地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
2.2.4、主启动类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
2.2.5、添加业务类,测试获取的配置信息
@RestController
@Slf4j
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
log.info("开始读取配置");
return configInfo;
}
}
结果:
2.3、客户端动态刷新设置
当git远程仓库中的配置信息被修改后,配置中心服务端获取到的信息会实时更新,但是配置中心客户端获取的配置信息并不会得到更新。
2.3.1、POM引入actuator监控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.3.2、修改yaml,暴露监控接口
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
2.3.3、配置中心客户端业务类 Controller 添加 @RefreshScope
@RefreshScope
@RestController
@Slf4j
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
log.info("开始读取配置");
return configInfo;
}
}
2.3.4、修改远程仓库代码后,需要发送post请求,刷新客户端
命令:curl -X POST "http://localhost:3355/actuator/refresh"