Spring boot 应用实现动态 Spring boot 应用实现动态刷新配置详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring boot 应用实现动态 Spring boot 应用实现动态刷新配置详解

lbl2018   2021-09-09 我要评论
想了解Spring boot 应用实现动态刷新配置详解的相关内容吗,lbl2018在本文为您仔细讲解Spring boot 应用实现动态的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,boot,Spring,boot实现动态,下面大家一起来学习吧。

参考:https://www.cnblogs.com/flying607/p/8459397.html

文章使用springboot版本:2.0.4.RELEASE springcloud版本Finchley.SR1

1. 依赖

需要引入下面三个依赖:

compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')

(1)spring-cloud-starter-config是为了实现刷新配置

(2)spring-boot-starter-actuator是为了暴露修改/刷新配置的接口

(3)spring-boot-starter-web是为了可以访问暴露的修改/刷新配置的接口

2. 配置暴露接口

application.properties

#使用端口9999
server.port=9999
#暴露接口
management.endpoints.web.exposure.include=env,refresh

(1)env接口,可以获取配置(GET),也可以修改配置(POST)

(2)refresh接口,可以刷新配置(POST),使得@RefreshScope标注的value可以重新注入。

3. @RefreshScope

在需要实时刷新配置的地方加上@RefreshScope注解

4. 启动服务

5. 修改配置

访问localhost:9999/actuator/env(GET),可以获得此时的配置

访问localhost:9999/actuator/env(POST)

{
    "name":"somekey",
    "value":"newvalue"
}

如上可以把配置中somekey对应的值改为newvalue

6. 获取配置值

不调用刷新接口,直接获取注入的配置值,发现还是旧的值

7. 刷新配置 重新获取

访问localhost:9999/actuator/refresh(POST)刷新配置

重新获取注入的配置值,发现是新的值

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们