Ribbon、Feign、Zuul的Fallback回退机制比较

概览

Ribbon Feign Zuul
使用的是@HystrixCommand注解,其实就是一个api 针对一个类,使用的是fallback属性 针对一个或多个微服务

Ribbon的Fallback机制

==使用的是@HystrixCommand注解,其实就是一个api ;==

如下:

@HystrixCommand(fallbackMethod = "defaultStores")
这个注解是通用的,不一定是Ribbon才用;

如何使用:

@Component
public class StoreIntegration {

    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getStores(Map<String, Object> parameters) {
        //do stuff that might fail
    }

    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

如果要让某些线程本地上下文传播到@HystrixCommand,则默认声明不起作用,因为它在线程池中执行该命令(可能造成超时)。 通过配置或直接在注释中切换Hystrix以使用与调用者相同的线程,方法是要求它使用不同的“隔离策略”。

以下示例演示如何在注释中设置线程:

@HystrixCommand(fallbackMethod = "stubMyService",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)
...

@HystrixCommand由名为“javanica”的Netflix contrib库提供。 Spring Cloud在连接到Hystrix断路器的代理中自动包装带有该注释的Spring bean。 断路器计算何时打开和关闭电路以及在发生故障时应采取的措施。

See here for more details. See the Hystrix wiki for details on the properties available.

Feign的Fallback机制

==针对一个类,使用的是fallback属性==

如下:

  • 第一种fallback形式:@FeignClient(name = "hello", fallback = HystrixClientFallback.class)

    • 代码示例:
    @FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)
    public interface UserFeignClient {
        @RequestMapping(method = RequestMethod.GET, value = "/user/{id}", consumes = "application/json")
        public User findById(@PathVariable("id") Long id);
    }
        
    
    static class HystrixClientFallback implements UserFeignClient {
        @Override
        public User findById(Long id) {
            User user = new User();
            user.setId(1L);
            return user;
        }
    }
    
  • 第二种:fallbackFactory形式@FeignClient(name = "microservice-provider-user", /*fallback = HystrixClientFallback.class, */ fallbackFactory = HystrixClientFallbackFactory.class)

    • 代码示例:
    @FeignClient(name = "microservice-provider-user", /*fallback = HystrixClientFallback.class, */ fallbackFactory = HystrixClientFallbackFactory.class)
    public interface UserFeignClient {
        @RequestMapping(method = RequestMethod.GET, value = "/user/{id}", consumes = "application/json")
        public User findById(@PathVariable("id") Long id);
    }    
        
    
    @Component
    static class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> {
        
        private static Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
        
        @Override
        public UserFeignClient create(Throwable cause) {
            //打印日志
            HystrixClientFallbackFactory.LOGGER.info("fallback; reason was: " + cause.getMessage());
            
            return new UserFeignClient() {
                @Override
                public User findById(Long id) {
                    User user = new User();
                    user.setId(-1L);
                    user.setUsername("我是HystrixClientFallbackFactory");
                    
                    return user;
                }
            };
        }
        
    }
    
    

Zuul的Fallback机制

==针对一个微服务==

如下:

Zuul的Fallback回退机制

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其...
    咔啡阅读 438评论 0 1
  • Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其...
    咔啡阅读 185评论 0 1
  • Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其...
    咔啡阅读 186评论 0 1
  • 介绍 现在我们假设一下,服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到服务返回。在高负载场景...
    RalapHao阅读 944评论 0 0
  • 文:心阳 风月无边,独自荒凉 心中凌乱不堪的 是被岁月砸疼的青春,和无助 有什么比肉体的腐朽 来得更悲凉 / 是否...
    LT心阳阅读 948评论 11 39