如下爲博主寫Hystrix系列的文章列表html
點擊查看 Hystrix入門java
點擊查看 Hystrix命令執行git
點擊查看 Hystrix處理異常機制(降級方法)github
默認狀況下,命令名稱來源於類名。後端
getClass().getSimpleName();
若是要顯示定義名稱的話,能夠經過 HystrixCommand
或者 HystrixObservableCommand
的構造函數:緩存
public HystrixHelloWorldCommand(String name) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command Group: Hello World")) .andCommandKey(HystrixCommandKey.Factory.asKey("Command Name: Hello World"))); this.name = name; }
爲了給每一個命令集合保存Setter配置,能夠緩存Setter,示例以下:ide
private static final Setter cachedSetter = Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")) .andCommandKey(HystrixCommandKey.Factory.asKey("Cache Setter: Hello World")); public HystrixHelloWorldCommand(String name) { super(cachedSetter); this.name = name; }
HystrixCommandKey
是一個接口,能夠做爲枚舉或者普通類實現。其實它有一個輔助工廠類,示例以下:函數
HystrixCommandKey.Factory.asKey("Command Name: Hello World")
HystrixCommandKey
源碼以下:單元測試
public interface HystrixCommandKey extends HystrixKey { public static class Factory { private static final InternMap<String, HystrixCommandKey.Factory.HystrixCommandKeyDefault> intern = new InternMap(new ValueConstructor<String, HystrixCommandKey.Factory.HystrixCommandKeyDefault>() { public HystrixCommandKey.Factory.HystrixCommandKeyDefault create(String key) { return new HystrixCommandKey.Factory.HystrixCommandKeyDefault(key); } }); private Factory() { } public static HystrixCommandKey asKey(String name) { return (HystrixCommandKey)intern.interned(name); } static int getCommandCount() { return intern.size(); } private static class HystrixCommandKeyDefault extends HystrixKeyDefault implements HystrixCommandKey { public HystrixCommandKeyDefault(String name) { super(name); } } } }
點擊查看源碼和單元測試測試
Hystrix使用命令分組將一塊兒的命令進行管理,好比報告、警報、儀表盤或組/庫。默認狀況下,Hystrix使用 HystrixCommandGroupKey
來定義命令線程池,除非單獨定義線程池。
HystrixCommandGroupKey
是一個接口,能夠做爲枚舉或者普通類實現。其實它有一個輔助工廠類,示例以下:
HystrixCommandGroupKey.Factory.asKey("Command Group: Hello World")
HystrixCommandGroupKey
源碼以下:
public interface HystrixCommandGroupKey extends HystrixKey { public static class Factory { private static final InternMap<String, HystrixCommandGroupKey.Factory.HystrixCommandGroupDefault> intern = new InternMap(new ValueConstructor<String, HystrixCommandGroupKey.Factory.HystrixCommandGroupDefault>() { public HystrixCommandGroupKey.Factory.HystrixCommandGroupDefault create(String key) { return new HystrixCommandGroupKey.Factory.HystrixCommandGroupDefault(key); } }); private Factory() { } public static HystrixCommandGroupKey asKey(String name) { return (HystrixCommandGroupKey)intern.interned(name); } static int getGroupCount() { return intern.size(); } private static class HystrixCommandGroupDefault extends HystrixKeyDefault implements HystrixCommandGroupKey { public HystrixCommandGroupDefault(String name) { super(name); } } } }
點擊查看源碼和單元測試
線程池主要體現是用於監測、指標發佈、緩存和其餘此類用途的HystrixThreadPool。 一個HystrixCommand與一個單獨注入到它的HystrixThreadPoolKey所檢索到的HystrixThreadPool相關聯, 或者默認爲使用HystrixCommandGroupKey的建立一個 。
若是要顯示定義名稱的話,能夠經過 HystrixCommand
或者 HystrixObservableCommand
的構造函數:
public HystrixHelloWorldCommand(String name) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Command Group: Hello World")) .andCommandKey(HystrixCommandKey.Factory.asKey("Command Name: Hello World")) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("Command ThreadPool: Hello World"))); this.name = name; }
HystrixThreadPoolKey
是一個接口,能夠做爲枚舉或者普通類實現。其實它有一個輔助工廠類,示例以下:
HystrixThreadPoolKey.Factory.asKey("Command ThreadPool: Hello World")
HystrixThreadPoolKey
源碼以下:
public interface HystrixThreadPoolKey extends HystrixKey { class Factory { private Factory() { } private static final InternMap<String, HystrixThreadPoolKey> intern = new InternMap<String, HystrixThreadPoolKey>( new InternMap.ValueConstructor<String, HystrixThreadPoolKey>() { @Override public HystrixThreadPoolKey create(String key) { return new HystrixThreadPoolKeyDefault(key); } }); public static HystrixThreadPoolKey asKey(String name) { return intern.interned(name); } private static class HystrixThreadPoolKeyDefault extends HystrixKeyDefault implements HystrixThreadPoolKey { public HystrixThreadPoolKeyDefault(String name) { super(name); } } static int getThreadPoolCount() { return intern.size(); } } }
可能使用HystrixThreadPoolKey而不單單是不一樣的HystrixCommandGroupKey的緣由是多個命令可能屬於相同的全部權或邏輯功能的「組」,可是某些命令可能須要彼此隔離。
簡單示例以下:
若是命令A的線程池潛在而且飽和,它就不該該阻止命令B訪問資源,由於他們互相命中不一樣的後端資源。所以,咱們在邏輯上但願這些命令組合在一塊兒,但但願它們以不一樣的方式隔離,並使用 HystrixThreadPoolKey
來給它們每一個線程池提供一個不一樣的線程池。
點擊查看源碼和單元測試
轉帖請註明原貼地址 : https://my.oschina.net/u/2342969/blog/1816623