Hystrix的回退

一 回退

执行超时等失败情况

断路器打开

线程池满载

二 测试断路器回退

package org.crazyit.cloud.cb;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommand.Setter;

public class FallbackMain {

    public static void main(String[] args) {
        FallbackCommand c = new FallbackCommand();
        String result = c.execute();
        System.out.println(result);
    }

    static class FallbackCommand extends HystrixCommand<String> {
        
        public FallbackCommand() {
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                          .withCircuitBreakerForceOpen(true)));
        }

        @Override
        protected String run() throws Exception {
            return "success";
        }

        @Override
        protected String getFallback() {
            return "fallback";
        }
    }
}

三 测试结果(断路器打开,总会执行回退)

fallback

四 回退模式

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81635269
今日推荐