nacos配置讀取實(shí)現(xiàn)過程
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)價管理
drugPriceAdjustment:
#藥品調(diào)價,未審核出、入庫類型的單據(jù)是否檢查提示出來 蘄春 1
isCheck: 13.后端創(chuàng)建對應(yīng)配置類
并在啟動時讀取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;
/**
* 藥庫管理獲取參數(shù)
* @author tancw
* @date 2025/11/17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "drugstoremanagement")
public class DrugStoreManaProperties {
// 藥品調(diào)價,未審核出、入庫類型的單據(jù)是否檢查提示出來 蘄春 1
@Value("${drugPriceAdjustment.isCheck:0}")
private String isCheck;
}
上面定義配置信息字段時,一定要給上默認(rèn)值,防止其它同事命名空間中沒有配置時,啟動程序讀取不到的問題發(fā)生
4.創(chuàng)建接口返回配置信息給前端
并加注解@RefreshScope(注解實(shí)現(xiàn)配置實(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í)時刷新
@RestController
@RequestMapping("/open/api")
@Api(tags = "接口工具")
public class OpenController extends BaseController {
@Autowired
DrugStoreManaProperties drugStoreManaProperties;
@ApiOperation("藥庫管理獲取參數(shù)")
@GetMapping("/queryDrugStoreManaProperties")
public AjaxResult queryDrugStoreManaProperties(){
return AjaxResult.success("操作成功",drugStoreManaProperties);
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java聊天室之實(shí)現(xiàn)獲取Socket功能
這篇文章主要為大家詳細(xì)介紹了Java簡易聊天室之實(shí)現(xiàn)獲取遠(yuǎn)程服務(wù)器和客戶機(jī)的IP地址和端口號功能,文中的示例代碼講解詳細(xì),需要的可以了解一下2022-10-10
Java Eclipse進(jìn)行斷點(diǎn)調(diào)試的方法
本篇文章主要介紹了Java Eclipse進(jìn)行斷點(diǎn)調(diào)試的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Maven修改運(yùn)行環(huán)境配置代碼實(shí)例
這篇文章主要介紹了Maven修改運(yùn)行環(huán)境配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
SpringBoot實(shí)現(xiàn)文件記錄日志及日志文件自動歸檔和壓縮
Logback是Java日志框架,通過Logger收集日志并經(jīng)Appender輸出至控制臺、文件等,SpringBoot配置logback-spring.xml可實(shí)現(xiàn)日志文件存儲,本文給大家介紹了SpringBoot實(shí)現(xiàn)文件記錄日志及日志文件自動歸檔和壓縮,需要的朋友可以參考下2025-05-05
java實(shí)現(xiàn)KFC點(diǎn)餐小程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Spring Boot攔截器Interceptor與過濾器Filter詳細(xì)教程(示例詳解)
本文詳細(xì)介紹了SpringBoot中的攔截器(Interceptor)和過濾器(Filter),包括它們的定義、作用范圍、使用場景、實(shí)現(xiàn)步驟、執(zhí)行順序、常見問題及解決方案,感興趣的朋友跟隨小編一起看看吧2025-03-03
java雙向循環(huán)鏈表的實(shí)現(xiàn)代碼
這篇文章介紹了java雙向循環(huán)鏈表的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-09-09

