1. 程式人生 > 實用技巧 >SpringBoot整合nacos示例

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);