add auto config

pull/304/head
weihu 3 years ago
parent 7b2dddc354
commit 719b63077d

@ -22,7 +22,6 @@ import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -59,4 +58,5 @@ public class PolarisFeignClientAutoConfiguration {
return new PolarisFeignBeanPostProcessor(consumerAPI);
}
}

@ -0,0 +1,35 @@
package com.tencent.cloud.polaris.circuitbreaker;
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
import com.tencent.polaris.api.core.ConsumerAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author : wh
* @date : 2022/6/21 21:34
* @description:
*/
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true", matchIfMissing = true)
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
public class PolarisRestTemplateAutoConfiguration {
@Bean
@ConditionalOnBean(RestTemplate.class)
public PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler(ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
return new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler);
}
@Bean
@ConditionalOnBean(RestTemplate.class)
public PolarisRestTemplateBeanPostProcessor polarisRestTemplateBeanPostProcessor(PolarisRestTemplateResponseErrorHandler restTemplateResponseErrorHandler) {
return new PolarisRestTemplateBeanPostProcessor(restTemplateResponseErrorHandler);
}
}

@ -0,0 +1,39 @@
package com.tencent.cloud.polaris.circuitbreaker;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.client.RestTemplate;
import java.util.Objects;
/**
* @author : wh
* @date : 2022/6/21 21:20
* @description:
*/
public class PolarisRestTemplateBeanPostProcessor implements ApplicationContextAware, SmartInitializingSingleton {
private ApplicationContext applicationContext;
private final PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler;
public PolarisRestTemplateBeanPostProcessor(PolarisRestTemplateResponseErrorHandler polarisRestTemplateResponseErrorHandler) {
this.polarisRestTemplateResponseErrorHandler = polarisRestTemplateResponseErrorHandler;
}
@Override
public void afterSingletonsInstantiated() {
RestTemplate restTemplate = this.applicationContext.getBean(RestTemplate.class);
if (Objects.nonNull(restTemplate)) {
restTemplate.setErrorHandler(polarisRestTemplateResponseErrorHandler);
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

@ -1,4 +1,6 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.polaris.circuitbreaker.PolarisCircuitBreakerBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration
com.tencent.cloud.polaris.circuitbreaker.PolarisFeignClientAutoConfiguration,\
com.tencent.cloud.polaris.circuitbreaker.PolarisRestTemplateAutoConfiguration

@ -19,7 +19,6 @@
package com.tencent.cloud.polaris.circuitbreaker.example;
import com.tencent.cloud.polaris.circuitbreaker.PolarisResponseErrorHandler;
import com.tencent.cloud.polaris.circuitbreaker.PolarisRestTemplateResponseErrorHandler;
import com.tencent.polaris.api.core.ConsumerAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
@ -45,9 +44,7 @@ public class ServiceA {
@Bean
@LoadBalanced
public RestTemplate restTemplate(ConsumerAPI consumerAPI, @Autowired(required = false) PolarisResponseErrorHandler polarisResponseErrorHandler) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new PolarisRestTemplateResponseErrorHandler(consumerAPI, polarisResponseErrorHandler));
return restTemplate;
return new RestTemplate();
}
}

@ -48,8 +48,8 @@ public class ServiceAController {
return polarisServiceB.info();
}
@GetMapping("/getBServiceInfo1")
public String rest() {
@GetMapping("/getBServiceInfoByRestTemplate")
public String getBServiceInfoByRestTemplate() {
return restTemplate.getForObject("http://polaris-circuitbreaker-example-b//example/service/b/info",String.class);
}

Loading…
Cancel
Save