SpringCloud-Spring Cloud Config配置属性刷新之手动刷新
程序员文章站
2022-06-12 23:33:35
...
目录
创建工程-microservice-config-client-refresh
远程配置
创建工程-microservice-config-client-refresh
pom.xml
config-learning/microservice-config-client-refresh/pom.xml
<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>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-client-refresh</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
</dependencies>
</project>
bootstrap.yml
spring:
cloud:
config:
uri: http://localhost:8080
profile: dev
label: master # 当configserver的后端存储是Git时,默认就是master
application:
name: foobar
application.yml
server:
port: 8081
ConfigClientController
com.itmuch.cloud.ConfigClientController
package com.itmuch.cloud;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile() {
return this.profile;
}
}
说明:
1)这里加上了@RefreshScope注解,支持配合更改刷新;
ConfigServerApplication
com.itmuch.cloud.ConfigServerApplication
package com.itmuch.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
测试
启动工程
1)microservice-config-server
2)microservice-config-client-refresh
修改前测试
修改远程配置文件并提交
手动刷新
C:\Users\shangwu>curl -X POST http://localhost:8081/refresh
["config.client.version","profile"]
C:\Users\shangwu>
再访问
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================
上一篇: 古代人为什么要建塔,塔有什么用途?
推荐阅读
-
使用Spring Cloud Config搭建配置中心(实现自动刷新)
-
spring cloud config+bus实现配置中心的自动刷新
-
Spring Cloud Bus与RabbitMq实现config-server自动刷新配置及注意问题
-
Spring Cloud(6)消息总线 Spring Cloud Bus + Spring Cloud Config 实现全自动刷新集群配置
-
SpringCloud-Spring Cloud Config配置属性刷新之手动刷新
-
【Spring Cloud总结】39.Spring Cloud Config配置属性刷新之手动刷新
-
Spring Cloud【Finchley】实战-06使用/actuator/bus-refresh端点手动刷新配置 + 使用Spring Cloud Bus自动更新配置
-
Spring Cloud 进阶之路 -- 消息总线 Spring Cloud Bus 配置手动刷新和动态自动刷新
-
Springcloud之config组件手动刷新
-
SpringCloud-Spring Cloud Config配置属性刷新之自动刷新