nacos配置讀取實(shí)現(xiàn)過(guò)程
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:
# 調(diào)價(jià)管理
drugPriceAdjustment:
#藥品調(diào)價(jià),未審核出、入庫(kù)類型的單據(jù)是否檢查提示出來(lái) 蘄春 1
isCheck: 13.后端創(chuàng)建對(duì)應(yīng)配置類
并在啟動(dòng)時(shí)讀取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;
/**
* 藥庫(kù)管理獲取參數(shù)
* @author tancw
* @date 2025/11/17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "drugstoremanagement")
public class DrugStoreManaProperties {
// 藥品調(diào)價(jià),未審核出、入庫(kù)類型的單據(jù)是否檢查提示出來(lái) 蘄春 1
@Value("${drugPriceAdjustment.isCheck:0}")
private String isCheck;
}
上面定義配置信息字段時(shí),一定要給上默認(rèn)值,防止其它同事命名空間中沒(méi)有配置時(shí),啟動(dòng)程序讀取不到的問(wèn)題發(fā)生
4.創(chuàng)建接口返回配置信息給前端
并加注解@RefreshScope(注解實(shí)現(xiàn)配置實(shí)時(shí)刷新)
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參數(shù)配置
*/
@RefreshScope // 注解實(shí)現(xiàn)配置實(shí)時(shí)刷新
@RestController
@RequestMapping("/open/api")
@Api(tags = "接口工具")
public class OpenController extends BaseController {
@Autowired
DrugStoreManaProperties drugStoreManaProperties;
@ApiOperation("藥庫(kù)管理獲取參數(shù)")
@GetMapping("/queryDrugStoreManaProperties")
public AjaxResult queryDrugStoreManaProperties(){
return AjaxResult.success("操作成功",drugStoreManaProperties);
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SprintBoot深入淺出講解場(chǎng)景啟動(dòng)器Starter
本篇文章將和大家分享一下 Spring Boot 框架中的 Starters 場(chǎng)景啟動(dòng)器的內(nèi)容,關(guān)于 Starters 具體是用來(lái)做什么的,以及在開(kāi)發(fā) Spring Boot項(xiàng)目前,要如何自定義一個(gè) Starters 場(chǎng)景啟動(dòng)器2022-06-06
Java中函數(shù)式接口實(shí)現(xiàn)分布式鎖
本文介紹了一種使用函數(shù)式接口實(shí)現(xiàn)分布式鎖模板的方法,通過(guò)定義一個(gè)只包含一個(gè)抽象方法的接口來(lái)簡(jiǎn)化加鎖邏輯,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-02-02
Spring Cloud中關(guān)于Feign的常見(jiàn)問(wèn)題總結(jié)
這篇文章主要給大家介紹了Spring Cloud中關(guān)于Feign的常見(jiàn)問(wèn)題,文中通過(guò)示例代碼介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02
SpringBoot整合MybatisPlus的基本應(yīng)用指南
MyBatis-Plus ,簡(jiǎn)稱 MP,是一個(gè) MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,下面小編就來(lái)和大家介紹一下SpringBoot整合MybatisPlus的一些基本應(yīng)用吧2025-03-03
Java模擬HTTP Get Post請(qǐng)求 輕松實(shí)現(xiàn)校園BBS自動(dòng)回帖
這篇文章主要介紹了Java模擬HTTP Get Post請(qǐng)求,輕松實(shí)現(xiàn)校園BBS自動(dòng)回帖,感興趣的小伙伴們可以參考一下2015-12-12
Java并發(fā)編程之代碼實(shí)現(xiàn)兩玩家交換裝備
這篇文章主要介紹了Java并發(fā)編程之代碼實(shí)現(xiàn)兩玩家交換裝備,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下2021-09-09
Spring框架實(shí)現(xiàn)AOP的兩種方式詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架實(shí)現(xiàn)AOP的兩種方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的借鑒價(jià)值,需要的可以參考一下2022-09-09

