spring boot配置动态刷新的示例分析

发布时间:2021-09-09 13:36:07 作者:小新
来源:亿速云 阅读:266

这篇文章给大家分享的是有关spring boot配置动态刷新的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体做法如下:

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

spring boot配置动态刷新的示例分析

3、发送空post(注意是post)请求到 http://localhost:80/refresh

spring boot配置动态刷新的示例分析

4、再次访问http://localhost/port显示 801测试成功

最后的补充:

即使结合配置中心服务端使用,该方法也是有效的,所有有效位置的有效配置文件(如git上的,jar内的,jar外的)都会被扫描,并根据一定的顺序进行覆盖

感谢各位的阅读!关于“spring boot配置动态刷新的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Spring Cloud搭建Config的操作步骤
  2. Spring Cloud Zuul路由规则动态更新的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:linux下gitlab搭建的全过程

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》