org.zodiac.autoconfigure.loadbalancer.AppLoadBalancerClientConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.loadbalancer;
import reactor.util.retry.RetrySpec;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnAppDiscoveryEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnBlockingAppDiscoveryEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnReactiveAppDiscoveryEnabled;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.application.AppInstance;
@SpringBootConfiguration
@ConditionalOnAppDiscoveryEnabled
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnClass(name = {"org.zodiac.core.loadbalancer.core.AppReactorLoadBalancer"})
public class AppLoadBalancerClientConfiguration extends org.zodiac.core.loadbalancer.annotation.AppLoadBalancerClientConfig {
private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 193827465;
@Override
@Bean
@ConditionalOnMissingBean
protected org.zodiac.core.loadbalancer.core.AppReactorLoadBalancer reactorServiceInstanceLoadBalancer(Environment environment,
org.zodiac.core.loadbalancer.support.AppLoadBalancerClientFactory loadBalancerClientFactory) {
return super.reactorServiceInstanceLoadBalancer(environment, loadBalancerClientFactory);
}
@SpringBootConfiguration
@ConditionalOnReactiveAppDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)
protected static class ReactiveSupportConfiguration extends AppLoadBalancerClientConfiguration.AbstractReactiveSupportConfiguration {
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "DEFAULT",
matchIfMissing = true)
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.discoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "ZONE")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
zonePreferenceDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.zonePreferenceDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "HEALTH_CHECK")
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient.class, org.springframework.web.reactive.function.client.WebClient.Builder.class})
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
healthCheckDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.healthCheckDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS,
havingValue = "STICKY_SESSION")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier stickySessionDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return super.stickySessionDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.ReactiveAppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS,
havingValue = "SAME_INSTANCE")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
sameInstancePreferenceServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.sameInstancePreferenceServiceInstanceListSupplier(context);
}
}
@SpringBootConfiguration
@ConditionalOnBlockingAppDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
protected static class BlockingSupportConfiguration extends AppLoadBalancerClientConfiguration.AbstractBlockingSupportConfiguration {
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "DEFAULT",
matchIfMissing = true)
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
discoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.discoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "ZONE")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
zonePreferenceDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.zonePreferenceDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class, RestTemplate.class})
@ConditionalOnMissingBean
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS, havingValue = "HEALTH_CHECK")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
healthCheckDiscoveryClientServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.healthCheckDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@ConditionalOnMissingBean
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS,
havingValue = "STICKY_SESSION")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier requestBasedStickySessionDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return super.requestBasedStickySessionDiscoveryClientServiceInstanceListSupplier(context);
}
@Override
@Bean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@ConditionalOnMissingBean
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CONFIGURATIONS,
havingValue = "SAME_INSTANCE")
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
sameInstancePreferenceServiceInstanceListSupplier(ConfigurableApplicationContext context) {
return super.sameInstancePreferenceServiceInstanceListSupplier(context);
}
}
@SpringBootConfiguration
@ConditionalOnBlockingAppDiscoveryEnabled
@ConditionalOnClass(value = {org.springframework.retry.support.RetryTemplate.class})
@Conditional(BlockingOnAvoidPreviousInstanceAndRetryEnabledCondition.class)
@AutoConfigureAfter(BlockingSupportConfiguration.class)
@ConditionalOnBean(org.zodiac.core.loadbalancer.core.AppInstanceListSupplier.class)
protected static class BlockingRetryConfiguration extends AppLoadBalancerClientConfiguration.AbstractBlockingRetryConfiguration {
@Override
@Bean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@Primary
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
retryAwareDiscoveryClientServiceInstanceListSupplier(org.zodiac.core.loadbalancer.core.AppInstanceListSupplier delegate) {
return super.retryAwareDiscoveryClientServiceInstanceListSupplier(delegate);
}
}
@SpringBootConfiguration
@ConditionalOnReactiveAppDiscoveryEnabled
@Conditional(ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition.class)
@AutoConfigureAfter(ReactiveSupportConfiguration.class)
@ConditionalOnBean(org.zodiac.core.loadbalancer.core.AppInstanceListSupplier.class)
@ConditionalOnClass(RetrySpec.class)
protected static class ReactiveRetryConfiguration extends AppLoadBalancerClientConfiguration.AbstractReactiveRetryConfiguration {
@Bean
@ConditionalOnBean(value = {org.zodiac.core.bootstrap.discovery.AppDiscoveryClient.class})
@Primary
protected org.zodiac.core.loadbalancer.core.AppInstanceListSupplier
retryAwareDiscoveryClientServiceInstanceListSupplier(org.zodiac.core.loadbalancer.core.AppInstanceListSupplier delegate) {
return super.retryAwareDiscoveryClientServiceInstanceListSupplier(delegate);
}
}
static final class BlockingOnAvoidPreviousInstanceAndRetryEnabledCondition extends AllNestedConditions {
private BlockingOnAvoidPreviousInstanceAndRetryEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_ENABLED, havingValue = "true",
matchIfMissing = true)
static class LoadBalancerRetryEnabled {
}
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_AVOID_PREVIOUS_INSTANCE, havingValue = "true",
matchIfMissing = true)
static class AvoidPreviousInstanceEnabled {
}
}
static final class ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition extends AllNestedConditions {
private ReactiveOnAvoidPreviousInstanceAndRetryEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_ENABLED, havingValue = "true")
static class LoadBalancerRetryEnabled {
}
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_AVOID_PREVIOUS_INSTANCE, havingValue = "true", matchIfMissing = true)
static class AvoidPreviousInstanceEnabled {
}
}
}