2020-11-13 13:42阅读: 495评论: 0推荐: 0

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 中国大陆许可协议进行许可。

posted @   zydjjcpdszylddpll  阅读(495)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起