概述
分布式系统面临的配置问题
SpringCloud config 是什么
SpringCloud config 能做什么
https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/
Config服务端配置与测试
1.在GitHub上新建一个名为springcloud-config的新Repository(需要是public的仓库,private的访问不了)
2.创建好后复制刚刚新建的git地址,黏贴到终端进行clone(可以先新建一个springcloud2021目录,等下用来clone到本地的存放目录)。(需要在本地安装好git 可看 windows安装git步骤)
此时在本地D盘 D:\document\GitHub\springcloud2021 下
此时这个springcloud2021就是我们本地的一个仓库
3.创建myorder文件夹
创建完后点击commit提交
4.创建视频中的三个yml文件
5.新建Module模块cloud-config-center-3344它既为Cloud的配置中心模块cloudConfig Center
6.改pom
<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka client(通过微服务名实现动态路由)-->
<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>
7.写yml
点击可以切换为ssh模式的地址(就是视频中的@git… ,不过ssh需要先配置才能使用,所以用https模式的地址比较方便)
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/aurora-am/springcloud-config.git #git的仓库地址
search-paths: #搜索目录
- springcloud-config
force-pull: true
username: aurora #用户名
password: zxcv.bnn #密码
label: master #读取的分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #服务注册到的eureka地址
8.主启动类
@EnableConfigServer
@SpringBootApplication
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
9.windows下修改hosts文件,增加映射
127.0.0.1 config-3344.com
10.在GitHub中的配置文件加入(点击这个文件,然后点击笔形状的按钮进行编辑)
#config-dev.yml
config:
info: "master branch,springcloud-config/config-dev.yml version=1"
#######################################
#config-prod.yml
config:
info: "master branch,springcloud-config/config-prod.yml version=1"
#######################################
#config-test.yml
config:
info: "master branch,springcloud-config/config-test.yml version=1"
11.测试通过Config微服务是否可以从Github上获取配置内容
先启动Eureka7001 再启动微服务3344 配置文件中有写3344要先去eureka中心注册
http://config-3344.com:3344/master/config-dev.yml
这里我连的时候报了一个错
org.eclipse.jgit.api.errors.TransportException: https://github.com/aurora-am
看了一下周阳老师的分支名是master 而我的默认是main 访问连接中也是通过master这个分支获取的 新建了一个分支master之后就好了
配置的读取原则
官网 有五种 这里讲常用的三种
第一种(我们上面用的就是这一种)
http://config-3344.com:3344/master/config-test.yml
第二种(分支不写,默认master分支)
http://config-3344.com:3344/config-test.yml
第三种(反着写)
http://config-3344.com:3344/config/test/master 返回json串
总结
Config客户端配置与测试
1.新建模块cloud-config-client-3355
2.改pom
<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka client(通过微服务名实现动态路由)-->
<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>
3. bootstrap.yml(系统级别的配置文件)
添加进来bootstrap.yml没有小叶子 手动添加一下
server:
port: 3355
spring:
application:
name: config-client
cloud:
config: #config客户端配置
label: master #分支名称
name: config #配置文件名称 这三个综合:master分支上的config-dev.yml的配置文件
profile: dev #读取后缀名称 被读取到http://config-3344.com:3344/master/config/dev
uri: http://localhost:3344 #配置中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #服务注册到的eureka地址
4.主启动类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
5.业务类 controller(读取GitHub的配置文件)
@RestController
public class ConfigClientController {
@Value("${config.info}") //spring的@Value注解
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
6.测试 启动Eureka 7001, 启动Config配置中心3344微服务并自测, 启动3355作为Client准备访问
7001测试: http://eureka7001.com:7001/
3344测试: http://config-3344.com:3344/master/config-dev.yml
3355测试: http://localhost:3355/configInfo
动态刷新问题
1.修改GitHub上的config-dev.yml文件的版本号为2。
2.刷新http://config-3344.com:3344/master/config-dev.yml,版本号发生改变。
3.刷新http://localhost:3355/configInfo,没有改变。
4.重启3355,刷新http://localhost:3355/configInfo
,读取到最新的版本号。
Config客户端之动态刷新
目的:避免每次更新配置都要重启客户端微服务3355
1.往config客户端3355在pom中添加(上面已经加了)图形化监控 除了网关不需要加 其他都要加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.然后在bootstrap.yml中添加
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3.在ConfigClientController类上加上@RefreshScope
注解。
4.重启3355。
5.修改GitHub上文件的版本号,然后访问3344和3355。(启动完成后再修改)
启动完后3355还是没读取到,需要发送post请求刷新3355才能生效 打开终端输入:
curl -X POST "http://localhost:3355/actuator/refresh"
刷新http://localhost:3355/configInfo 已刷新
成功实现了客户端3355刷新到最新配置内容 避免了服务的重启
👉 消息总线