IT徐胖子原创本文未授权请勿转载
1 文章概述
假设服务提供者提供A服务,但是A服务并不稳定,那么如果服务消费者无法正常消费A服务就需要做降级处理,不再消费A服务而是返回一个mock值,这就是所谓服务降级。
但是在降级之前,消费者有可能重试消费服务A,或者直接返回空结果,或者延时重试,这就是所谓集群容错策略。集群容错策略有很多,本文我们分析快速失败策略。
Failfast策略被称为快速失败策略,只消费一次服务,一旦消费失败则抛出异常,我们看一个消费者代码实例。
<beans><dubbo:application name="xpz-consumer" ><dubbo:registry address="zookeeper://127.0.0.1:2181" ><dubbo:reference id="helloService" cluster="failfast" interface="com.itxpz.dubbo.demo.provider.HelloService" ></beans>
public class Consumer {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath*:META-INF/spring/dubbo-consumer.xml" });context.start();HelloService helloService = (HelloService) context.getBean("helloService");String result = helloService.sayHello("IT徐胖子");System.out.println("==========客户端收到结果==========" + result);}}
// 通过动态代理机制生成代理对象Proxy// Proxy持有InvokerInvocationHandler// InvokerInvocationHandler持有invoker = MockClusterInvoker(FailfastClusterInvoker)HelloService helloService = (HelloService) context.getBean("helloService");
2 源码分析
public class FailfastClusterInvoker<T> extends AbstractClusterInvoker<T> {public FailfastClusterInvoker(Directory<T> directory) {super(directory);}@Overridepublic Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {// 检查生产者invokers是否合法checkInvokers(invokers, invocation);// 负载均衡选择一个生产者invokerInvoker<T> invoker = select(loadbalance, invocation, invokers, null);try {// 服务消费发起远程调用return invoker.invoke(invocation);} catch (Throwable e) {// 服务消费失败不重试直接抛出异常if (e instanceof RpcException && ((RpcException) e).isBiz()) {throw (RpcException) e;}throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : 0, "Failfast invoke providers " + invoker.getUrl() + " " + loadbalance.getClass().getSimpleName() + " select from all providers " + invokers + " for service " + getInterface().getName() + " method " + invocation.getMethodName() + " on consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", but no luck to perform the invocation. Last error is: " + e.getMessage(), e.getCause() != null ? e.getCause() : e);}}}
3 文章总结
Failfast策略只消费一次服务,消费失败则直接抛出异常。如果一个服务需要幂等,或者对错误容忍度比较高,或者需要快速响应,可以使用快速失败策略。
长按二维码关注更多精彩文章
文章转载自JAVA前线,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




