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;
static NacosBootstrapper create() {
public static NacosBootstrapper create() {
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.properties.bind.Binder;
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.LoaderInterceptor;
@ -110,8 +109,10 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
List<Option> options = new ArrayList<>();
options.add(Option.IGNORE_IMPORTS);
options.add(Option.IGNORE_PROFILES);
if (StringUtils.hasText(resource.getProfiles())) {
options.add(Option.PROFILE_SPECIFIC);
for (String profile : resource.getAcceptedProfiles()) {
if (source.getName().contains("-" + profile + ".")) {
options.add(Option.PROFILE_SPECIFIC);
}
}
return Options.of(options.toArray(new Option[0]));
});

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

Loading…
Cancel
Save