开发者

nacos配置中心的配置修改之后,无需重启服务的实现过程

目录
  • 前言
  • 方式一:使用@RefreshScope注解
  • 方式二:使用@ConfigurationProperties
  • 总结

前言

在微服务的项目中,我们经常使用Nacos作为配置中心,用于管理应用程序的属性配置。当我们在Nacos上修改属性值时,希望应用程序能够自动刷新并应用最新的属性值,以避免重启应用。

本篇文章将介绍Nacos属性值自动刷新的方式,并提供相应的示例代码:

项目中引入相关依赖

<!--nacos config-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

方式一:使用@RefreshScope注解

@RefreshScope注解是Spring Cloud提供的一种属性刷新机制。

它可以应用于需要动态刷新的配置类或方法上,当Nacos上的属性值发生变化时,通过调用/actuator/refresh端点来刷新被注解的类或方法

nacos配置中心的配置修改之后,无需重启服务的实现过程

nacos配置中心的配置修改之后,无需重启服务的实现过程

nacos配置中心的配置修改之后,无需重启服务的实现过程

这个时候是获取到配置文件信息,但是当你修改完配置,只能通过重启服务才能获取到最新的配置信息

nacos配置中心的配置修改之后,无需重启服务的实现过程

nacos配置中心的配置修改之后,无需重启服务的实现过程

想要实现动态刷新无需重启服务,来加载最新的配置信息:

在需要动态刷新的配置类或方法上添加@RefreshScope注解

import com.supervise.common.core.web.controller.BaseController;
import com.supervise.common.core.web.domain.AJAXResult;
import org.springframework.beans.facjavascripttory.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 javascriptBaseController {

    //测试
    @Value("${sys.test.name}")
    private String name;

    /**
     * 测试自动刷新配置
     */
    @GetMapping(value = "/onTrial")
    public AjaxResult onTrial() {

        return success(name);
    }

}

配置文件改成:张三888,看结果,实现了配置文件动态刷新

nacos配置中心的配置修改之后,无需重启服务的实现过程

nacos配置中心的配置修改之后,无需重启服务的实现过程

创建配置类:

@Data
@RefreshScope //自动刷新bean配置
@Component
public class TestConfig {

    //测编程客栈试
    @Value("${sys.test.name}")
    private String name;

}

nacos配置中心的配置修改之后,无需重启服务的实现过程

配置文件改成:张三666,看结果,这样也是实现了配置文件动态刷新

nacos配置中心的配置修改之后,无需重启服务的实现过程

方式二:使用@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.contexRWSlQVpkNAt.properties.ConfigurationPropertiepythons;
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,看结果,这样也是能实现了配置文件动态刷新

nacos配置中心的配置修改之后,无需重启服务的实现过程

微服务项目要特别注意一下,在服务启动的时候加载配置文件是有一个优先级的,优先加载本服务所对应的配置文件,后加载公共的配置文件,要实现配置自动刷新的情况,需要把配置信息写到当前服务所对应的配置文件中!!! 

总结

本篇文章介绍了实现Nacos属性值自动刷新的方式:使用@RefreshScope注解、@ConfigurationProperties。

通过这些方式,您可以在应用程序运行时动态刷新Nacos上的属性值,避免了重启应用的麻烦。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜