| | |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * Safe replacement for the common Sentinel Feign fallback. |
| | | * 公共 Sentinel Feign 降级处理的安全替代实现。 |
| | | * |
| | | * <p>The dependency version assumes every generic return type is a Class. That |
| | | * is false for declarations such as {@code Map<String, Object>}, whose runtime |
| | | * type is {@link ParameterizedType}; the failed cast used to hide the original |
| | | * downstream exception behind an AssertionError.</p> |
| | | * <p>依赖版本假定所有泛型返回类型均为 {@link Class}。对于 |
| | | * {@code Map<String, Object>} 等声明并不成立,其运行时类型为 |
| | | * {@link ParameterizedType};此前转换失败会以 AssertionError 掩盖原始下游异常。</p> |
| | | */ |
| | | public class DefaultFeignFallback<T> implements MethodInterceptor { |
| | | private static final Logger log = LoggerFactory.getLogger(DefaultFeignFallback.class); |
| | |
| | | Class<?> returnType = rawType(declaredReturnType); |
| | | Object data = defaultValue(returnType); |
| | | |
| | | // Only methods that explicitly declare ResponseData may receive a |
| | | // ResponseData fallback. Returning it from Map/List methods causes a |
| | | // second ClassCastException in the Feign proxy. |
| | | // 仅显式声明返回 ResponseData 的方法可以返回 ResponseData 降级结果。 |
| | | // 在 Map/List 方法中返回该对象会导致 Feign 代理再次抛出 ClassCastException。 |
| | | if (ResponseData.class.equals(returnType)) { |
| | | Type dataType = firstTypeArgument(declaredReturnType); |
| | | return ResponseData.builder() |