SpringBoot集成nacos示例
一、引入pom
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency>
二、resource目录下添加bootstrap.properties
server.port=8080
#命名空间
spring.cloud.nacos.config.namespace=xxx
#nacos地址
spring.cloud.nacos.config.server-addr=x.x.x.x:x
#配置文件一
spring.cloud.nacos.config.extension-configs[0].data-id=xxx.yaml
spring.cloud.nacos.config.extension-configs[0].refresh=true
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
#配置文件二
spring.cloud.nacos.config.extension-configs[1].data-id=xxx.yaml
spring.cloud.nacos.config.extension-configs[1].refresh=true
spring.cloud.nacos.config.extension-configs[1].group=DEFAULT_GROUP
三、读取nacos配置文件的工具类
import com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder; import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator; import com.alibaba.nacos.api.exception.NacosException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.stereotype.Component; import java.lang.reflect.Field; import java.util.List; @Component @Slf4j public class NacosConfig { @Autowired private PropertySourceBootstrapConfiguration propertySourceBootstrapConfiguration; public String getData(String group,String dataId,long timeout){ try { Field propertySourceLocators = PropertySourceBootstrapConfiguration.class.getDeclaredField("propertySourceLocators"); propertySourceLocators.setAccessible(true); List<PropertySourceLocator> list = (List<PropertySourceLocator>)propertySourceLocators.get(propertySourceBootstrapConfiguration); NacosPropertySourceLocator nacosLocator = null; for(PropertySourceLocator locator : list){ if(locator instanceof NacosPropertySourceLocator){ nacosLocator = (NacosPropertySourceLocator)locator; } } if(nacosLocator == null){ return null; } Field nacosPropertySourceBuilderField = NacosPropertySourceLocator.class.getDeclaredField("nacosPropertySourceBuilder"); nacosPropertySourceBuilderField.setAccessible(true); NacosPropertySourceBuilder nacosPropertySourceBuilder = (NacosPropertySourceBuilder)nacosPropertySourceBuilderField.get(nacosLocator); String config = nacosPropertySourceBuilder.getConfigService().getConfig(dataId, group, timeout); return config; } catch (NoSuchFieldException | IllegalAccessException | NacosException e) { log.error("nacos工具异常",e); } return null; } }
四、在需要用到的地方注入
@Autowired private NacosConfig nacosConfig; nacosConfig.getData("DEFAULT_GROUP", "xxx.yaml", 30000);
本文作者:zydjjcpdszylddpll
本文链接:https://www.cnblogs.com/jyfs/p/13952047.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步