Remove @EnableAsync and @EnableScheduling to avoid conflicts. (#817)

* Remove @EnableAsync and @EnableScheduling to avoid conflicts.  Fixes #780
This commit is contained in:
Ryan Baxter
2021-07-01 11:14:15 -04:00
committed by GitHub
parent 2442f4695e
commit 994576af9e
3 changed files with 49 additions and 11 deletions

View File

@@ -51,12 +51,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-rsa</artifactId>

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration;
import org.springframework.cloud.commons.util.TaskSchedulerWrapper;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator;
@@ -41,8 +42,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.Assert;
/**
@@ -65,8 +65,6 @@ public class ConfigReloadAutoConfiguration {
*/
@ConditionalOnProperty("spring.cloud.kubernetes.reload.enabled")
@ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class })
@EnableScheduling
@EnableAsync
protected static class ConfigReloadAutoConfigurationBeans {
@Autowired
@@ -87,12 +85,14 @@ public class ConfigReloadAutoConfiguration {
@Autowired(
required = false) ConfigMapPropertySourceLocator configMapPropertySourceLocator,
@Autowired(
required = false) SecretsPropertySourceLocator secretsPropertySourceLocator) {
required = false) SecretsPropertySourceLocator secretsPropertySourceLocator,
TaskSchedulerWrapper taskScheduler) {
switch (properties.getMode()) {
case POLLING:
return new PollingConfigurationChangeDetector(this.environment,
properties, this.kubernetesClient, strategy,
configMapPropertySourceLocator, secretsPropertySourceLocator);
configMapPropertySourceLocator, secretsPropertySourceLocator,
taskScheduler.getTaskScheduler(), properties);
case EVENT:
return new EventBasedConfigurationChangeDetector(this.environment,
properties, this.kubernetesClient, strategy,
@@ -102,6 +102,18 @@ public class ConfigReloadAutoConfiguration {
"Unsupported configuration reload mode: " + properties.getMode());
}
@Bean("springCloudKubernetesTaskScheduler")
@ConditionalOnMissingBean
public TaskSchedulerWrapper taskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setThreadNamePrefix(
"spring-cloud-kubernetes-ThreadPoolTaskScheduler-");
threadPoolTaskScheduler.setDaemon(true);
return new TaskSchedulerWrapper(threadPoolTaskScheduler);
}
/**
* @param properties config reload properties
* @param ctx application context

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.kubernetes.config.reload;
import java.time.Duration;
import java.util.List;
import javax.annotation.PostConstruct;
@@ -24,13 +25,15 @@ import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.task.TaskSchedulerBuilder;
import org.springframework.cloud.kubernetes.config.ConfigMapPropertySource;
import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator;
import org.springframework.cloud.kubernetes.config.SecretsPropertySource;
import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
/**
* A change detector that periodically retrieves secrets and configmaps and fire a reload
@@ -47,6 +50,11 @@ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetec
private SecretsPropertySourceLocator secretsPropertySourceLocator;
private TaskScheduler taskExecutor;
private Duration period = Duration.ofMillis(1500);
@Deprecated
public PollingConfigurationChangeDetector(AbstractEnvironment environment,
ConfigReloadProperties properties, KubernetesClient kubernetesClient,
ConfigurationUpdateStrategy strategy,
@@ -56,15 +64,31 @@ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetec
this.configMapPropertySourceLocator = configMapPropertySourceLocator;
this.secretsPropertySourceLocator = secretsPropertySourceLocator;
this.taskExecutor = new TaskSchedulerBuilder().build();
}
public PollingConfigurationChangeDetector(AbstractEnvironment environment,
ConfigReloadProperties properties, KubernetesClient kubernetesClient,
ConfigurationUpdateStrategy strategy,
ConfigMapPropertySourceLocator configMapPropertySourceLocator,
SecretsPropertySourceLocator secretsPropertySourceLocator,
TaskScheduler taskExecutor, ConfigReloadProperties configReloadProperties) {
super(environment, properties, kubernetesClient, strategy);
this.configMapPropertySourceLocator = configMapPropertySourceLocator;
this.secretsPropertySourceLocator = secretsPropertySourceLocator;
this.taskExecutor = taskExecutor;
this.period = configReloadProperties.getPeriod();
}
@PostConstruct
public void init() {
this.log.info("Kubernetes polling configuration change detector activated");
PeriodicTrigger trigger = new PeriodicTrigger(period.toMillis());
trigger.setInitialDelay(period.toMillis());
taskExecutor.schedule(this::executeCycle, trigger);
}
@Scheduled(initialDelayString = "${spring.cloud.kubernetes.reload.period:15000}",
fixedDelayString = "${spring.cloud.kubernetes.reload.period:15000}")
public void executeCycle() {
boolean changedConfigMap = false;