nacos配置读取实现过程
作者: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);
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
