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



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


想要實現(xiàn)動態(tài)刷新無需重啟服務,來加載最新的配置信息:
在需要動態(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)刷新

微服務項目要特別注意一下,在服務啟動的時候加載配置文件是有一個優(yōu)先級的,優(yōu)先加載本服務所對應的配置文件,后加載公共的配置文件,要實現(xiàn)配置自動刷新的情況,需要把配置信息寫到當前服務所對應的配置文件中?。。?nbsp;
總結(jié)
本篇文章介紹了實現(xiàn)Nacos屬性值自動刷新的方式:使用@RefreshScope注解、@ConfigurationProperties。
通過這些方式,您可以在應用程序運行時動態(tài)刷新Nacos上的屬性值,避免了重啟應用的麻煩。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring單元測試下模擬rabbitmq的實現(xiàn)
這篇文章主要介紹了spring單元測試下模擬rabbitmq的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
詳解Spring Cloud Zuul中路由配置細節(jié)
本篇文章主要介紹了詳解Spring Cloud Zuul中路由配置細節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例
這篇文章主要介紹了springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
springboot快速整合Mybatis組件的方法(推薦)
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。這篇文章主要介紹了springboot快速整合Mybatis組件的方法,需要的朋友可以參考下2019-11-11

