spring boot 配置动态刷新详解
作者:剑握在手 发布时间:2023-09-26 10:24:42
标签:spring,boot,动态刷新
本文测试使用的springcloud版本为:
Dalston.SR1
很多朋友只知道springcloudconfig可以刷新远程git的配置到内存中,
却不知道springcloudconfig的客户端可以脱离服务端使用,
更不知道springcloudconfig客户端结合actuator还可以刷新本地的配置文件到内存中。
具体做法如下:
1、pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuyx</groupId>
<artifactId>test-config-refresh</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--监控+refresh配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
单独引入 spring-boot-starter-actuator或者spring-cloud-starter-config(springcloudconfig的客户端) 是不会暴露/refresh端点的,两者同时引入之后才能暴露/refresh端点。
2、一般使用spring-cloud-starter-config的文章都会让你在bootstrap里加上配置中心服务端的地址,这里我们要脱离配置中心服务端使用,所以这些配置完全不需要。
3、对需要刷新的属性使用@Value注解,同时将类使用@RefreshScope注解进行标记,示例如下:
package com.liuyx.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RefreshScope
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
private static int port;
@Value("${server.port}")
public void setPort(int port){
this.port=port;
}
@RequestMapping("/port")
public int port(){
return port;
}
}
这里我的变量是一个static变量,所以只能在非static的set方法上加@Value注解,而不是变量定义行的上方。如果不是静态变量则可以直接写作:
@Value("${server.port}")
private int port;
4、application.properties配置
server.port=80
local.test=hello1
management.security.enabled=false
5、测试
1、启动项目,访问 http://localhost/port显示 80
2、修改classpath(注意是classpath,即你编译后的class文件所处的目录)下的application.properties将server.port改为801
3、发送空post(注意是post)请求到 http://localhost:80/refresh
4、再次访问http://localhost/port显示 801测试成功
最后的补充:
即使结合配置中心服务端使用,该方法也是有效的,所有有效位置的有效配置文件(如git上的,jar内的,jar外的)都会被扫描,并根据一定的顺序进行覆盖
来源:https://www.cnblogs.com/flying607/p/8459397.html
0
投稿
猜你喜欢
- 目录断言对象、数组、集合ObjectUtilsStringUtilsCollectionUtils文件、资源、IO 流FileCopyUti
- 下标到指针之间和转换以下的程序做了什么。#include <stdio.h> int main() { int a
- 本文实例讲述了Java文本文件操作方法。分享给大家供大家参考。具体分析如下:最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了
- 记录窗口上次关闭的位置和大小namespace PDSafe.Base{ public class Se
- 当我们的idea无法自动下在所需的Maven依赖时,我们可以到Maven的远程仓库中下载所需要的jar包,然后添加到我们的本地仓库中。1.首
- 本文介绍restTemplate基础用法。Java中get和post的用法请参考:一文带你搞懂Java中Get和Post的使用1 提供get
- 本文实例为大家分享了winform可拖动的自定义Label控件,供大家参考,具体内容如下效果预览:实现步骤如下:(1)首先在项目上右击选择:
- 本文实例为大家分享了android recyclerview模拟聊天界面的具体代码,供大家参考,具体内容如下效果图:实现代码:package
- ScriptControl接口属性名称类型备注AllowUIBOOL检测是否允许运行用户的接口元素。如果为False,则诸如消息框之类的界面
- 本文为大家分享了java实现工资管理简单程序的具体代码,供大家参考,具体内容如下程序总体说明(ReadMe):总体分为四部分:管理程序(st
- 需求描述 今日需求是删除资源时同时删除与该资源绑定的角色数据,有两张表,资源表、
- jdk中自带了很多工具可以用于性能分析,位于jdk的bin目录下,jvisualvm工具可以以图形化的方式更加直观的监控本地以及远程的jav
- 在面向对象设计原则中,要求"要依赖于抽象,不要依赖于具体", 这句话有很多人搞不懂。在这里谈谈我自己的理解。首先看看以下
- springboot-dubbo cannot be cast to使用spring boot 集成dubbo的时候遇到问题:2018-05
- 1、在设计初期阶段,应该要有意识的将不同的两层分离,比如考虑数据访问层、业务逻辑层、表示层之间建立外观模式,这样可以为子系统提供简单一致的接
- 本文介绍Android中的5种数据存储方式。数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是
- @Valid:@Valid注解用于校验,所属包为:javax.validation.Valid。① 首先需要在实体类的相应字段上添加用于充当
- 第一步:图形验证码接口1.使用第三方的验证码生成工具Kaptchahttps://github.com/penggle/kaptcha@Co
- 前言Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spr
- 前言《英文猜词游戏》代码行数没有超过200行,是之前为了背英语单词,特意研发的小游戏。主要设计1.事先准备单词文本。2.为了让玩家能与程序互