nacos配置热更新

什么是配置热更新?

配置热更新就是说,如果我在nacos控制台修改了配置文件那么就会直接更新到微服务之中,不需要重新启动微服务。

如何配置?如何使用?

能够读取到nacos的配置之后,只需要在代码中读取配置即可监听到,读取的方式一般有两种:

方式一:

使用@RefreshScope@Value实现Nacos配置热更新

# 应用名称
spring:
  application:
    name: test-config

  # Nacos配置中心配置
  cloud:
    nacos:
      # 配置文件后缀
      config:
        file-extension: yaml
        # 是否启用配置刷新
        refresh-enabled: true
        # 是否启用配置管理
        enabled: true
        # Nacos服务器地址
        server-addr: 127.0.0.1:8848
        # 配置分组
        group: DEFAULT_GROUP
        # 扩展配置列表
        extension-configs[0]:
          data-id: config-student.yaml
          group: DEFAULT_GROUP
          refresh: true
        extension-configs[1]:
          data-id: config-teacher.yaml
          group: DEFAULT_GROUP
          refresh: true

      # Nacos服务发现配置
      discovery:
        server-addr: 127.0.0.1:8848
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * 使用@RefreshScope注解的Bean,当配置更新时,会自动刷新。
 */
@Component
@RefreshScope
public class TestConfig {

    /**
     * 使用@Value注解注入配置项,这里的配置项来自Nacos。
     * 当Nacos中的配置更新后,可以通过发送POST请求到/actuator/refresh端点来触发刷新操作。
     */
    @Value("${teacher.name}")
    private String teacherName;

    /**
     * 使用@Value注解注入配置项。
     */
    @Value("${teacher.age}")
    private Integer teacherAge;

    // Getter和Setter方法
    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public Integer getTeacherAge() {
        return teacherAge;
    }

    public void setTeacherAge(Integer teacherAge) {
        this.teacherAge = teacherAge;
    }
}

方式二:

@ConfigurationProperties注解来指定前缀,并绑定Nacos中的配置项。

# 应用名称
spring:
  application:
    name: test-config

  # Nacos配置中心配置
  cloud:
    nacos:
      # 配置文件后缀
      config:
        file-extension: yaml
        # 是否启用配置刷新
        refresh-enabled: true
        # 是否启用配置管理
        enabled: true
        # Nacos服务器地址
        server-addr: 127.0.0.1:8848
        # 配置分组
        group: DEFAULT_GROUP
        # 配置文件的data-id,与@ConfigurationProperties中的prefix对应
        data-id: myapp.yaml
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 使用@ConfigurationProperties注解来绑定Nacos中的配置项。
 * prefix指定了配置项的前缀,与Nacos中data-id的名称相对应。
 */
@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {

    /**
     * 定义属性与Nacos配置中的属性对应。
     */
    private String name;
    private Integer age;

    // Getter和Setter方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值