SpringCloud之Config配置中心搭建和客户端配置文件实时刷新
一、配置中心作用
spring cloud config就是一个配置中心。其采用集中式管理每个微服务的配置信息,并使用GIT等版本仓库统一存储配置内容,实现版本化管理控制。微服务与配置中心使用rest方式交互来实现可扩展的配置服务。
spring cloud config配置中心解决了微服务系统的配置中心化、配置版本控制、平台独立、语言独立等问题,其特性如下:
- 提供服务端和客户端支持(spring cloud config server和spring cloud config client);
- 集中式管理分布式环境中的配置信息;
- 基于spring环境提供配置管理,与spring系列框架无缝结合;
- 可用于任何语言开发环境;
- 默认基于GIT仓库实现版本控制;
二、配置中心原理
三、配置中心应用
提前搭建好eureka。
远程配置文件
注意名称。
服务端配置
pom.xml引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动类配置yml配置
server:
port: 8999
spring:
application:
name: base-spring-config
cloud:
config:
server:
git:
uri: https://gitee.com/xxx/static-resource.git
username: xxx #用户名
password: xxx#密码
default-label: master #默认分支,如果不开启需要输入分支
search-paths: /** #配置文件目录
eureka:
client:
service-url:
defaultZone: http://192.168.xxx:9320/eureka
register-with-eureka: false #是否注册到eureka
fetch-registry: false #本地缓存服务
registry-fetch-interval-seconds: 5 #eureka client刷新本地缓存时间,默认30
instance:
appname: base-spring-config
prefer-ip-address: true
instance-id: ${ spring.cloud.client.ip-address}:${ spring.application.name}:${ server.port}
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则),默认30
lease-renewal-interval-in-seconds: 5
#Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己),默认90
lease-expiration-duration-in-seconds: 7
客户端配置
项目结构
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml
spring:
cloud:
config:
name: base-app-region
profile: dev
label: master
uri: http://127.0.0.1:8999/
实体类
@Data
public class PlatformBo {
private Long id;
private String platformName;
private String areaCode;
private String areaName;
private Integer levelType;
private String addressUrl;
private Integer sort;
private Integer isEnable;
}
@Data
@Component
@ConfigurationProperties(prefix = "regions")
public class PlatformBoConfig {
private List<PlatformBo> platform;
}
获取
四、热刷新client配置内容
配置拉取是一方面,如何做到不用重启项目,git上更新文件,客户端能够实时获取到。
@RefreshScope
在客户端的访问层添加config自带的刷新注解然后访问客户端:
http://127.0.0.1:8095/actuator/refresh进行刷新。
还没有评论,来说两句吧...