|
|
@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.tencent.cloud.polaris.circuitbreaker.exception.FallbackWrapperException;
|
|
|
|
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
|
|
|
|
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
|
|
|
|
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
|
|
|
|
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
|
|
|
|
|
|
|
|
|
|
|
@ -63,39 +64,50 @@ public class PolarisCircuitBreakerRestTemplateInterceptor implements ClientHttpR
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
|
|
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
|
|
|
return circuitBreakerFactory.create(request.getURI().getHost() + "#" + request.getURI().getPath()).run(
|
|
|
|
try {
|
|
|
|
() -> {
|
|
|
|
return circuitBreakerFactory.create(request.getURI().getHost() + "#" + request.getURI().getPath()).run(
|
|
|
|
try {
|
|
|
|
() -> {
|
|
|
|
ClientHttpResponse response = execution.execute(request, body);
|
|
|
|
try {
|
|
|
|
ResponseErrorHandler errorHandler = restTemplate.getErrorHandler();
|
|
|
|
ClientHttpResponse response = execution.execute(request, body);
|
|
|
|
if (errorHandler.hasError(response)) {
|
|
|
|
ResponseErrorHandler errorHandler = restTemplate.getErrorHandler();
|
|
|
|
errorHandler.handleError(request.getURI(), request.getMethod(), response);
|
|
|
|
if (errorHandler.hasError(response)) {
|
|
|
|
|
|
|
|
errorHandler.handleError(request.getURI(), request.getMethod(), response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
catch (IOException e) {
|
|
|
|
}
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
catch (IOException e) {
|
|
|
|
}
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t -> {
|
|
|
|
},
|
|
|
|
if (StringUtils.hasText(polarisCircuitBreaker.fallback())) {
|
|
|
|
t -> {
|
|
|
|
CircuitBreakerStatus.FallbackInfo fallbackInfo = new CircuitBreakerStatus.FallbackInfo(200, null, polarisCircuitBreaker.fallback());
|
|
|
|
if (StringUtils.hasText(polarisCircuitBreaker.fallback())) {
|
|
|
|
|
|
|
|
CircuitBreakerStatus.FallbackInfo fallbackInfo = new CircuitBreakerStatus.FallbackInfo(200, null, polarisCircuitBreaker.fallback());
|
|
|
|
|
|
|
|
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PolarisCircuitBreakerFallback.class.toGenericString().equals(polarisCircuitBreaker.fallbackClass().toGenericString())) {
|
|
|
|
|
|
|
|
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerFallback.class, "fallback");
|
|
|
|
|
|
|
|
PolarisCircuitBreakerFallback polarisCircuitBreakerFallback = applicationContext.getBean(polarisCircuitBreaker.fallbackClass());
|
|
|
|
|
|
|
|
return (PolarisCircuitBreakerHttpResponse) ReflectionUtils.invokeMethod(method, polarisCircuitBreakerFallback);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t instanceof CallAbortedException) {
|
|
|
|
|
|
|
|
CircuitBreakerStatus.FallbackInfo fallbackInfo = ((CallAbortedException) t).getFallbackInfo();
|
|
|
|
|
|
|
|
if (fallbackInfo != null) {
|
|
|
|
|
|
|
|
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
|
|
|
|
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PolarisCircuitBreakerFallback.class.toGenericString().equals(polarisCircuitBreaker.fallbackClass().toGenericString())) {
|
|
|
|
|
|
|
|
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerFallback.class, "fallback");
|
|
|
|
|
|
|
|
PolarisCircuitBreakerFallback polarisCircuitBreakerFallback = applicationContext.getBean(polarisCircuitBreaker.fallbackClass());
|
|
|
|
|
|
|
|
return (PolarisCircuitBreakerHttpResponse) ReflectionUtils.invokeMethod(method, polarisCircuitBreakerFallback);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t instanceof CallAbortedException) {
|
|
|
|
|
|
|
|
CircuitBreakerStatus.FallbackInfo fallbackInfo = ((CallAbortedException) t).getFallbackInfo();
|
|
|
|
|
|
|
|
if (fallbackInfo != null) {
|
|
|
|
|
|
|
|
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FallbackWrapperException(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalStateException(t);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
catch (FallbackWrapperException e) {
|
|
|
|
|
|
|
|
// unwrap And Rethrow
|
|
|
|
|
|
|
|
Throwable underlyingException = e.getCause();
|
|
|
|
|
|
|
|
if (underlyingException instanceof RuntimeException) {
|
|
|
|
|
|
|
|
throw (RuntimeException) underlyingException;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalStateException(underlyingException);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|