溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring boot配置動態(tài)刷新的示例分析

發(fā)布時間:2021-09-09 13:36:07 來源:億速云 閱讀:283 作者:小新 欄目:開發(fā)技術

這篇文章給大家分享的是有關spring boot配置動態(tài)刷新的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

具體做法如下:

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>
        <!--監(jiān)控+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注解,而不是變量定義行的上方。如果不是靜態(tài)變量則可以直接寫作:

@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配置動態(tài)刷新的示例分析

3、發(fā)送空post(注意是post)請求到 http://localhost:80/refresh

spring boot配置動態(tài)刷新的示例分析

4、再次訪問http://localhost/port顯示 801測試成功

最后的補充:

即使結合配置中心服務端使用,該方法也是有效的,所有有效位置的有效配置文件(如git上的,jar內的,jar外的)都會被掃描,并根據(jù)一定的順序進行覆蓋

感謝各位的閱讀!關于“spring boot配置動態(tài)刷新的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI