org.cattleframework.cloud.discovery.DiscoveryAutoConfiguration 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;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.cattleframework.cloud.discovery.enhancement.EnhancementConfiguration;
import org.cattleframework.cloud.discovery.enhancement.context.StaticDataManager;
import org.cattleframework.cloud.discovery.event.ServiceRegistryEvent;
import org.cattleframework.cloud.discovery.reflect.ServiceRegistryAspect;
import org.cattleframework.exception.web.ExceptionProcessCustomizer;
import org.cattleframework.exception.web.ExceptionProcessResponse;
import org.cattleframework.utils.http.RestTemplateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.FeignBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.RestTemplate;
import feign.FeignException;
import feign.Request.Options;
import feign.RetryableException;
/**
* 服务发现自动配置
*
* @author orange
*
*/
@AutoConfiguration
@EnableDiscoveryClient
@EnableConfigurationProperties({ DiscoveryInfoProperties.class, CommunicateProperties.class,
LoadBalancerProperties.class })
@Import(EnhancementConfiguration.class)
public class DiscoveryAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(DiscoveryAutoConfiguration.class);
@Bean
public ServiceRegistryAspect serviceRegistryAspect(
@Autowired(required = false) List serviceRegistryEvents) {
return new ServiceRegistryAspect(serviceRegistryEvents);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "feign.Feign")
public static class FeignConfig {
@Bean
public FeignBuilderCustomizer feignBuilderCustomizer(CommunicateProperties communicateProperties) {
long connectTimeout = communicateProperties.getConnectTimeout() < 0L ? 5L
: communicateProperties.getConnectTimeout();
long readTimeout = communicateProperties.getReadTimeout() < 0L ? 5L
: communicateProperties.getReadTimeout();
logger.debug("Feign设置的连接超时:{}秒,读取超时:{}秒", connectTimeout, readTimeout);
return customizer -> {
customizer.options(new Options(connectTimeout, TimeUnit.SECONDS, readTimeout, TimeUnit.SECONDS, true));
};
}
@Bean
public ExceptionProcessCustomizer feignExceptionProcessCustomizer() {
return new ExceptionProcessCustomizer() {
@Override
public Class> getType() {
return FeignException.class;
}
@Override
public ExceptionProcessResponse customize(Throwable exception) {
FeignException feignException = (FeignException) exception;
int status = feignException.status();
if (status == -1 && feignException instanceof RetryableException) {
status = HttpStatus.SERVICE_UNAVAILABLE.value();
}
return new ExceptionProcessResponse(status, exception.getMessage());
}
};
}
}
@Configuration(proxyBeanMethods = false)
@Conditional(NotReactiveWebApplicationCondition.class)
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
public static class RestTemplateConfig {
@Bean(DiscoveryConstants.REST_TEMPLATE_NAME)
@LoadBalanced
@ConditionalOnMissingBean(name = DiscoveryConstants.REST_TEMPLATE_NAME)
public RestTemplate cloudRestTemplate(RestTemplateBuilder restTemplateBuilder,
CommunicateProperties communicateProperties) {
long connectTimeout = communicateProperties.getConnectTimeout() < 0L ? 5L
: communicateProperties.getConnectTimeout();
long readTimeout = communicateProperties.getReadTimeout() < 0L ? 5L
: communicateProperties.getReadTimeout();
logger.debug("RestTemplate设置的连接超时:{}秒,读取超时:{}秒", connectTimeout, readTimeout);
restTemplateBuilder = restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(connectTimeout))
.setReadTimeout(Duration.ofSeconds(readTimeout));
return restTemplateBuilder.build();
}
@Bean(DiscoveryConstants.REST_TEMPLATE_UTILS_NAME)
@ConditionalOnMissingBean(name = DiscoveryConstants.REST_TEMPLATE_UTILS_NAME)
public RestTemplateUtils cloudRestTemplateUtils(
@Qualifier(DiscoveryConstants.REST_TEMPLATE_NAME) RestTemplate restTemplate) {
return new RestTemplateUtils(restTemplate);
}
}
@Bean
public StaticDataManager staticDataManager(DiscoveryInfoProperties discoveryInfoProperties) {
return new StaticDataManager(discoveryInfoProperties);
}
static class NotReactiveWebApplicationCondition extends NoneNestedConditions {
NotReactiveWebApplicationCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
private static final class ReactiveWebApplication {
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy