nacos配置读取实现过程
更新时间:2026年01月04日 09:05:06 作者:TTc_
本文介绍了在Spring Boot应用中如何指定和使用配置文件,创建配置类读取Nacos配置,并通过接口返回给前端,同时使用@RefreshScope实现配置实时刷新
1.application中指定配置文件tbyf-public-config.yml
server:
port: 19120
spring:
application:
name: @artifactId@
cloud:
nacos:
username: @nacos.username@
password: @nacos.password@
discovery:
server-addr: ${NACOS_HOST:tbyf-cloud-register}:${NACOS_PORT:8848}
namespace: ${profiles.active}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
namespace: ${profiles.active}
config:
import:
- nacos:application.yml
- nacos:${spring.application.name}.yml
- nacos:tbyf-public-config.yml
2.tbyf-public-config.yml中添加配置信息
drugstoremanagement:
# 调价管理
drugPriceAdjustment:
#药品调价,未审核出、入库类型的单据是否检查提示出来 蕲春 1
isCheck: 13.后端创建对应配置类
并在启动时读取nacos中的配置
package com.tbyf.system.properties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 药库管理获取参数
* @author tancw
* @date 2025/11/17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "drugstoremanagement")
public class DrugStoreManaProperties {
// 药品调价,未审核出、入库类型的单据是否检查提示出来 蕲春 1
@Value("${drugPriceAdjustment.isCheck:0}")
private String isCheck;
}
上面定义配置信息字段时,一定要给上默认值,防止其它同事命名空间中没有配置时,启动程序读取不到的问题发生
4.创建接口返回配置信息给前端
并加注解@RefreshScope(注解实现配置实时刷新)
package com.tbyf.system.controller;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tbyf.common.core.domain.R;
import com.tbyf.common.core.utils.HttpUtils;
import com.tbyf.common.core.utils.StringUtils;
import com.tbyf.common.core.utils.sign.Base64;
import com.tbyf.common.core.web.controller.BaseController;
import com.tbyf.common.core.web.domain.AjaxResult;
import com.tbyf.system.api.domain.ApiconvertBaseInfoModel;
import com.tbyf.system.domain.ApiconvertBaseinfo;
import com.tbyf.system.domain.ExecutSqlsInfo;
import com.tbyf.system.domain.vo.FieldMapping;
import com.tbyf.system.mapper.EmrDataMapper;
import com.tbyf.system.properties.DrugStoreManaProperties;
import com.tbyf.system.properties.InpatiWrokBeachProperties;
import com.tbyf.system.properties.OutpatientDoctorProperties;
import com.tbyf.system.properties.ReportUrlConfig;
import com.tbyf.system.service.IApiconvertBaseinfoService;
import com.tbyf.system.service.IFieldMappingService;
import com.tbyf.system.service.ISysDatasourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 读取nacos参数配置
*/
@RefreshScope // 注解实现配置实时刷新
@RestController
@RequestMapping("/open/api")
@Api(tags = "接口工具")
public class OpenController extends BaseController {
@Autowired
DrugStoreManaProperties drugStoreManaProperties;
@ApiOperation("药库管理获取参数")
@GetMapping("/queryDrugStoreManaProperties")
public AjaxResult queryDrugStoreManaProperties(){
return AjaxResult.success("操作成功",drugStoreManaProperties);
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot实现文件记录日志及日志文件自动归档和压缩
Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logback-spring.xml可实现日志文件存储,本文给大家介绍了SpringBoot实现文件记录日志及日志文件自动归档和压缩,需要的朋友可以参考下2025-05-05
Spring Boot拦截器Interceptor与过滤器Filter详细教程(示例详解)
本文详细介绍了SpringBoot中的拦截器(Interceptor)和过滤器(Filter),包括它们的定义、作用范围、使用场景、实现步骤、执行顺序、常见问题及解决方案,感兴趣的朋友跟随小编一起看看吧2025-03-03


最新评论