SpringBoot2集成nacos(一)

根據nacos官網的描述,集成時總是出現錯誤,讀不到nacos中的配置,用的nacos-config-spring-boot-starter版本爲0.2.3。
 

後來經過debug,發現有幾個參數是必須配置的,否則項目不會啓動成功。


    public NacosPropertySource reqNacosConfig(Properties configProperties, String dataId, String groupId, ConfigType type) {
        String config = NacosUtils.getContent(builder.apply(configProperties), dataId, groupId);
        NacosPropertySource nacosPropertySource = new NacosPropertySource(dataId, groupId,
                buildDefaultPropertySourceName(dataId, groupId, configProperties), config, type.getType());
        nacosPropertySource.setDataId(dataId);
        nacosPropertySource.setType(type.getType());
        nacosPropertySource.setGroupId(groupId);
        return nacosPropertySource;
    }

上邊是nacos的NacosConfigUtils源碼片段,

                buildDefaultPropertySourceName(dataId, groupId, configProperties), config, type.getType());

 

上邊的type是在nacos中配置的內容格式,

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0  = the "License"");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.api.config;

/**
 * @author liaochuntao
 * @date 2019-06-14 21:12
 **/
public enum ConfigType {

    /**
     * config type is "properties"
     */
    PROPERTIES("properties"),

    /**
     * config type is "xml"
     */
    XML("xml"),

    /**
     * config type is "json"
     */
    JSON("json"),

    /**
     * config type is "text"
     */
    TEXT("text"),

    /**
     * config type is "html"
     */
    HTML("html"),

    /**
     * config type is "yaml"
     */
    YAML("yaml");

    String type;

    ConfigType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

 

單獨在nacos中指定類型,在運行時還是會報空指針異常,因此還需要在配置nacos時指定:nacos.config.type

另外還需要配置的是nacos.config bootstrap.enable=true或nacos.config bootstrap.log.enable=true,這兩個配置必須指定其一。因爲在NacosConfigApplicationContextInitializer的initialize方法中會調用enable()方法中判斷這兩個配置項是否其一爲true。

public class NacosConfigApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

  

@Override
    public void initialize(ConfigurableApplicationContext context) {
        CacheableEventPublishingNacosServiceFactory singleton = CacheableEventPublishingNacosServiceFactory.getSingleton();
        singleton.setApplicationContext(context);
        environment = context.getEnvironment();
        if (!enable()) {
            logger.info("[Nacos Config Boot] : The preload configuration is not enabled");
        } else {
            Function<Properties, ConfigService> builder = properties -> {
                try {
                    return singleton.createConfigService(properties);
                } catch (
                        NacosException e) {
                    throw new RuntimeException("ConfigService can't be created with properties : " + properties, e);
                }
            };
            nacosConfigProperties = NacosConfigPropertiesUtils.buildNacosConfigProperties(environment);
            NacosConfigUtils configUtils = new NacosConfigUtils(nacosConfigProperties, environment, builder);

            if (processor.enable(environment)) {
                configUtils.addListenerIfAutoRefreshed(processor.getDeferPropertySources());
            } else {
                configUtils.loadConfig(false);
                configUtils.addListenerIfAutoRefreshed();
            }

        }

    }


//其他代碼省略...
    
private boolean enable() {
        return processor.enable(environment) || Boolean.parseBoolean(environment.getProperty(NacosConfigConstants.NACOS_BOOTSTRAP, "false"));
    }

}

 SpringBoot2集成nacos的完整配置文件如下:

nacos:
  config:
    type: yaml
    server-addr: 192.168.111.111:8848
    namespace: 32a9fc67-5fc9-47a7-947b-863364d93a88
    context-path: nacos
    data-id: the-dataid
    auto-refresh: true
    group: the-group
    bootstrap:
      enable: true
      log:
        enable: true

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章