网站设计导航,代理公司的经营范围,兰州新增94个高风险区,合肥设计工作室有哪些场景
基于springboot开发的项目#xff0c;对接第三方#xff0c;第三方的接口有限流策略#xff0c;某个时间段内有调用频率限制#xff0c;返回的状态码HttpStatus不是200#xff0c;而HttpStatus是429。现基于HttpStatus我们发起的重试。
技术点
springbootfeign fe…场景
基于springboot开发的项目对接第三方第三方的接口有限流策略某个时间段内有调用频率限制返回的状态码HttpStatus不是200而HttpStatus是429。现基于HttpStatus我们发起的重试。
技术点
springbootfeign feign要基于HttpStatus重试。
实现
一般我不喜欢配置全局的配置因为feign的客户端可能会有多个如果只有一个第三方服务那可以配置全局的。
基于HttpStatus为429的解码器
Slf4j
public class RemoteErrorDecoder implements ErrorDecoder {private static final int TOO_MANY_REQUESTS_CODE 429;private final ErrorDecoder defaultErrorDecoder new ErrorDecoder.Default();Overridepublic Exception decode(String methodKey, Response response) {if (response.status() TOO_MANY_REQUESTS_CODE) {log.error(请求因为限流被拒绝,methodKey:{},status:{}, methodKey, response.status());return new RetryableException(TOO_MANY_REQUESTS_CODE, 请求因为限流被拒绝, response.request().httpMethod(), null,response.request());} else {log.error(其他状态码,methodKey:{},status:{}, methodKey, response.status());return defaultErrorDecoder.decode(methodKey, response);}}
}feign配置类
public class RemoteFeignConfig {Beanpublic ErrorDecoder errorDecoder() {return new RemoteErrorDecoder();}// 重试器可以使用默认的 这边可以根据自己实际情况配置Beanpublic Retryer retryer() {return new Retryer.Default(30000, 30000, 3);}
}feign client
FeignClient(name xxx, url xxx, configuration RemoteFeignConfig.class)
public interface RemoteService {// TODO 调用的接口
}