为了避免更改配置后重新启动应用,本文介绍了SpringBoot应用在运行时重新加载配置的方法。

方案一: 使用spring-cloud-starter-config实现

spring cloud config 不仅可以刷新远程git的配置到内存中,而且还可以结合actuator还可以刷新本地的配置文件到内存中。

  1. 添加 maven 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用于监控和管理应用自身信息, 可以动态refresh配置-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--用于配置管理-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

其中: (1)spring-cloud-starter-config是为了实现刷新配置 (2)spring-boot-starter-actuator是为了暴露修改/刷新配置的接口

  1. 配置actuator 端点
management:
  server:
    port: 9001
  endpoints:
    web:
      exposure:
        include: "*"

  1. 便写获取配置的测试类 测试使用@Enviroment 、 @Value、自定义配置类三种方式
@RestController
@RequestMapping("/api/v1/config")
@RefreshScope
public class ConfigController {
    @Autowired
    ConfigProperties configProperties;

    @Autowired
    Environment environment;

    @Value("${server.port}")
    private String port;

    @GetMapping("/getConfig")
    public Map<String, Object> getConfig() {
        Map<String, Object> data = new HashMap<>();
        data.put("AppNameInConfigProperties", configProperties.getAppName());
        data.put("PortInEnvironment", environment.getProperty("server.port"));
        data.put("PortInValue", port);
        return data;
    }
}

package com.mochi.example.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix="mochi")
@Component
@RefreshScope
public class ConfigProperties {
    private String appName;

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }
}

配置项如下:

server:
  port: 9311

mochi:
  appName: mochi-app-test

4.1. 使用HTTP请求动态更改配置

使用 curl -X GET '<http://localhost:9311/api/v1/config/getConfig'> 查看配置信息

{
  AppNameInConfigProperties: "mochi-app-test",
  PortInEnvironment: "9311",
  PortInValue: "9311"
}

然后使用用 POST 请求访问 http://localhost:9001/actuator/env 来修改具体配置;使用 POST访问http://localhost:9001/actuator/refresh 进行刷新

curl -X POST -H "Content-Type:application/json" --data "{\\\\"name\\\\": \\\\"mochi.appName\\\\", \\\\"value\\\\": \\\\"newappname\\\\"}"  '<http://localhost:9001/actuator/env>'

curl -X POST '<http://localhost:9001/actuator/refresh>'

重新获取配置信息,发现已经更新成最新值了

{
  AppNameInConfigProperties: "newappname",
  PortInEnvironment: "9311",
  PortInValue: "9311"
}