欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nacos配置中心的配置修改之后,無需重啟服務的實現(xiàn)過程

 更新時間:2025年08月08日 16:43:48   作者:敲代碼的豬豬俠  
本文介紹Nacos配置自動刷新的兩種方式:@RefreshScope注解和@ConfigurationProperties,強調(diào)需將配置寫入當前服務的配置文件以確保優(yōu)先加載和動態(tài)更新,避免重啟服務

前言

在微服務的項目中,我們經(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 使用ftpClient下載ftp上所有文件解析

    使用ftpClient下載ftp上所有文件解析

    最近項目需要寫個小功能,需求就是實時下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。今天小編給大家分享使用ftpClient下載ftp上所有文件的方法,需要的的朋友參考下吧
    2017-04-04
  • SpringBoot整合Redis的示例

    SpringBoot整合Redis的示例

    這篇文章主要介紹了SpringBoot整合Redis的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-10-10
  • spring單元測試下模擬rabbitmq的實現(xiàn)

    spring單元測試下模擬rabbitmq的實現(xiàn)

    這篇文章主要介紹了spring單元測試下模擬rabbitmq的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 詳解Spring循環(huán)依賴的解決方案

    詳解Spring循環(huán)依賴的解決方案

    這篇文章主要介紹了詳解Spring循環(huán)依賴的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解Spring Cloud Zuul中路由配置細節(jié)

    詳解Spring Cloud Zuul中路由配置細節(jié)

    本篇文章主要介紹了詳解Spring Cloud Zuul中路由配置細節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例

    springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例

    這篇文章主要介紹了springboot結(jié)合mysql主從來實現(xiàn)讀寫分離的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Spring框架實現(xiàn)文件上傳功能

    Spring框架實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細介紹了Spring框架實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 新版POI獲取日期類型cell值過程圖解

    新版POI獲取日期類型cell值過程圖解

    這篇文章主要介紹了新版POI獲取日期類型cell值過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • springboot快速整合Mybatis組件的方法(推薦)

    springboot快速整合Mybatis組件的方法(推薦)

    Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。這篇文章主要介紹了springboot快速整合Mybatis組件的方法,需要的朋友可以參考下
    2019-11-11
  • eclipse如何運行springboot項目

    eclipse如何運行springboot項目

    這篇文章主要介紹了eclipse如何運行springboot項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-05-05

最新評論