说明:关于SpringCloud系列的文章中的代码都在码云上面
地址:https://gitee.com/zh_0209_java/springcloud-alibaba.git
Config
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。
Spring Cloud 提供了 ConfigServer来解决这个问题.
简介
Spring Cloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
SpringCloud Config 分为服务端和客户端两部分。
服务端也称为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密 信息等访问接口。
客户端则是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
创建Config Server端 cloud-config-center3344微服务
- 修改pom文件
<dependencies>
<!--引入config server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 核心依赖-->
<!-- <dependency>-->
<!-- <groupId>com.zh.springcloud</groupId>-->
<!-- <artifactId>core</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- </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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
-
在远程仓库(gitee/github)上创建配置文件,我这里使用的是gitee,国内的话码云还是比较快的
远程仓库中配置文件的名称为{application}-{profile}组成,比如我自己建的 config-dev.yml配置文件,config对应{application},dev 对应{profile},注意:{application}和{profile}之间必须使用-
-
项目中添加yml配置类
server:
port: 3344
spring:
application:
name: cloud-config-center # 注册进eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://gitee.com/zh_0209_java/springcloud-config.git # 远程仓库上配置文件的地址
label: master # 读取的分支
# ============ eureka client ===========
eureka:
# 设置控制台显示的服务路径
instance:
instance-id: configCenter3344
prefer-ip-address: true # 访问地址可以显示ip
# Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 1
# Eureka 服务端在收到最后一次心跳后等待时间上线,单位为秒(默认是90秒),超时将剔除服务
lease-expiration-duration-in-seconds: 2
client:
# 表示是否将自己注册进eurekaServer,默认为true
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
# 本机入住eurekaServer 地址
defaultZone: http://localhost:7001/eureka # 单机版
# defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka # 集群版
- 添加启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer // 开启配置中心服务端
public class ConfigCenter3344Application {
public static void main(String[] args) {
SpringApplication.run(ConfigCenter3344Application.class,args);
}
}
- 启动eureka7001和cloud-config-center3344 项目,访问测试
bootstrap.yml 和 application.yml的区别
-
application.yml
是用户级别的资源配置项 -
bootstrap.yml
是系统级别的,优先级更高 -
Spring Cloud 会创建一个 “Bootstrap Context”, 作为Spring应用的“ Application Context”的 父上下文。初始化的时候,‘Bootstrap Context’ 负责从 外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的“Environment”。
-
‘Bootstrap’ 属性有高优先级,默认情况下,他们不会被本地配置覆盖。‘Bootstrap Context’和‘Application Context’ 有着不同的约定,所以新增了一个‘bootstrap.yml’ 文件,保证‘Bootstrap Context’ 和‘Application Context’ 配置的分离。
-
要将client模块下的application.yml 文件改为bootstrap.yml 这是很关键的,
-
因为bootstrap.yml是比application.yml 先加载的。bootstrap.yml,优先级高于application.yml
新建Config 客户端 cloud-config-client3355
- 修改pom
<dependencies>
<!--引入config client依赖-->
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 新增
bootstrap.yml
配置文件
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 读取的分支
name: config # 配置文件名称
profile: dev # 读取后缀名称,上述3个综合条件:master分支上config-dev.yml的配置文件被读取
uri: http://localhost:3344 # 配置中心地址
# ============ eureka client ===========
eureka:
# 设置控制台显示的服务路径
instance:
instance-id: configClient3355
prefer-ip-address: true # 访问地址可以显示ip
# Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 1
# Eureka 服务端在收到最后一次心跳后等待时间上线,单位为秒(默认是90秒),超时将剔除服务
lease-expiration-duration-in-seconds: 2
client:
# 表示是否将自己注册进eurekaServer,默认为true
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
# 本机入住eurekaServer 地址
defaultZone: http://localhost:7001/eureka # 单机版
# defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka # 集群版
- 新增启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClient3355Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3355Application.class,args);
}
}
- 新增业务类
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String info;
@GetMapping("/configInfo")
public String getInfo(){
return info;
}
}
- 启动eureka7001, cloud-config-center3344 和cloud-config-client3355, 并访问3355服务中的configInfo接口
成功的实现了客户端3355访问SpringCloud Config3344 通过Gitee获取配置信息,当切换分支测试发现也是可以的,
问题:当我们修改了远程仓库的内容是,在访问3344 发现立即更新,但是访问3355发现没有更新,需要重启config客户端才能访问到最新的配置
Config 动态刷新
想要解决的问题就是避免每次更新配置都要重启客户端微服务才能生效的问题
- 在客户端3355服务中修改pom文件, 新增监控依赖
<!--监控依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改配置文件
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
- 在用到配置文件内容的业务类上新增注解
@RefreshScope
@RestController
@RefreshScope // 刷新注解
public class ConfigClientController {
@Value("${config.info}")
private String info;
@GetMapping("/configInfo")
public String getInfo(){
return info;
}
}
- 重启服务,修改远程仓库地址,在进行访问测试,发现还没有进行更新,
注意:当更新完配置文件,还需要访问一个POST请求即可 “curl -X POST http://localhost/3355/actuator/refresh”, 在此访问configInfo发现已经刷新。