Use NacosConfigManager to create ConfigService.

Modify whether is PROFILE_SPECIFIC logic.
Make NacosBootstrapper method public.
pull/2349/head
Freeman Lau 3 years ago
parent 21e22e0696
commit 769bced209

@ -40,7 +40,7 @@ public class NacosBootstrapper implements BootstrapRegistryInitializer {
private LoaderInterceptor loaderInterceptor; private LoaderInterceptor loaderInterceptor;
static NacosBootstrapper create() { public static NacosBootstrapper create() {
return new NacosBootstrapper(); return new NacosBootstrapper();
} }

@ -38,7 +38,6 @@ import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
import static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoadContext; import static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoadContext;
import static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoaderInterceptor; import static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoaderInterceptor;
@ -110,9 +109,11 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
List<Option> options = new ArrayList<>(); List<Option> options = new ArrayList<>();
options.add(Option.IGNORE_IMPORTS); options.add(Option.IGNORE_IMPORTS);
options.add(Option.IGNORE_PROFILES); options.add(Option.IGNORE_PROFILES);
if (StringUtils.hasText(resource.getProfiles())) { for (String profile : resource.getAcceptedProfiles()) {
if (source.getName().contains("-" + profile + ".")) {
options.add(Option.PROFILE_SPECIFIC); options.add(Option.PROFILE_SPECIFIC);
} }
}
return Options.of(options.toArray(new Option[0])); return Options.of(options.toArray(new Option[0]));
}); });
} }

@ -20,10 +20,9 @@ import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -173,12 +172,10 @@ public class NacosConfigDataLocationResolver
private void registerConfigService(NacosConfigProperties properties, private void registerConfigService(NacosConfigProperties properties,
ConfigurableBootstrapContext bootstrapContext) { ConfigurableBootstrapContext bootstrapContext) {
try {
if (!bootstrapContext.isRegistered(ConfigService.class)) { if (!bootstrapContext.isRegistered(ConfigService.class)) {
ConfigService configService = ConfigFactory.createConfigService(properties.assembleConfigServiceProperties()); Optional.ofNullable(new NacosConfigManager(properties).getConfigService())
bootstrapContext.register(ConfigService.class, InstanceSupplier.of(configService)); .ifPresent(configService -> bootstrapContext.register(
} ConfigService.class, InstanceSupplier.of(configService)));
} catch (NacosException ignore) {
} }
} }

Loading…
Cancel
Save