feat:add throwable handler to EnhancedFeignPlugin.

pull/435/head
SkyeBeFreeman 3 years ago
parent 7b9e217601
commit fcdf0fa283

@ -10,6 +10,9 @@ spring:
enabled: true
circuitbreaker:
enabled: true
stat:
enabled: true
port: 28081
feign:
hystrix:
enabled: true

@ -8,3 +8,6 @@ spring:
address: grpc://183.47.111.80:8091
namespace: default
enabled: true
stat:
enabled: true
port: 28082

@ -8,3 +8,6 @@ spring:
address: grpc://183.47.111.80:8091
namespace: default
enabled: true
stat:
enabled: true
port: 28083

@ -33,7 +33,6 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@ -60,7 +59,7 @@ public class RpcEnhancementAutoConfiguration {
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
@AutoConfigureBefore(FeignAutoConfiguration.class)
@AutoConfigureBefore(name = "org.springframework.cloud.openfeign.FeignAutoConfiguration")
protected static class PolarisFeignClientAutoConfiguration {
@Bean

@ -91,7 +91,12 @@ public class EnhancedFeignClient implements Client {
// Run pre enhanced feign plugins.
for (EnhancedFeignPlugin plugin : preEnhancedFeignPlugins) {
plugin.run(enhancedFeignContext);
try {
plugin.run(enhancedFeignContext);
}
catch (Throwable throwable) {
plugin.handlerThrowable(enhancedFeignContext, throwable);
}
}
try {
Response response = delegate.execute(request, options);
@ -99,7 +104,12 @@ public class EnhancedFeignClient implements Client {
// Run post enhanced feign plugins.
for (EnhancedFeignPlugin plugin : postEnhancedFeignPlugins) {
plugin.run(enhancedFeignContext);
try {
plugin.run(enhancedFeignContext);
}
catch (Throwable throwable) {
plugin.handlerThrowable(enhancedFeignContext, throwable);
}
}
return response;
}
@ -107,14 +117,24 @@ public class EnhancedFeignClient implements Client {
enhancedFeignContext.setException(origin);
// Run exception enhanced feign plugins.
for (EnhancedFeignPlugin plugin : exceptionEnhancedFeignPlugins) {
plugin.run(enhancedFeignContext);
try {
plugin.run(enhancedFeignContext);
}
catch (Throwable throwable) {
plugin.handlerThrowable(enhancedFeignContext, throwable);
}
}
throw origin;
}
finally {
// Run finally enhanced feign plugins.
for (EnhancedFeignPlugin plugin : finallyEnhancedFeignPlugins) {
plugin.run(enhancedFeignContext);
try {
plugin.run(enhancedFeignContext);
}
catch (Throwable throwable) {
plugin.handlerThrowable(enhancedFeignContext, throwable);
}
}
}
}

@ -47,5 +47,15 @@ public interface EnhancedFeignPlugin extends Ordered {
*
* @param context context in enhanced feign client.
*/
void run(EnhancedFeignContext context);
void run(EnhancedFeignContext context) throws Throwable;
/**
* Handler throwable from {@link EnhancedFeignPlugin#run(EnhancedFeignContext)}.
*
* @param context context in enhanced feign client.
* @param throwable throwable thrown from run method.
*/
default void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
}
}

@ -71,6 +71,13 @@ public class ExceptionPolarisReporter implements EnhancedFeignPlugin {
}
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
Request request = context.getRequest();
Response response = context.getResponse();
LOG.error("ExceptionPolarisReporter runs failed. Request=[{}]. Response=[{}].", request, response, throwable);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 1;

@ -68,6 +68,13 @@ public class SuccessPolarisReporter implements EnhancedFeignPlugin {
}
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
Request request = context.getRequest();
Response response = context.getResponse();
LOG.error("SuccessPolarisReporter runs failed. Request=[{}]. Response=[{}].", request, response, throwable);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 1;

@ -141,6 +141,11 @@ public class EnhancedFeignClientTest {
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
}
@Override
public int getOrder() {
return 0;
@ -158,6 +163,11 @@ public class EnhancedFeignClientTest {
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
}
@Override
public int getOrder() {
return 0;
@ -175,6 +185,11 @@ public class EnhancedFeignClientTest {
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
}
@Override
public int getOrder() {
return 0;
@ -192,6 +207,11 @@ public class EnhancedFeignClientTest {
}
@Override
public void handlerThrowable(EnhancedFeignContext context, Throwable throwable) {
}
@Override
public int getOrder() {
return 0;

Loading…
Cancel
Save