一:概述
二:Config 服务端 配置与测试
三:Config 客户端 配置与测试
四:Config 客户端 之 动态刷新:(实战、重要)
一:概述
1)分布式系统面临的问题 之 --配置问题:
2)是什么:
3)能干嘛:
a:集中管理配置文件:
b:不同环境,不同配置,动态化的配置更新,分环境部署,比如:dev/test/prod/beta/release
c:运行期间动态调整配置,不需要在每个服务部署的机器上,编写配置文件,服务会向 配置中心 统一拉取配置自己的信息。
d:当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置。
e:将配置信息,以 REST 接口的形式暴露。
4)与 Github 整合配置:
5)官网:
二:Config 服务端 配置与测试
1)用自己的账号,在 Github 上,新建一个 名称为 spring-cloud-config 的新 Repository:并建立 配置文件 :
2)由上一步获得,刚新建的 git 地址:(https://gitee.com/zhanglaoshi498/spring-cloud-config-3344.git)
3)本地硬盘目录上,新建 git 仓库,并 clone:(git clone)
4)此时,在本地 D 盘下,新建 D:\44\Spring-Cloud-Config 文件夹:
5)新建 Module 模块 cloud-config-center-3344 ,它即为 cloud 的配置中心模块 CloudConfig Center:
6)POM:
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
7)YML:
server:
port: 3344
spring:
application:
name: cloud-config-center-3344
cloud:
config:
server:
git:
# github 仓库路径
uri: https://gitee.com/zhanglaoshi498/spring-cloud-config-3344.git
# 搜索目录
search-paths:
- spring-cloud-config-3344
# 拉取分支
default-label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
8)主启动类:ConfigCenter3344:
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigCenter3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenter3344.class, args);
}
}
9)windows 下修改 hosts 文件,增加映射:(127.0.0.1 config-3344.com)
10)测试通过 Config 微服务,是否可以从 Guithub 上获取配置内容:
a:启动 3344
b:访问:(http://config-3344.com:3344/config-dev.yml)
11)配置读取规则:(5种)
a:/{label} / {application}-{profile}.properties
b:{application}-{profile}.yml:(默认读取 master 分支)(建议方式)
c:{application}-{profile}[/{label}]:
12)成功实现了 用 Spring Config 通过 GitHub 获取配置信息。
三:Config 客户端 配置与测试
1)新建 cloud-config-client-3355:
2)POM:
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3)bootstrap.yml:
a:是什么:
b:内容是什么:
server:
port: 3355 # 注意 ,先按照 配置文件的 端口来配置
spring:
application:
name: cloud-config-client-3355
cloud:
config:
label: master # 分支名称
name: config # 配置文件名称(后面会自动加上 - )
profile: dev # 读取后缀名称
uri: http://config-3344.com:3344 # 配置中心地址
eureka:
client:
# 表示 是否将自己注册进 eureka ,默认为 true
register-with-eureka: true
# 是否从 Eureka 抓取自己的注册信息,默认为 true
# 单节点无所谓,集群必须设置为 true,才能配合 ribbon 使用 负载均衡。
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
4)修改 config-dev.yml 配置,并提交到 github 中:比如加个变量 age / 加个 版本号 version
5)主启动:
@EnableEurekaClient
@SpringBootApplication
public class ConfigClient3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3355.class, args);
}
}
6)业务类:
@RestController
public class ConfigClientController {
/**
* 测试 从配置文件 获取配置信息
* 本地配置文件(application.yml)也有,
* 配置中心配置文件(bootstrap.yml)也有,
* 则优先加载 bootstrap 配置文件,并且不会被覆盖;
*/
@Value("${my.address}")
private String str;
@GetMapping(value = "/testConfig")
public String testConfig() {
return str;
}
}
7)测试:(http://127.0.0.1:3355/testConfig)返回(秦皇岛)
8)成功实现了 客户端 3355 访问 配置服务端 3344,通过 github 获取配置信息:(要是换环境,直接换 dev --> pro)
9)问题 随时而来:分布式配置的 动态刷新问题:
a:Linux 运维修改 Github 上的 配置文件内容,做调整。
b:刷新 配置中心 3344,发现 ConfigServer 配置中心立刻做出改变。
c:刷新 客户端 3355,发现 ConfigClient 客户端,没有做出改变。
d:3355 没有变化,除非重启 或 重新加载
e:难道 每次运维 修改配置文件,客户端都需要重启吗?
四:Config 客户端 之 动态刷新:(实战、重要)
1)避免 每次更新配置,都要重新启动客户端 3355:
2)动态刷新:(步骤)
a:修改 3355 模块:
b:POM 引入 actuator 监控:
c:修改 YML,暴露监控端口:
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
d:@RefreshScope 业务类 Controller 修改:
@RefreshScope
@RestController
public class ConfigClientController {
/**
* 测试 从配置文件 获取配置信息
* 本地配置文件(application.yml)也有,
* 配置中心配置文件(bootstrap.yml)也有,
* 则优先加载 bootstrap 配置文件,并且不会被覆盖;
*/
@Value("${my.address}")
private String str;
@GetMapping(value = "/testConfig")
public String testConfig() {
return str;
}
}
e:此时修改 github --> 3344 ---> 3355:(还是不生效)
f:HOW ?:需要运维人员 发送 post 请求,刷新 3355;
g:再次修改 github 配置文件,就可以实现 客户端 3355 的配置文件的 更新:
3)想想还有什么问题:
a:手动发送刷新请求,实现配置文件刷新,繁琐。(可以写脚本,批量刷新)
b:假如有 多个微服务 客户端,3355、3366、3377.......
c:每个微服务都要执行一次 post 请求,手动刷新?
d:可否广播,一次通知,处处生效??
e:我们想大范围的刷新,求方法:(做不到)(所以引入下面,消息总线 BUS)