nacos配置中心的配置修改之后,無需重啟服務(wù)的實現(xiàn)過程
前言
在微服務(wù)的項目中,我們經(jīng)常使用Nacos作為配置中心,用于管理應(yīng)用程序的屬性配置。當(dāng)我們在Nacos上修改屬性值時,希望應(yīng)用程序能夠自動刷新并應(yīng)用最新的屬性值,以避免重啟應(yīng)用。
本篇文章將介紹Nacos屬性值自動刷新的方式,并提供相應(yīng)的示例代碼:
項目中引入相關(guān)依賴
<!--nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>方式一:使用@RefreshScope注解
@RefreshScope注解是Spring Cloud提供的一種屬性刷新機(jī)制。
它可以應(yīng)用于需要動態(tài)刷新的配置類或方法上,當(dāng)Nacos上的屬性值發(fā)生變化時,通過調(diào)用/actuator/refresh端點來刷新被注解的類或方法



這個時候是獲取到配置文件信息,但是當(dāng)你修改完配置,只能通過重啟服務(wù)才能獲取到最新的配置信息


想要實現(xiàn)動態(tài)刷新無需重啟服務(wù),來加載最新的配置信息:
在需要動態(tài)刷新的配置類或方法上添加@RefreshScope注解
import com.supervise.common.core.web.controller.BaseController;
import com.supervise.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@RefreshScope //自動刷新bean配置
public class TestController extends BaseController {
//測試
@Value("${sys.test.name}")
private String name;
/**
* 測試自動刷新配置
*/
@GetMapping(value = "/onTrial")
public AjaxResult onTrial() {
return success(name);
}
}配置文件改成:張三888,看結(jié)果,實現(xiàn)了配置文件動態(tài)刷新


創(chuàng)建配置類:
@Data
@RefreshScope //自動刷新bean配置
@Component
public class TestConfig {
//測試
@Value("${sys.test.name}")
private String name;
}

配置文件改成:張三666,看結(jié)果,這樣也是實現(xiàn)了配置文件動態(tài)刷新

方式二:使用@ConfigurationProperties
import com.supervise.common.core.web.controller.BaseController;
import com.supervise.common.core.web.domain.AjaxResult;
import com.supervise.supervision.config.TestConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
//@RefreshScope //自動刷新bean配置
public class TestController extends BaseController {
//測試
@Value("${sys.test.name}")
private String name;
@Resource
private TestConfig testConfig;
/**
* 測試自動刷新配置
*/
@GetMapping(value = "/onTrial")
public AjaxResult onTrial() {
return success(testConfig.getName());
}
}package com.supervise.supervision.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = "sys.test") //只要前綴名和變量名兩者拼接與配置配置文件一致,就能完成屬性的自動注入
@Component
public class TestConfig {
private String name;
}配置文件改成:張三999,看結(jié)果,這樣也是能實現(xiàn)了配置文件動態(tài)刷新

微服務(wù)項目要特別注意一下,在服務(wù)啟動的時候加載配置文件是有一個優(yōu)先級的,優(yōu)先加載本服務(wù)所對應(yīng)的配置文件,后加載公共的配置文件,要實現(xiàn)配置自動刷新的情況,需要把配置信息寫到當(dāng)前服務(wù)所對應(yīng)的配置文件中?。?!
總結(jié)
本篇文章介紹了實現(xiàn)Nacos屬性值自動刷新的方式:使用@RefreshScope注解、@ConfigurationProperties。
通過這些方式,您可以在應(yīng)用程序運行時動態(tài)刷新Nacos上的屬性值,避免了重啟應(yīng)用的麻煩。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot接口正確接收時間參數(shù)的幾種方式
這篇文章主要給大家介紹了關(guān)于SpringBoot接口正確接收時間參數(shù)的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用springboot具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
SpringBoot利用限速器RateLimiter實現(xiàn)單機(jī)限流的示例代碼
本文主要介紹了SpringBoot利用限速器RateLimiter實現(xiàn)單機(jī)限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
Springboot使用SPI注冊bean到spring容器的示例代碼
這篇文章主要介紹了Springboot使用SPI注冊bean到spring容器,主要包括mydriver接口,mysqldriver實現(xiàn)過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
Springboot自動配置原理及DataSource的應(yīng)用方式
這篇文章主要介紹了Springboot自動配置原理及DataSource的應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot項目組引入JMeter的實現(xiàn)步驟
本文主要介紹了springboot項目組引入JMeter的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

