1 概述
产生的原因:
每个微服务都需要一个配置文件,并且,如果有几个微服务都需要连接数据库
那么就需要配4次数据库相关配置(假如用的同一个数据库),并且当数据库发生改动,那么需要同时修改4个微服务的配置文件才可以,所以产生分布式配置中心
Spring Cloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供一个中心化的外部配置。
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。
目前SpringCloud Config的使用主要是通过Git/SVN方式做一个配置中心,然后每个服务从其中获取自身配置所需的参数。SpringCloud Config也支持本地参数配置的获取
2 创建config服务端
1)在git上新建一个名为springcloud-config的Repository
- 引入pom文件
<!-- Config服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 配置文件application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://gitee.com/xxxx/sprincloud-config.git #GitHub上面的git仓库名字
search-paths: #搜索目录
- springcloud-config
label: master #默认读取分支
#启动成功后访问的路径 http://ip:3344/{label}/{application}-{profile}.yml 能访问的配置文件 就表示成功了
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 主启动
@SpringBootApplication
@EnableConfigServer
public class CloudConfigCenter3344Application {
public static void main(String[] args) {
SpringApplication.run(CloudConfigCenter3344Application.class, args);
System.out.println("启动成功");
}
}
5)启动成功后访问
http://localhost:3344/master/config-dev.yml
3 创建config 客户端
1)引入pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2) 配置文件bootstrap.yml
application.yml 是用户级的资源配置项
bootstrap.yml 是系统级,优先级高
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述三个综合http://localhost:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心的地址
#服务注册到eureka地址
eureka:
client:
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://localhost:7001/eureka #单机版
3)主启动
@SpringBootApplication
@EnableConfigServer
ublic class CloudConfigClient3355Application {
public static void main(String[] args) {
SpringApplication.run(CloudConfigClient3355Application.class, args);
System.out.println("启动成功");
}
}
4)编写controller
配置中心将配置信息以Rest接口的形式暴露
@RestController
public class ConfigController {
@Value("${config.info}") //git上的信息参数配置
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
5)启动后访问http://localhost:3355/configInfo 获取到git上的信息
6)假如git上配置文件修改了,3344能实时获取到最新的数据,但是3355需要重启才能获取最新数据,可通过实现动态刷新获取实时数据
7)实现动态刷新
7.1)引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
7.2) 修改配置文件
# 暴露监控端点,默认打开所有 否则 curl -X POST "http://localhost:3355/actuator/refresh" 不可使用
management:
endpoints:
web:
exposure:
include: "*"
7.3) 修改controller
@RestController
@RefreshScope
public class ConfigController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
7.4) 所有服务启动后,修改配置文件,此时3355还不可以动态获取
因为此时,还需要外部发送post请求通知3355
curl -X POST "http://localhost:3355/actuator/refresh"
再次请求就可以动态获取
7.5)问题
如果有多个客户端怎么办(3355,3356,3357…)
虽然可以使用shell脚本,循环刷新
但是,可不可以使用广播,一次通知??
这些springconfig做不到,需要使用springcloud Bus消息总线
参考地址:https://editor.youkuaiyun.com/md?not_checkout=1&articleId=123069116