美文网首页微服务架构
SpringBoot 配置项动态实时刷新

SpringBoot 配置项动态实时刷新

作者: DongGuangqing | 来源:发表于2020-02-16 10:45 被阅读0次

    为了避免更改配置后重新启动应用,本文介绍了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"
    }
    

    4.2 直接更改配置文件来动态刷新配置
    更改 target/classes/application.yaml 中的配置,然后使用 POST访问http://localhost:9001/actuator/refresh 进行刷新即可

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

    方案二: 使用监听配置文件变动来实现

    主要使用FileAlterationMonitor、FileAlterationObserver、FileAlterationListenerAdapter 来实现,主要流程如下,其中FileListenerAdaptor需要实现FileAlterationListenerAdapter的 onChange方法:

                // 构造观察类主要提供要观察的文件或目录,当然还有详细信息的filter
                FileAlterationObserver observer = new FileAlterationObserver(
                        new File(configPath), null, null);
                // 构造收听类
                FileListenerAdaptor listener = new FileListenerAdaptor();
                // 为观察对象添加收听对象
                observer.addListener(listener);
                // 配置Monitor,第一个参数单位是毫秒,是监听的间隔;第二个参数就是绑定我们之前的观察对象。
                FileAlterationMonitor fileMonitor = new FileAlterationMonitor(
                        1000, new FileAlterationObserver[]{observer});
    
                // 启动开始监听
                fileMonitor.start();
    

    更多干货,欢迎关注个人博客:http://mochi.website/

    相关文章

      网友评论

        本文标题:SpringBoot 配置项动态实时刷新

        本文链接:https://www.haomeiwen.com/subject/nejefhtx.html