如下爲博主寫Hystrix系列的文章列表html
點擊查看 Hystrix入門java
點擊查看 Hystrix命令執行git
點擊查看 Hystrix處理異常機制(降級方法)github
點擊查看 Hystrix命令名稱、分組、線程池ide
點擊查看 Hystrix命令名稱、Hystrix請求處理單元測試
最基本的操做是僅僅執行一個操做,沒有回退或者降級方案。若是出現異常,則直接拋出一個異常。測試
就像下面示例同樣:this
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 public class HystrixFailsFast extends HystrixCommand<String> { private final boolean throwException; public HystrixFailsFast(boolean throwException) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.throwException = throwException; } @Override protected String run() { if (throwException) { throw new RuntimeException("failure from HystrixFailsFast"); } else { return "success"; } } }
點擊查看完整代碼spa
以上代碼的單元測試以下:.net
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 @Test public void testSuccess() { // 若是不拋異常,則返回成功的字符串 assertEquals("success", new HystrixFailsFast(false).execute()); } @Test public void testFailure() { try { // 程序拋異常 new HystrixFailsFast(true).execute(); // 判斷異常狀況 fail("we should have thrown an exception"); } catch (HystrixRuntimeException e) { // 抓獲異常,斷言異常信息 assertEquals("failure from HystrixFailsFast", e.getCause().getMessage()); e.printStackTrace(); } }
HystrixObservableCommand
等價 對於 HystrixObservableCommand
的快速失敗解決方案是調用重寫 resumeWithFallback
方法,示例以下:
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 @Override protected Observable<String> resumeWithFallback() { if (throwException) { return Observable.error(new Throwable("failure from CommandThatFailsFast")); } else { return Observable.just("success"); } }
靜默失敗至關於返回空響應或刪除功能(這是與靜態降級的區別)。它能夠經過返回null、空Map、空List或其餘此類響應來完成。 在 HystrixCommand
實例下,能夠經過實現一個 getFallback()
方法,示例以下:
(執行示意圖)
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 public class HystrixFailsSilently extends HystrixCommand<List<String>> { private final boolean throwException; public HystrixFailsSilently(boolean throwException) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.throwException = throwException; } @Override protected List<String> run() { if (throwException) { // 模擬出現異常,以便觸發降級邏輯 throw new RuntimeException("failure from HystrixFailsSilently"); } else { // 模擬正常邏輯 ArrayList<String> values = new ArrayList<String>(); values.add("success"); return values; } } @Override protected List<String> getFallback() { // 觸發降級,返回空List return Collections.emptyList(); } }
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 @Test public void testSuccess() { // 單元測試正常邏輯執行 assertEquals("success", new HystrixFailsSilently(false).execute().get(0)); } @Test public void testFailure() { try { // 單元測試異常邏輯,返回list元素個數 assertEquals(0, new HystrixFailsSilently(true).execute().size()); } catch (HystrixRuntimeException e) { fail("we should not get an exception as we fail silently with a fallback"); } }
HystrixObservableCommand
等價 對於 HystrixObservableCommand
的靜默失敗解決方案是調用重寫 resumeWithFallback()
方法,示例以下:
// 轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652 @Override protected Observable<String> resumeWithFallback() { return Observable.empty(); }
轉帖請註明原貼地址:https://my.oschina.net/u/2342969/blog/1817652