1. 程式人生 > >Ribbon、Feign、Zuul的Fallback回退機制比較

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回退機制