Downgrade boot version.

Temporarily remove the extension class and add it when the boot version is greater than 2.4.5.
pull/2349/head
Freeman Lau 3 years ago
parent 769bced209
commit e3ce3ee92d

@ -41,8 +41,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<!-- make compatible -->
<version>2.4.5</version>
<optional>true</optional>
</dependency>

@ -1,120 +0,0 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* 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
*
* https://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.cloud.nacos.configdata;
import java.util.function.BiFunction;
import java.util.function.Function;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.util.Assert;
/**
* Spring boot >= 2.4.5 supports {@link BootstrapRegistryInitializer}.
*
* @author freeman
*/
public class NacosBootstrapper implements BootstrapRegistryInitializer {
private Function<BootstrapContext, ConfigService> configServiceFactory;
private LoaderInterceptor loaderInterceptor;
public static NacosBootstrapper create() {
return new NacosBootstrapper();
}
public NacosBootstrapper withConfigServiceFactory(
Function<BootstrapContext, ConfigService> configServiceFactory) {
this.configServiceFactory = configServiceFactory;
return this;
}
public NacosBootstrapper withLoaderInterceptor(LoaderInterceptor loaderInterceptor) {
this.loaderInterceptor = loaderInterceptor;
return this;
}
@Override
public void initialize(BootstrapRegistry registry) {
if (configServiceFactory != null) {
registry.register(ConfigService.class, configServiceFactory::apply);
}
if (loaderInterceptor != null) {
registry.register(LoaderInterceptor.class,
InstanceSupplier.of(loaderInterceptor));
}
}
public interface LoaderInterceptor extends Function<LoadContext, ConfigData> {
}
@FunctionalInterface
public interface LoaderInvocation extends
BiFunction<ConfigDataLoaderContext, NacosConfigDataResource, ConfigData> {
}
public static class LoadContext {
private final ConfigDataLoaderContext loaderContext;
private final NacosConfigDataResource resource;
private final Binder binder;
private final LoaderInvocation invocation;
LoadContext(ConfigDataLoaderContext loaderContext,
NacosConfigDataResource resource, Binder binder,
LoaderInvocation invocation) {
Assert.notNull(loaderContext, "loaderContext may not be null");
Assert.notNull(resource, "resource may not be null");
Assert.notNull(binder, "binder may not be null");
Assert.notNull(invocation, "invocation may not be null");
this.loaderContext = loaderContext;
this.resource = resource;
this.binder = binder;
this.invocation = invocation;
}
public ConfigDataLoaderContext getLoaderContext() {
return this.loaderContext;
}
public NacosConfigDataResource getResource() {
return this.resource;
}
public Binder getBinder() {
return this.binder;
}
public LoaderInvocation getInvocation() {
return this.invocation;
}
}
}

@ -17,9 +17,7 @@
package com.alibaba.cloud.nacos.configdata;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import com.alibaba.cloud.nacos.NacosConfigProperties;
@ -31,16 +29,11 @@ import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.logging.Log;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigData.Option;
import org.springframework.boot.context.config.ConfigData.Options;
import org.springframework.boot.context.config.ConfigDataLoader;
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 static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoadContext;
import static com.alibaba.cloud.nacos.configdata.NacosBootstrapper.LoaderInterceptor;
import static com.alibaba.cloud.nacos.configdata.NacosConfigDataResource.NacosItemConfig;
/**
@ -52,8 +45,6 @@ import static com.alibaba.cloud.nacos.configdata.NacosConfigDataResource.NacosIt
*/
public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataResource> {
private static final EnumSet<Option> ALL_OPTIONS = EnumSet.allOf(Option.class);
private final Log log;
public NacosConfigDataLoader(Log log) {
@ -63,15 +54,6 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
@Override
public ConfigData load(ConfigDataLoaderContext context,
NacosConfigDataResource resource) {
if (context.getBootstrapContext().isRegistered(LoaderInterceptor.class)) {
LoaderInterceptor interceptor = context.getBootstrapContext()
.get(LoaderInterceptor.class);
if (interceptor != null) {
Binder binder = context.getBootstrapContext().get(Binder.class);
return interceptor
.apply(new LoadContext(context, resource, binder, this::doLoad));
}
}
return doLoad(context, resource);
}
@ -94,29 +76,9 @@ public class NacosConfigDataLoader implements ConfigDataLoader<NacosConfigDataRe
NacosPropertySourceRepository.collectNacosPropertySource(propertySource);
if (ALL_OPTIONS.size() == 1) {
// boot 2.4.2 and prior
return new ConfigData(propertySources);
}
else if (ALL_OPTIONS.size() == 2) {
// boot 2.4.3 and 2.4.4
return new ConfigData(propertySources, Option.IGNORE_IMPORTS,
Option.IGNORE_PROFILES);
}
else if (ALL_OPTIONS.size() > 2) {
// boot 2.4.5+
return new ConfigData(propertySources, source -> {
List<Option> options = new ArrayList<>();
options.add(Option.IGNORE_IMPORTS);
options.add(Option.IGNORE_PROFILES);
for (String profile : resource.getAcceptedProfiles()) {
if (source.getName().contains("-" + profile + ".")) {
options.add(Option.PROFILE_SPECIFIC);
}
}
return Options.of(options.toArray(new Option[0]));
});
}
// TODO Currently based on 2.4.2,
// compatibility needs to be done when upgrading to boot version 2.4.5
return new ConfigData(propertySources);
}
catch (Exception e) {
if (log.isDebugEnabled()) {

Loading…
Cancel
Save