org.cattleframework.cloud.discovery.enhancement.EnhancementConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-cloud-discovery-common Show documentation
Show all versions of cattle-cloud-discovery-common Show documentation
Cattle framework cloud discovery common component pom
/*
* Copyright (C) 2018 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
*
* http://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 org.cattleframework.cloud.discovery.enhancement;
import java.util.ArrayList;
import java.util.List;
import org.cattleframework.cloud.discovery.DiscoveryConstants;
import org.cattleframework.cloud.discovery.enhancement.feign.EnhancedLoadBalancerFeignRequestTransformer;
import org.cattleframework.cloud.discovery.enhancement.feign.FeignBeanPostProcessor;
import org.cattleframework.cloud.discovery.enhancement.filter.ReactiveFilter;
import org.cattleframework.cloud.discovery.enhancement.filter.ServletFilter;
import org.cattleframework.cloud.discovery.enhancement.plugin.DefaultPluginRunner;
import org.cattleframework.cloud.discovery.enhancement.plugin.Plugin;
import org.cattleframework.cloud.discovery.enhancement.plugin.PluginRunner;
import org.cattleframework.cloud.discovery.enhancement.resttemplate.RestTemplateInterceptor;
import org.cattleframework.cloud.discovery.enhancement.resttemplate.RestTemplateLoadBalancerRequestTransformer;
import org.cattleframework.cloud.discovery.enhancement.scg.GatewayFilter;
import org.cattleframework.cloud.discovery.enhancement.webclient.WebClientExchangeFilterFunction;
import org.cattleframework.cloud.discovery.enhancement.webclient.WebClientLoadBalancerClientRequestTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
import org.springframework.cloud.client.loadbalancer.reactive.WebClientCustomizer;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Role;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import jakarta.servlet.DispatcherType;
/**
* 增强自动配置
*
* @author orange
*
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "cattle.cloud.enhancement.enabled", havingValue = "true", matchIfMissing = true)
public class EnhancementConfiguration {
@Bean
@Lazy
public PluginRunner pluginRunner(@Autowired(required = false) List plugins,
@Autowired(required = false) Registration registration) {
return new DefaultPluginRunner(plugins, registration);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
@AutoConfigureBefore(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
@Role(RootBeanDefinition.ROLE_INFRASTRUCTURE)
public static class FeignClientAutoConfiguration {
@Bean
public FeignBeanPostProcessor feignBeanPostProcessor(@Lazy PluginRunner pluginRunner) {
return new FeignBeanPostProcessor(pluginRunner);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = {
"org.springframework.cloud.openfeign.loadbalancer.LoadBalancerFeignRequestTransformer" })
public EnhancedLoadBalancerFeignRequestTransformer enhancedLoadBalancerFeignRequestTransformer() {
return new EnhancedLoadBalancerFeignRequestTransformer();
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
public static class RestTemplateAutoConfiguration {
@Bean
public RestTemplateInterceptor restTemplateInterceptor(@Lazy PluginRunner pluginRunner) {
return new RestTemplateInterceptor(pluginRunner);
}
@Bean
@ConditionalOnMissingBean
public RestTemplateCustomizer restTemplateCustomizer(LoadBalancerInterceptor loadBalancerInterceptor,
RestTemplateInterceptor restTemplateInterceptor) {
return restTemplate -> {
List list = new ArrayList<>(restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
list.add(restTemplateInterceptor);
restTemplate.setInterceptors(list);
};
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = { "org.springframework.cloud.client.loadbalancer.LoadBalancerRequestTransformer" })
public RestTemplateLoadBalancerRequestTransformer restTemplateLoadBalancerRequestTransformer() {
return new RestTemplateLoadBalancerRequestTransformer();
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.web.reactive.function.client.WebClient")
public static class WebClientAutoConfiguration {
@Bean
public WebClientExchangeFilterFunction webClientExchangeFilterFunction(@Lazy PluginRunner pluginRunner) {
return new WebClientExchangeFilterFunction(pluginRunner);
}
@Bean
@ConditionalOnMissingBean
public WebClientCustomizer webClientCustomizer(
WebClientExchangeFilterFunction webClientExchangeFilterFunction) {
return webClientBuilder -> {
webClientBuilder.filter(webClientExchangeFilterFunction);
};
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerClientRequestTransformer")
public WebClientLoadBalancerClientRequestTransformer webClientLoadBalancerClientRequestTransformer() {
return new WebClientLoadBalancerClientRequestTransformer();
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.cloud.gateway.config.GatewayAutoConfiguration")
@Role(RootBeanDefinition.ROLE_INFRASTRUCTURE)
public static class GatewayAutoConfiguration {
@Bean
@ConditionalOnClass(name = "org.springframework.cloud.gateway.filter.GlobalFilter")
public GatewayFilter gatewayFilter(@Lazy PluginRunner pluginRunner) {
return new GatewayFilter(pluginRunner);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public static class ServletFilterConfig {
@Bean
public FilterRegistrationBean servletFilterRegistrationBean(ServletFilter servletFilter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(servletFilter);
filterRegistrationBean.setDispatcherTypes(DispatcherType.ASYNC, DispatcherType.ERROR,
DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
filterRegistrationBean.setOrder(DiscoveryConstants.Order.Server.Servlet.ENHANCED_FILTER_ORDER);
return filterRegistrationBean;
}
@Bean
public ServletFilter servletFilter(@Lazy PluginRunner pluginRunner) {
return new ServletFilter(pluginRunner);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public static class ReactiveFilterConfig {
@Bean
public ReactiveFilter reactiveFilter(@Lazy PluginRunner pluginRunner) {
return new ReactiveFilter(pluginRunner);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy