微服务架构整理-(九、SpringCloud实战之Hystrix [2])

SpringCloud实战之Hystrix [2]-自定义异常熔断处理

    • 自定义熔断机制
    • 测试
      • 同步调用
      • 异步调用
    • 总结

上文中,对异常处理是使用注解@HystrixCommand完成的,当然也可以不使用此注解来完成,我们可以自定义一个机制处理超时,抛出异常等情况。整个流程可以分为如下几步:

  • 定义一个类继承HystrixCommand,传入的泛型为目标函数返回的类型
  • 在自定义的类里覆盖getFallback方法实现熔断逻辑
  • 在自定义在run方法中调用远程服务
  • 在getFallback方法中调用getExecutionException方法获取服务抛出的异常

自定义熔断机制

这里给出一个案例,按照以上4个步骤,自定义一个类,如下所示:

//1,继承HystrixCommand,泛型为目标函数返回的类型
public class OrderHystrixCommand extends HystrixCommand<Product> {
    private RestTemplate restTemplate;
    private String url;
    public OrderHystrixCommand(Setter setter, RestTemplate restTemplate, String url) {
        super(setter);
        this.restTemplate = restTemplate;
        this.url = url;
    }
    /**
     * 调用远程服务
     */
    @Override
    protected Product run() throws Exception {
    	//3,调用远程服务
        Product product = restTemplate.getForObject(url, Product.class);
        // 这里故意弄一个超时,强迫其服务降级,真正用的时候可以去掉
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return product;
    }

    /**
     *2,覆盖getFallback方法,实现熔断逻辑,这里定义一个fake product 并返回
     * 降级方法:超时,异常,不可用等情况时调用
     */
    @Override
    public Product getFallback() {
    //4,如果服务断有异常,可以在此方法中拿到服务端抛出的异常。
        /* Throwable serviceException = super.getExecutionException();
        System.out.println("Exception from service provider : " + serviceException.getMessage());*/
        Product product = new Product();
        product.setId(0L);
        product.setName("A fake product");
        product.setPrice(new BigDecimal(0));
        return product;
    }
}

测试

在目标方法中使用自定义的类实例调用execute/queue方法,其中execute是同步调用,而queue是异步调用。继续使用之前findById的例子,去掉@HystrixCommand 注解,当超出1.5秒(默认1秒)没有响应时,执行服务降级。这里设置超时时间用的是HystrixCommandProperties.Setter, 其原理同注解中的属性commandProperties一样。

同步调用

  @GetMapping(value = "/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = null;
        String url = "http://service-product/product/" + id;
        // 默认超时为1000,可能设成1500
        HystrixCommandProperties.Setter commandProperty = HystrixCommandProperties.Setter();
        commandProperty.withExecutionTimeoutInMilliseconds(1500);

        setter = com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(""));
        setter.andCommandPropertiesDefaults(commandProperty);
        OrderHystrixCommand orderHystrixCommand = new OrderHystrixCommand(
                setter,
                restTemplate, url);
        //同步调用        
        product = orderHystrixCommand.execute();
        System.out.println("success");
        return product;
    }

异步调用

@GetMapping(value = "/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = null;
        String url = "http://service-product/product/" + id;
        // 默认超时为1000,可能设成1500
        HystrixCommandProperties.Setter commandProperty = HystrixCommandProperties.Setter();
        commandProperty.withExecutionTimeoutInMilliseconds(1500);

        setter = com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(""));
        setter.andCommandPropertiesDefaults(commandProperty);
        OrderHystrixCommand orderHystrixCommand = new OrderHystrixCommand(
                setter,
                restTemplate, url);
        //异步调用
        Future<Product> queue = orderHystrixCommand.queue();
        /**
         * 这里可以根据具体的需求加一些业务处理,将处理结果再与返程服务返回的结果进行整合
         */
        try {
            //阻塞式方法,一直等到拿到返回值为止
            product = queue.get();
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
        System.out.println("success");
        return product;
    }

总结

使用自定义熔断处理机制没法处理消费者断的异常,例如findById函数中的业务逻辑有异常,是不会进入降级方法的,这里有别于使用注解的方法。关于Hystrix自定义异常熔断处理机制就介绍到这,大家根据项目的具体需求来衡量是否需要进行自定义。下一篇将介绍Hystrix的监控表盘。最后,希望本文能帮助大家,祝大家在IT之路上少走弯路,一路绿灯不堵车,测试一性通过,bug秒解!
源码下载

你可能感兴趣的:(java,微服务,spring,cloud,架构,微服务,Hystrix)