Skip to content

配置属性值调整后 @Value 无法动态进行修改  #6

@kangjunxiang

Description

@kangjunxiang

业务模块版本:Springboot 2.3.3
配置客户端使用版本:configcenter-spring-boot-starter-1.6.1.RELEASE
yml 配置:

configcenter:
  app-id: customer
  server-url: http://127.0.0.1:6220
  enable: true
ant:
  env:
    refresh-placeholders:
      enable: true
    refresh-properties:
      enable: true

业务代码:

 @Refreshable
@RestController
public class DemoController {
    // 配置变更后,自动刷新redisHost字段
    @Value("${redis.host}")
    private String redisHost;
    // 配置变更后,自动刷新redisHost字段
    @Value("${redis.host}")
    private String redisHost2;

    private String redisHost3;

    // 配置变更后,自动重新调用setPort方法
    @Value("${redis.host}")
    public void setPort(String host){
        redisHost3 = host;
    }
    /**
     * 获取动态参数
     */
    @GetMapping("/getDynamic")
    public AjaxResult getDynamic(HttpServletResponse response) throws IOException {
        System.out.println(redisHost);
        Config config = ConfigsContexts.getConfig("customer");
        String redisHostFromConfig = config.getProperties().getProperty("redis.host");
        System.out.println(redisHostFromConfig);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("redisHost",redisHost);
        ajax.put("redisHostFromConfig",redisHostFromConfig);
        ajax.put("redisHost2",redisHost2);
        ajax.put("redisHost3",redisHost3);
        return ajax;
    }

响应报文:

{"msg":"操作成功","redisHost":"6666","redisHost3":"6666","code":200,"redisHostFromConfig":"8888","redisHost2":"6666"}

操作:
将 customer 应用下的 redis.host 配置在界面调整(6666调整为8888)并发布后,目前 @value("${redis.host}") 注解未自动刷新属性值,重启后该值才能被替换。config.getProperties().getProperty("redis.host"); 的用法可以获取到动态值。

        Config config = ConfigsContexts.getConfig("customer");
        String redisHostFromConfig = config.getProperties().getProperty("redis.host");

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions