spring boot 配置動(dòng)態(tài)刷新實(shí)現(xiàn)詳解
本文測(cè)試使用的spring cloud版本為:
Dalston.SR1
很多朋友只知道spring cloud config可以刷新遠(yuǎn)程git的配置到內(nèi)存中,
卻不知道spring cloud config的客戶(hù)端可以脫離服務(wù)端使用,
更不知道spring cloud config客戶(hù)端結(jié)合actuator還可以刷新本地的配置文件到內(nè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>
單獨(dú)引入 spring-boot-starter-actuator或者spring-cloud-starter-config(spring cloud config的客戶(hù)端) 是不會(huì)暴露/refresh端點(diǎn)的,兩者同時(shí)引入之后才能暴露/refresh端點(diǎn)。
2、一般使用spring-cloud-starter-config的文章都會(huì)讓你在bootstrap里加上配置中心服務(wù)端的地址,這里我們要脫離配置中心服務(wù)端使用,所以這些配置完全不需要。
3、對(duì)需要刷新的屬性使用@Value注解,同時(shí)將類(lèi)使用@RefreshScope注解進(jìn)行標(biāo)記,示例如下:
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;
}
}
這里我的變量是一個(gè)static變量,所以只能在非static的set方法上加@Value注解,而不是變量定義行的上方。如果不是靜態(tài)變量則可以直接寫(xiě)作:
@Value("${server.port}")
private int port;
4、application.properties配置
server.port=80 local.test=hello1 management.security.enabled=false
5、測(cè)試
1、啟動(dòng)項(xiàng)目,訪(fǎng)問(wèn) http://localhost/port 顯示 80
2、修改classpath(注意是classpath,即你編譯后的class文件所處的目錄)下的application.properties將server.port改為801

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

4、再次訪(fǎng)問(wèn) http://localhost/port 顯示 801 測(cè)試成功
最后的補(bǔ)充:
即使結(jié)合配置中心服務(wù)端使用,該方法也是有效的,所有有效位置的有效配置文件(如git上的,jar內(nèi)的,jar外的)都會(huì)被掃描,并根據(jù)一定的順序進(jìn)行覆蓋
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot整合郵件發(fā)送與注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于Spring Boot整合郵件發(fā)送與注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Java基礎(chǔ)學(xué)習(xí)之ArrayList類(lèi)概述與常用方法
這篇文章主要為大家簡(jiǎn)單的介紹Java中ArrayList類(lèi)的概述、常用方法及存儲(chǔ)字符串并遍歷,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(30)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
Java多線(xiàn)程編程之使用Exchanger數(shù)據(jù)交換實(shí)例
這篇文章主要介紹了Java多線(xiàn)程編程之使用Exchanger數(shù)據(jù)交換實(shí)例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-05-05
redis?redisTemplate數(shù)據(jù)類(lèi)型基礎(chǔ)操作
這篇文章主要介紹了redis?redisTemplate數(shù)據(jù)類(lèi)型基礎(chǔ)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Java調(diào)用參數(shù)類(lèi)型是application/x-www-form-urlencoded的API問(wèn)題
在使用Postman進(jìn)行接口測(cè)試時(shí),對(duì)于POST請(qǐng)求,需將請(qǐng)求頭設(shè)置為application/x-www-form-urlencoded,并將參數(shù)轉(zhuǎn)為String類(lèi)型,通常在GET請(qǐng)求中,參數(shù)直接拼接在URL后,本文通過(guò)具體實(shí)例,詳細(xì)講解了參數(shù)處理的方法,適合API開(kāi)發(fā)者參考2024-09-09

