一、Nacos配置更新不生效

问题描述

在代码调适过程中,在Nacos中修改了文件上传的地址,但是在文件上传服务中读取出来的配置文件一直不会刷新,重启Nacos之后,修改之后的配置才生效

#文件上传配置
file:
  # 文件上传目录
  uploadFolder: E:/upload/

解决方法

我们需要在取此配置的代码处,加入注解@RefreshScope即可,如下:

package com.framework.pie.admin.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

@Configuration
@RefreshScope
public class UploadFileConfig {

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    public String getUploadFolder() {
        if (uploadFolder == null){
            File path = null;
            try {
                path = new File(ResourceUtils.getURL("classpath:").getPath());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if(!path.exists()) path = new File("");{
               uploadFolder = path.getAbsolutePath();
            }
        }
        return uploadFolder;
    }
}

你可能感兴趣的:(一、Nacos配置更新不生效)