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: 1

3.后端创建对应配置类

并在启动时读取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);
    }

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SprintBoot深入浅出讲解场景启动器Starter

    SprintBoot深入浅出讲解场景启动器Starter

    本篇文章将和大家分享一下 Spring Boot 框架中的 Starters 场景启动器的内容,关于 Starters 具体是用来做什么的,以及在开发 Spring Boot项目前,要如何自定义一个 Starters 场景启动器
    2022-06-06
  • Java中函数式接口实现分布式锁

    Java中函数式接口实现分布式锁

    本文介绍了一种使用函数式接口实现分布式锁模板的方法,通过定义一个只包含一个抽象方法的接口来简化加锁逻辑,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2026-02-02
  • Spring Cloud中关于Feign的常见问题总结

    Spring Cloud中关于Feign的常见问题总结

    这篇文章主要给大家介绍了Spring Cloud中关于Feign的常见问题,文中通过示例代码介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • SpringBoot整合MybatisPlus的基本应用指南

    SpringBoot整合MybatisPlus的基本应用指南

    MyBatis-Plus ,简称 MP,是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,下面小编就来和大家介绍一下SpringBoot整合MybatisPlus的一些基本应用吧
    2025-03-03
  • Mac下设置Java默认版本的方法

    Mac下设置Java默认版本的方法

    今天工作的时候发现了一个错误,提示java版本太低,无法启动!想起自己装过高版本的Java,但是却没有默认启动,从网上找了一些资料,整理下现在分享给大家,有需要的可以参考借鉴。
    2016-10-10
  • Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖

    Java模拟HTTP Get Post请求 轻松实现校园BBS自动回帖

    这篇文章主要介绍了Java模拟HTTP Get Post请求,轻松实现校园BBS自动回帖,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Java中数组的定义和使用教程(二)

    Java中数组的定义和使用教程(二)

    这篇文章主要给大家介绍了关于Java中数组的定义和使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java中捕获线程异常的几种方式总结

    Java中捕获线程异常的几种方式总结

    这篇文章主要介绍了Java中捕获线程异常的几种方式总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • Java并发编程之代码实现两玩家交换装备

    Java并发编程之代码实现两玩家交换装备

    这篇文章主要介绍了Java并发编程之代码实现两玩家交换装备,文中有非常详细的代码示例,对正在学习java的小伙伴们有一定的帮助,需要的朋友可以参考下
    2021-09-09
  • Spring框架实现AOP的两种方式详解

    Spring框架实现AOP的两种方式详解

    这篇文章主要为大家详细介绍了Spring框架实现AOP的两种方式,文中的示例代码讲解详细,对我们学习有一定的借鉴价值,需要的可以参考一下
    2022-09-09

最新评论