Hystrix常用功能介绍

  • Hystrix常用功能介绍已关闭评论
  • 25 views
  • A+
所属分类:编程开发

Hystrix是一个简单易用的熔断中间件,本篇文章会介绍下常规的使用方式。

目录

  • helloWorld初窥Hystrix
  • HystrixCommand基本配置、同步和异步执行
  • request cache的使用
  • fallback
    • default fallback
    • 单级fallback
    • 多级fallback
    • 主次多HystrixCommand fallback
  • 接入现有业务
  • 总结

helloWorld初窥Hystrix

先贴代码

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

public class CommandHelloWorld extends HystrixCommand<String> {

    private final String name;

    public CommandHelloWorld(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected String run() {
        return "Hello " + name + "!";
    }
}

代码很简单,声明一个类CommandHelloWorld,集成HystrixCommand, HystrixCommand携带泛型,泛型的类型就是我们的执行方法run()返回的结果的类型。逻辑执行体就是run方法的实现。
构造方法至少要传递一个分组相关的配置给父类才能实现实例化,具体用来干什么的后面会描述。

下面测试一下

public class CommandHelloWorldTest {
    @Test
    public void test_01(){
        String result = new CommandHelloWorld("world").execute();
        Assert.assertEquals("Hello world!",result);
    }
}

就这样第一个hellworld就跑起来,so easy

HystrixCommand基本配置、同步和异步执行

1.HystrixCommand、Group、ThreadPool 配置

Hystrix把执行都包装成一个HystrixCommand,并启用线程池实现多个依赖执行的隔离。
上面的代码集成了HystrixCommand并实现了类似分组key的构造方法,那么分组是用来做什么呢?还有没有其他类似的东西?怎么没有看到线程配置呢?

Hystrix每个command都有对应的commandKey可以认为是command的名字,默认是当前类的名字getClass().getSimpleName(),每个command也都一个归属的分组,这两个东西主要方便Hystrix进行监控、报警等。
HystrixCommand使用的线程池也有线程池key,以及对应线程相关的配置

下面是代码的实现方式
自定义command key

HystrixCommandKey.Factory.asKey("HelloWorld")

自定义command group

HystrixCommandGroupKey.Factory.asKey("ExampleGroup")

那么线程池呢?

HystrixThreadPoolKey.Factory.asKey("HelloWorldPool")

Hystrix command配置有熔断阀值,熔断百分比等配置,ThreadPoll有线程池大小,队列大小等配置,如何设置?
Hystrix的配置可以通过Setter进行构造

  public CommandHelloWorld(){
        super(Setter
                //分组key
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey("helloWorldGroup"))

                //commandKey
                .andCommandKey(HystrixCommandKey.Factory.asKey("commandHelloWorld"))
                //command属性配置
                .andCommandPropertiesDefaults(HystrixPropertiesCommandDefault.Setter().withCircuitBreakerEnabled(true).withCircuitBreakerForceOpen(true))

                //线程池key
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("helloWorld_Poll"))
                //线程池属性配置
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(20).withMaxQueueSize(25))
        );
    }

其它的详细配置可参考https://github.com/Netflix/Hy... 后续也会整理对应的配置介绍文章。

2.HystrixCommand和分组、线程池三者的关系

Hystrix常用功能介绍
commandKey分组内唯一,HystrixCommand和分组、线程池是多对1的关系。分组和线程池没关系。

3.HystrixCommand如何执行?同步?异步?

同步
从helloWorld的例子可以看到,我们实例化了我们的HelloWorldCommand,调用了execute方法,从而执行了command的Run()。这种是同步的执行方式。

异步执行
在实际业务中,有时候我们会同时触发多个业务依赖的调用,而这些业务又相互不依赖这时候很适合并行执行,我们可以使用Future方式,调用command的queue()方法。
我们可以再写一个helloWorld2

public class CommandHelloWorld2 extends HystrixCommand<String> {

    private final String name;

    public CommandHelloWorld2(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected String run() {
        return "Hello " + name + "!";
    }
}

具体异步调用

    @Test
    public void test_02() throws ExecutionException, InterruptedException {
        Future<String> future1 = new CommandHelloWorld("world").queue();
        Future<String> future2 = new CommandHelloWorld2("world").queue();

        Assert.assertEquals("Hello world!",future1.get());
        Assert.assertEquals("Hello world!",future2.get());
    }

request cache的使用

先贴代码

public class CachedCommand extends HystrixCommand<String> {

    private String key;

    private static final HystrixCommandKey COMMANDKEY = HystrixCommandKey.Factory.asKey("CachedCommand_cmd");

    protected CachedCommand(String key){
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CachedCommand"))
                .andCommandKey(COMMANDKEY));
        this.key = key;
    }

    @Override
    protected String getCacheKey() {
        return this.key;
    }

    public static void flushCache(String key) {
        HystrixRequestCache.getInstance(COMMANDKEY,
                HystrixConcurrencyStrategyDefault.getInstance()).clear(key);
    }

    @Override
    protected String run() throws Exception {
        return "hello "+ key +" !";
    }
}

Hystrix的cache,个人的理解就是在上下文中,多次请求同一个command,返回值不会发生改变的时候可以使用。cache如果要生效,必须声明上下文

HystrixRequestContext context = HystrixRequestContext.initializeContext();

....... command 的调用 ........

context.shutdown();

清缓存,就是先获得到command然后把对应的key删除

 HystrixRequestCache.getInstance(COMMANDKEY,
                HystrixConcurrencyStrategyDefault.getInstance()).clear(key);

接下来看下完整的调用

    @Test
    public void test_no_cache(){
        HystrixRequestContext context = HystrixRequestContext.initializeContext();

        String hahahah = "hahahah";
        CachedCommand cachedCommand = new CachedCommand(hahahah);
        Assert.assertEquals("hello hahahah !", cachedCommand.execute());
        Assert.assertFalse(cachedCommand.isResponseFromCache());

        CachedCommand cachedCommand2 = new CachedCommand(hahahah);
        Assert.assertEquals("hello hahahah !", cachedCommand2.execute());
        Assert.assertTrue(cachedCommand2.isResponseFromCache());


        //清除缓存
        CachedCommand.flushCache(hahahah);

        CachedCommand cachedCommand3 = new CachedCommand(hahahah);
        Assert.assertEquals("hello hahahah !", cachedCommand3.execute());
        Assert.assertFalse(cachedCommand3.isResponseFromCache());

        context.shutdown();
    }

fallback

1.单个fallback

fallback就是当HystrixCommand 执行失败的时候走的后备逻辑,只要实现HystrixCommand 的fallback方法即可

Hystrix常用功能介绍

public class CommandWithFallBack extends HystrixCommand<String> {

    private final boolean throwException;

    public CommandWithFallBack(boolean throwException) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.throwException = throwException;
    }

    @Override
    protected String run() {
        if (throwException) {
            throw new RuntimeException("failure from CommandThatFailsFast");
        } else {
            return "success";
        }
    }

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


}

测试结果

    @Test
    public void testSuccess() {
        assertEquals("success", new CommandWithFallBack(false).execute());
    }

    @Test
    public void testFailure() {
        try {
            assertEquals("I'm fallback", new CommandWithFallBack(true).execute());
        } catch (HystrixRuntimeException e) {
            Assert.fail();
        }
    }
2.多级fallback

当我们执行业务的时候,有时候会有备用方案1、备用方案2,当备用方案1失败的时候启用备用方案2,所以可以使用多级fallback。
多级fallback没有名字那么神秘,说到底其实就是HystrixCommand1执行fallback1, fallback1的执行嵌入HystrixCommand2,当HystrixCommand2执行失败的时候,触发HystrixCommand2的fallback2,以此循环下去实现多级fallback,暂未上限,只要你的方法栈撑的起。

Hystrix常用功能介绍

代码实现

command1

public class CommandWithMultiFallBack1 extends HystrixCommand<String> {

    private final boolean throwException;

    public CommandWithMultiFallBack1(boolean throwException) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.throwException = throwException;
    }

    @Override
    protected String run() {
        if (throwException) {
            throw new RuntimeException("failure from CommandThatFailsFast");
        } else {
            return "success";
        }
    }

    @Override
    protected String getFallback() {
        return new CommandWithMultiFallBack2(true).execute();
    }


}

command2

public class CommandWithMultiFallBack2 extends HystrixCommand<String> {

    private final boolean throwException;

    public CommandWithMultiFallBack2(boolean throwException) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.throwException = throwException;
    }

    @Override
    protected String run() {
        if (throwException) {
            throw new RuntimeException("failure from CommandThatFailsFast");
        } else {
            return "I'm fallback1";
        }
    }

    @Override
    protected String getFallback() {
        return "I'm fallback2";
    }

调用测试

    @Test
    public void testMultiFailure(){
        try {
            assertEquals("I'm fallback2", new CommandWithMultiFallBack1(true).execute());
        } catch (HystrixRuntimeException e) {
            Assert.fail();
        }
    }
3.主次多HystrixCommand fallback

这里探讨的是 主Command里串行执行 多个Command时的fallback执行逻辑

Hystrix常用功能介绍

这里就不贴代码了,fallback的跳转也比较好理解,次command,不管任何一个执行失败都认为主command的run执行失败,进而进入主command的fallback

接入现有业务

上面的章节主要在解释如何使用HystrixCommand,但是我们在开发中都已经分好了各种业务的servie,如何套入这个Hystrix?

1.模拟业务场景

假设我们要加载商品详情页,需要加载商品信息、用户信息、店铺信息
接入Hystrix前的代码(代码有点天真,只是为了表述下意思)

//主功能类
public class GoodsService {

    private UserService userService = new UserService();
    private ShopService shopService = new ShopService();

    /**
     * 获取商品详情
     * @return
     */
    public GoodsDetailFrontModel getGoodsFrontDetail(){
        GoodsDetailFrontModel goodsDetailFrontModel = new GoodsDetailFrontModel();
        goodsDetailFrontModel.setTitle("这是一个测试商品");
        goodsDetailFrontModel.setPrice(10000L);

        UserModel userInfo = userService.getUserInfo(1000001L);
        ShopModel shopInfo = shopService.getShopInfo(2001L);

        goodsDetailFrontModel.setShopModel(shopInfo);
        goodsDetailFrontModel.setUserModel(userInfo);

        return goodsDetailFrontModel;
    }

}

//依赖的用户类
public class UserService {

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    public UserModel getUserInfo(Long userId){
        return new UserModel();
    }
}

下面我们对用户服务套入Hystrix,为了不侵入我们依赖的服务,我们新建一个门户类,包装Hystrix相关的代码

public class UserServiceFacade extends HystrixCommand<UserModel> {

    //原业务service
    private UserService userService = new UserService();

    private Long userId;

    protected UserServiceFacade() {
        super(HystrixCommandGroupKey.Factory.asKey("UserServiceFacade"));
    }

    @Override
    protected UserModel run() throws Exception {
        return userService.getUserInfo(userId);
    }


    /**
      *如果执行失败则返回游客身份
     **/
    @Override
    protected UserModel getFallback() {
        UserModel userModel = new UserModel();
        userModel.setName("游客");
        return userModel;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }
}

然后我们再看下主执行类

GoodsService

   /**
     * 获取商品详情
     * @return
     */
    public GoodsDetailFrontModel getGoodsFrontDetail(){
        GoodsDetailFrontModel goodsDetailFrontModel = new GoodsDetailFrontModel();
        goodsDetailFrontModel.setTitle("这是一个测试商品");
        goodsDetailFrontModel.setPrice(10000L);

        //原写法
        //UserModel userInfo = userService.getUserInfo(1000001L);

        //这里替换成了调用用户门面类
        UserServiceFacade userServiceFacade = new UserServiceFacade();
        userServiceFacade.setUserId(1000001L);
        UserModel userInfo = userServiceFacade.execute();


        ShopModel shopInfo = shopService.getShopInfo(2001L);

        goodsDetailFrontModel.setShopModel(shopInfo);
        goodsDetailFrontModel.setUserModel(userInfo);

        return goodsDetailFrontModel;
    }

上面的代码提供一个套入的思路,官方原生的Hystrix就是这样接入的,这里注意一点,HystrixCommand每次执行都需要new一个,不能使用单例,一个command实例只能执行一次,上面的代码也就是我们的userServiceFacade,每次执行都需要new一个新的对象。

总结

上面介绍了Hystrix的常规用法,也是我们公司目前的使用方式,官网还有HystrixObservableCommand的使用方式介绍,主要是rxjava的使用方式,获取到observable可以进行更加灵活的处理,这里就不介绍了。

回顾下,Hystrix能给我们带来什么好处
1.多业务依赖隔离,不会相互影响,并可以根据需要给不同的依赖分不同的线程资源
2.业务依赖fail-fast
3.依赖服务恢复,能合理感知并恢复对服务的依赖
4.对依赖服务限流,Hystrix对每个业务的依赖都包装成了一个command,并分配线程池,线程池的容量也就是能下发请求的能力,防止雪崩

使用的介绍先到这里了,后续大家有什么建议或者想法可以一起交流、碰撞。

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar