springcloud配置中心CONFIG自动刷新
程序员文章站
2022-07-03 19:50:01
...
配置中心自动刷新的工作原理:
在git上修改配置文件,通过远程git配置的webhooks地址,通知本地,刷新本地缓存的配置信息,客户端调用配置
1.服务端配置(就是Config配置中心)
下面是我的config pom.xml,可以参考一下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ppc</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置中心启动类上加注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer //
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
3.在config配置文件配置git地址
git信息、mq信息、注册中心、打开刷新本地git缓存开关
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/******/springcloud.git //git地址
username: *********@163.com //账号
password: ******** //密码
bus:
trace:
enabled: true
rabbitmq: //rabbitmq
host: 192.168.38.133
port: 5672
username: guest
password: guest
eureka: //注册中心
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management: //打开刷新本地git仓库开关
endpoints:
web:
exposure:
include: "*"
4.在github上传配置文件,这个命名是有要求的是后面客户端的服务名加其他
/{name}-{profile}.yml
/{label}-{name}-{profile}.yml
name:即spring.application.name
profile: **的剖面
label: git分支,默认是master
5.客户端添加依赖,下面是我的pom仅供参考
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>fg</groupId>
<artifactId>testfeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testfeign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<product-client.version>0.0.1-SNAPSHOT</product-client.version>
<order-common.version>0.0.1-SNAPSHOT</order-common.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId> <!--添加依赖-->
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.配置客户端配置文件,配置中心信息等,并把文件名改成bootstrap.yml,如果不改有些配置会在加载配置中心文件之前加载,却找不到配置项,例如数据库配置
此处寻找的配置文件名字 :order-dev
spring:
application:
name: order
cloud:
config:
discovery:
enabled: true
service-id: CONFIG //配置中心
profile: dev //此处寻找的配置文件名字 :order-dev
rabbitmq:
host: 192.168.38.133
port: 5672
username: guest
password: guest
server:
port: 8083
7.在代码中获取配置信息
这是git上的配置信息
第一种 :在类上加@RefreshScope注解
@RestController
@RefreshScope
public class EnvController {
@Value("${env}")//env是配置名
private String env;
@GetMapping("/print")
public String print() {
return env;
}
}
第二种
加注解@ConfigurationProperties("girl") 找到对应配置
@RefreshScope 刷新配置值
@Data
@Component
@ConfigurationProperties("girl")
@RefreshScope
public class GirlConfig {
private String name;
private int age;
}
8.配置webhooks
在settings里面找到Webhooks
添加地址(本地localhost不能用,可以用natapp免费版映射一下)
推荐阅读
-
Nacos(四):SpringCloud项目中接入Nacos作为配置中心
-
SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码
-
Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
-
Springcloud 2.x 版本 分布式配置中心
-
带你入门SpringCloud 之 通过SpringCloud Bus 自动更新配置
-
跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
-
SpringCloud微服务(06):Config组件,实现配置统一管理
-
SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
-
spring cloud config 配置中心快速实现过程解析
-
spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config