一、引入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.yamlspring.cloud.nacos.config.extension-configs[0].refresh=truespring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP#配置文件二spring.cloud.nacos.config.extension-configs[1].data-id=xxx.yamlspring.cloud.nacos.config.extension-configs[1].refresh=truespring.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@Slf4jpublic 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);
原文转载:http://www.shaoqun.com/a/489979.html
拍拍网:https://www.ikjzd.com/w/2205
环球易购:https://www.ikjzd.com/w/1034
友家快递:https://www.ikjzd.com/w/1341
一、引入pom<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.1.RELEASE</version></
cbo:cbo
vat:vat
亚马逊LISTING流程化运营体系:关于亚马逊卖家联盟(Amazon affiliate)注册操作流程全解析:亚马逊LISTING流程化运营体系:关于亚马逊卖家联盟(Amazon affiliate)注册操作流程全解析
厦门玛琪雅朵花海好玩吗?有什么好玩的啊?:厦门玛琪雅朵花海好玩吗?有什么好玩的啊?
2020年端午节高速免费吗?:2020年端午节高速免费吗?
没有评论:
发表评论