源码中熔断逻辑需要RetrofitBlockException异常,我这里遇到的问题是B服务不在线时抛出RetrofitIOException 原因是@RetrofitClient注解默认使用的错误解码器封装为RetrofitIOException 解决方案是自定义错误解码器 完成的配置和代码如下 # 配置 retrofit.degrade.degrade-type=sentinel retrofit.degrade.global-sentinel-degrade.enable=true @Component @RetrofitClient(baseUrl = "${chat.navi.host}", fallback = ChatClientFallback.class, errorDecoder = ChatErrorDecoder.class) @SentinelDegrade public interface ChatClient { @POST("/chat") ChatResponse chat(@Body ChatRequest request); } @Component public class ChatClientFallback implements ChatClient { @Override public ChatResponse chat(ChatRequest request) { ChatResponse chatResponse = new ChatResponse(); chatResponse.setCode(500); chatResponse.setResponse("服务异常请稍后重试!"); return chatResponse; } } @Component public class ChatErrorDecoder implements ErrorDecoder { @Override public RuntimeException ioExceptionDecode(Request request, IOException cause) { return new RetrofitBlockException(cause); } }
lianjiatech retrofit boot-starter3.0.3 配置fallback不生效
于 2024-04-09 16:54:04 首次发布
本文介绍了如何在源码中处理B服务不在线时引发的RetrofitIOException,通过自定义错误解码器和使用Sentinel实现全局降级策略,确保API请求的优雅失败并返回适当响应。
摘要由CSDN通过智能技术生成