org.zodiac.autoconfigure.bootstrap.loadbalancer.AppLoadBalancerAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.bootstrap.loadbalancer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
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.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.application.AppInstance;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalanced;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancedRetryFactory;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerClient;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerInfoHolder;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerInterceptor;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerRequestFactory;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerRequestTransformer;
import org.zodiac.core.bootstrap.loadbalancer.RetryAppLoadBalancerInterceptor;
import org.zodiac.core.bootstrap.loadbalancer.RestTemplateCustomizer;
import org.zodiac.core.bootstrap.loadbalancer.reactive.AppReactiveLoadBalancer;
@SpringBootConfiguration
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnClass(value = {org.springframework.web.client.RestTemplate.class})
@ConditionalOnBean(value = {AppLoadBalancerClient.class})
//@EnableConfigurationProperties(value = {AppLoadBalancerProperties.class})
public class AppLoadBalancerAutoConfiguration {
@AppLoadBalanced
@Autowired(required = false)
private List restTemplates = Collections.emptyList();
@Autowired(required = false)
private List transformers = Collections.emptyList();
private AppLoadBalancerProperties appLoadBalancerProperties;
public AppLoadBalancerAutoConfiguration(AppLoadBalancerProperties appLoadBalancerProperties) {
super();
this.appLoadBalancerProperties = appLoadBalancerProperties;
}
// @Bean
// @ConditionalOnMissingBean
// @ConfPropetiesProcessor(prefix = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_PREFIX)
// protected AppLoadBalancerInfo appLoadBalancerProperties() {
// return new AppLoadBalancerProperties();
// }
@Bean
protected SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(
final ObjectProvider> restTemplateCustomizers) {
return () -> restTemplateCustomizers.ifAvailable(customizers -> {
for (org.springframework.web.client.RestTemplate restTemplate : AppLoadBalancerAutoConfiguration.this.restTemplates) {
for (RestTemplateCustomizer customizer : customizers) {
customizer.customize(restTemplate);
}
}
});
}
@Bean
@ConditionalOnMissingBean
protected AppLoadBalancerRequestFactory loadBalancerRequestFactory(AppLoadBalancerClient loadBalancerClient) {
return new AppLoadBalancerRequestFactory(loadBalancerClient, this.transformers);
}
@PostConstruct
public void init() {
AppLoadBalancerInfoHolder.setLoadBalancerInfo(appLoadBalancerProperties);
}
@SpringBootConfiguration
@Conditional(RetryMissingOrDisabledCondition.class)
static class LoadBalancerInterceptorConfig {
@Bean
protected AppLoadBalancerInterceptor loadBalancerInterceptor(AppLoadBalancerClient loadBalancerClient,
AppLoadBalancerRequestFactory requestFactory) {
return new AppLoadBalancerInterceptor(loadBalancerClient, requestFactory);
}
@Bean
@ConditionalOnMissingBean
protected RestTemplateCustomizer restTemplateCustomizer(final AppLoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List list = new ArrayList<>(restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
}
private static class RetryMissingOrDisabledCondition extends AnyNestedCondition {
RetryMissingOrDisabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnMissingClass(value = {"org.springframework.retry.support.RetryTemplate"})
static class RetryTemplateMissing {
}
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_ENABLED,
havingValue = "false")
static class RetryDisabled {
}
}
@SpringBootConfiguration
@ConditionalOnClass(value = {org.springframework.retry.support.RetryTemplate.class})
protected static class RetryAutoConfiguration {
@Bean
@ConditionalOnMissingBean
protected AppLoadBalancedRetryFactory loadBalancedRetryFactory() {
return new AppLoadBalancedRetryFactory() {};
}
}
@SpringBootConfiguration
@ConditionalOnClass(value = {org.springframework.retry.support.RetryTemplate.class})
@ConditionalOnBean(value = {AppReactiveLoadBalancer.Factory.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_RETRY_ENABLED,
matchIfMissing = true)
protected static class RetryInterceptorAutoConfiguration {
@Bean
@ConditionalOnMissingBean
protected RetryAppLoadBalancerInterceptor loadBalancerInterceptor(AppLoadBalancerClient loadBalancerClient,
AppLoadBalancerProperties properties, AppLoadBalancerRequestFactory requestFactory,
AppLoadBalancedRetryFactory loadBalancedRetryFactory,
AppReactiveLoadBalancer.Factory loadBalancerFactory) {
return new RetryAppLoadBalancerInterceptor(loadBalancerClient, properties, requestFactory,
loadBalancedRetryFactory, loadBalancerFactory);
}
@Bean
@ConditionalOnMissingBean
protected RestTemplateCustomizer
restTemplateCustomizer(final RetryAppLoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List list = new ArrayList<>(restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
}
}