Springcloud 如何读取热更新的Nacos配置

103 阅读1分钟

编写代码读取动态更新的Nacos 配置

package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RefreshScope
public class NacosConfigController {

    @Value("${config.appName}")
    private String appName;

    // 商品信息
    @RequestMapping("/nacos/config")
    public String getConfig() {
        return appName;
    }
}

image.png

注意:读取配置的格式外面要加 ${}

上面的配置变量对应的配置为下图红框中所标识的:

image.png

相关阅读: Nacos 配置共享