1 /**2 * Commandwithfallbackvianetwork.run Analog Remote call failed, fallbackvianetwork simulation needs to get old data from Redis over the network3 */4 Public classCommandwithfallbackvianetworkextendsHystrixcommand<string> {5 Private Final intID;6 7 protectedCommandwithfallbackvianetwork (intID) {8 //when Threadpoolkey is not set, Threadpoolkey defaults to Groupkey, and different threadpoolkey represent different thread pools. 9 //a group name for a hystrixcommand. This was used for grouping together commands such as for reporting, alerting, dashboards or team/library ownership.Ten Super(Setter.withgroupkey (HystrixCommandGroupKey.Factory.asKey ("Remoteservicex")) One //a key to represent a hystrixcommand for monitoring, circuit-breakers, metrics publishing, caching and other such uses. A. Andcommandkey (HystrixCommandKey.Factory.asKey ("Getvaluecommand")) - . Andcommandpropertiesdefaults (Hystrixcommandproperties.setter () - . Withexecutionisolationstrategy (HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))) ; the This. ID =ID; - } - - @Override + protectedString Run () { - //Remoteservicexclient.getvalue (ID); + //analog Remote call failed A Throw NewRuntimeException ("Force Failure for example"); at } - - @Override - protectedString Getfallback () { - return Newfallbackvianetwork (ID). Execute (); - } in - Private Static classFallbackvianetworkextendsHystrixcommand<string> { to Private Final intID; + Private StaticMap<integer, string> memcacheclient =NewConcurrenthashmap<> ();//serves as a Redis cache and requires access to the network - the PublicFallbackvianetwork (intID) { * //Groupkey is consistent with Commandwithfallbackvianetwork, $ Super(Setter.withgroupkey (HystrixCommandGroupKey.Factory.asKey ("Remoteservicex"))Panax Notoginseng. Andcommandkey (HystrixCommandKey.Factory.asKey ("Getvaluefallbackcommand")) - //Use a different threadpool for the fallback command the //So saturating the Remoteservicex pool won ' t prevent + //fallbacks from executing A. Andthreadpoolkey (HystrixThreadPoolKey.Factory.asKey ("Remoteservicexfallback"))); the This. ID =ID; + } - $ @Override $ protectedString Run () { - returnmemcacheclient.get (ID); - } the - @OverrideWuyi protectedString Getfallback () { the //The fallback also failed - //So this fallback-of-a-fallback would Wu //fail silently and return null - return NULL; About } $ } -}
netflix-hystrix-Simple Example