springboot的配置通常是在以下文件中进行配置,在打包时就涵盖,或者在发布时指定外部配置文件
通常一旦程序发布就不能修改配置,那有什么办法能够修改这上下文里的参数呢?
以下方法涉及:
以下测试版本说明:
主要用 SpringCloud 的上下文参数刷新的功能,接口 /actuator/refresh 的端点,实现应用属性的实时刷新。
application.yaml:
server:
port: 8080
spring:
application:
name: springboot-config-refresh
management:
endpoints:
web:
exposure:
include: refresh
demo:
message: files
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starterartifactId>
<version>${spring-cloud.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
dependency>
dependencies>
MyConfig.class
@Data
@Configuration
@ConfigurationProperties(prefix = "demo")
public class MyConfig {
private String message ="default message";
}
@RestController
@RequestMapping
public class ValueController {
@Autowired
private MyConfig myConfig;
@GetMapping
public String value(){
return myConfig.getMessage();
}
}
请求API获取初始配置
curl --location --request GET '127.0.0.1:8080/'
files
--spring.config.location=/Users/chenzy/application.yaml
,此时修改外部文件保存即可server:
port: 8080
spring:
application:
name: springboot-config-refresh
management:
endpoints:
web:
exposure:
include: refresh
endpoint:
refresh:
enabled: true
demo:
message: newfiles
请求 /actuator/refresh 触发旧bean的销毁,并返回修改项
curl --location --request POST '127.0.0.1:8080/actuator/refresh' \
--header 'Content-Type: application/json'
[
"demo.message"
]
请求API获取新配置
curl --location --request GET '127.0.0.1:8080/'
newfiles
配置被刷新,符合预期
以下演示 @Value 在指定的配置类中,而非 Controller 中直接引入
1.2步骤同(1.1 @ConfigurationProperties + /actuator/refresh)
@Data
@Configuration
@RefreshScope
public class MyValue {
@Value("${demo.value}")
private String demoValue;
}
application.yaml
server:
port: 8080
spring:
application:
name: springboot-config-refresh
management:
endpoints:
web:
exposure:
include: refresh
endpoint:
refresh:
enabled: true
demo:
message: files
value: files
@RestController
@RequestMapping
public class ValueController {
@Autowired
private MyConfig myConfig;
@Autowired
private MyValue myValue;
@GetMapping
public String value(){
return myConfig.getMessage();
}
@GetMapping("demo")
private String demoValue(){
return myValue.getDemoValue();
}
}
请求API获取初始配置
curl --location --request GET '127.0.0.1:8080/demo'
files
参照 上一个方法去修改对应配置的值
调用 /actuator/refresh 方法后,能够获取到最新值
curl --location --request POST '127.0.0.1:8080/actuator/refresh' \
--header 'Content-Type: application/json'
[
"demo.value"
]
curl --location --request GET '127.0.0.1:8080/demo'
newfiles
这种方法目前用处不多,可以自行参考demo