1.微服务之间调用,再出现异常时希望知道异常的原因此时需要实现FallbackFactory获取异常信息
实例:
1.定义一个抽象的回调处理类,所有回调都要继承该抽象类

/**
 * @author lyr
 * @date: 2021-05-18 15:35
 */
public abstract class FeignBaseAbstract {
    protected Throwable cause;

    public Throwable getCause() {
        return cause;
    }

    public void setCause(Throwable cause) {
        this.cause = cause;
    }
}

2.定义回调工厂类创建
/**
 * @author lyr
 * @date: 2021-05-18 10:19
 */
@Slf4j
@Component
public class WebSocketServiceFallbackFactory implements FallbackFactory<WebSocketServiceFallback> {


    @Override
    public WebSocketServiceFallback create(Throwable cause) {
        WebSocketServiceFallback webSocketServiceFallback = new WebSocketServiceFallback();
        webSocketServiceFallback.setCause(cause);
        return webSocketServiceFallback;
    }
}

在这里插入代码片
3.定义回调接口处理类继承抽象类,并在处理方法中直接抛出异常,让全局异常自行处理
/**
 * @author lyr
 */
@Slf4j
public class WebSocketServiceFallback extends FeignBaseAbstract implements WebSocketService {

    @Override
    public ResultData topicSendMsg(@RequestBody SendMsgDTO sendMsg) {
        throw new FeignCallException(cause.getMessage(),cause);
    }

    @Override
    public ResultData queueSendMsg(@RequestBody SendMsgDTO sendMsg) {
        throw new FeignCallException(cause.getMessage(),cause);
    }
}
4.配置feign异常封装类,转换异常
/**
 * feign调用异常
 * @author lyr
 * @date: 2021-05-18 17:25
 */
public class FeignCallException extends RuntimeException {

    public FeignCallException(Throwable t) {
        super(t);
    }

}

5.在feign接口中配置刚刚创建的工厂类
/**
 * @Author: lyr
 * @Date: 2021/3/28 19:14
 */
@FeignClient(value = "websocket-serv",fallbackFactory = WebSocketServiceFallbackFactory.class)
public interface WebSocketService {



    /**
     * 通过主题方式发送ws推送信息
     * @param sendMsg
     */
    @PostMapping("/topicSendMsg")
    ResultData topicSendMsg(@RequestBody SendMsgDTO sendMsg);

    /**
     * 通过队列方式发送ws推送信息
     * @param sendMsg
     */
    @PostMapping("/queueSendMsg")
    ResultData queueSendMsg(@RequestBody SendMsgDTO sendMsg);
}
6.扩展,还可以在feign的请求前作参数处理,或重试等,需要可自行配置
/**
 * @author lyr
 * @date: 2021-05-18 14:22
 */
/**
 * @summary fegin 客户端的自定义配置
 */
@Slf4j
@Configuration
public class MyConfiguration {

    /**
     * 自定义重试机制
     * @return
     */
    @Bean
    public Retryer feignRetryer() {
        //最大请求次数为5,初始间隔时间为100ms,下次间隔时间1.5倍递增,重试间最大间隔时间为1s,
        //return Retryer.NEVER_RETRY;
        return new Retryer.Default();
    }

    @Bean
    public ErrorDecoder feignError() {
        return (key, response) -> {
            log.error("请求xxx服务4888异常,返回:,key:{},status:{},body{}",key,response.status(), response.body());
            if (response.status() == 400) {
                log.error("请求xxx服务400参数错误,返回:{}", response.body());
            }

            if (response.status() == 409) {
                log.error("请求xxx服务409异常,返回:{}", response.body());
            }

            if (response.status() == 404) {
                log.error("请求xxx服务404异常,返回:{}", response.body());
            }

            // 其他异常交给Default去解码处理
            // 这里使用单例即可,Default不用每次都去new
            return new ErrorDecoder.Default().decode(key, response);
        };
    }

    /**
     * fegin 拦截器
     * @return
     */
    @Bean
    public RequestInterceptor cameraSign() {
        return template -> {
            // 如果是get请求
            if (template.method().equals(Request.HttpMethod.GET.name())) {
                //获取到get请求的参数
                Map<String, Collection<String>> queries = template.queries();
            }

            //如果是Post请求
            if (template.method().equals(Request.HttpMethod.POST.name())) {
                //获得请求body
                String body = template.requestBody().asString();
                JSONPObject request = JSON.parseObject(body, JSONPObject.class);
            }

            //Do what you want... 例如生成接口签名

            String sign = "根据请求参数生成的签名";
            //放入url?之后
            template.query("sign", sign);

            //放入请求body中
            String newBody = "原有body" + sign;
            template.body(Request.Body.encoded(newBody.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
        };
    }
}

官网:springOpenFeign
参考博客:Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 实践

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐