Hystrix杂谈(顺带瞎BB)

初窥熔断器

熔断器的作用

  • 对来自依赖的延迟和故障进行防护和控制——这些依赖通常都是通过网络访问的
  • 阻止故障的连锁反应
  • 快速失败并迅速恢复
  • 回退并优雅降级
  • 提供近实时的监控与告警())

Hystrix的方法

execute()和queue() 适用于HystrixCommand对象,而observe()和toObservable()适用于HystrixObservableCommand对象

  1. excute:以同步堵塞方式执行run()
  2. queue:以异步非阻塞方式执行run()
  3. oberve:这个是订阅模式。调用observe()自动触发执行run()/construct(),无论是否存在订阅者(异步模式)可以发送多条数据
  4. toObervable:这个是订阅模式。调用toObservable()不会立即触发执行run()/construct(),必须有订阅者订阅Observable时才会执行。(异步模式)可以发送多条数据

代码示例

java代码
import com.netflix.hystrix.*;
import java.util.Random;

public class MyHystrixDemo extends HystrixCommand<String> {
    public static String groupKey = "HystrixCommand";
    public static String threadKey = "ThreadPool";

    protected MyHystrixDemo(String commandKey) {
        /**
         * 设置熔断相关
         *CircuitBreaker是熔断器,所有相关配置项都是设置这个的
         *withExecutionTimeoutInMilliseconds这个是设置的超时时间,限流的一部分
         *  最最重要的withCircuitBreakerSleepWindowInMilliseconds设置断路器的回复时间,
         */
//        HystrixCommandProperties.Setter commandPorperties=HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(50)
//                .withExecutionTimeoutEnabled(true)
//                .withExecutionTimeoutInMilliseconds(5000)
//                .withCircuitBreakerSleepWindowInMilliseconds(5000);
//        HystrixThreadPoolProperties.Setter threadPoolProperties=HystrixThreadPoolProperties.Setter().withCoreSize(10);
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadKey))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
//                        .withCircuitBreakerErrorThresholdPercentage(50)
                        .withExecutionTimeoutEnabled(true)
                        .withExecutionTimeoutInMilliseconds(2000)
                        .withCircuitBreakerRequestVolumeThreshold(1)
//                        .withCircuitBreakerErrorThresholdPercentage(1)
                        .withCircuitBreakerSleepWindowInMilliseconds(50000))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10)));
    }

    @Override
    public String getFallback() {
        return "Error:熔断";
    }

    @Override
    protected String run() throws Exception {
        Random random = new Random();
        Integer time = random.nextInt(10);
        System.out.println("Time :" + time.toString());
        Thread.sleep(time * 1000);
        return "Hello World!";
    }
}

spring的代码
//注解实现Hystrix功能
@EnableHystrix


//注解实现熔断 
@Component
public class HelloApiProxy {

//    @HystrixCommand(commandProperties={
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="500"),
//            @HystrixProperty(name="execution.timeout.enabled",value="true")
//    }, fallbackMethod = "fallback")
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    },threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "10"),
            @HystrixProperty(name = "maxQueueSize", value = "20"),
            @HystrixProperty(name = "keepAliveTimeMinutes", value = "0"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
            @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
    }, fallbackMethod = "fallback")
    public String getString(){
        Random random=new Random();
        Integer time = random.nextInt(10);
        try {
            Thread.sleep(time*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Time:"+time.toString()+"  helloApiProxy";
    }

    public String  fallback(){
        return "fallback";
    }
}

    protected String run() throws Exception {
        Random random = new Random();
        Integer time = random.nextInt(10);
        System.out.println("Time :" + time.toString());
        Thread.sleep(time * 1000);
        return "Hello World!";
    }
}

注解的实现原理

@AutoConfiguration扫描到Hystrix的包,然后通过factory的包去自动化注入Configure;在configure中有关于HystrixCommand的切面,然后通过反射去实现了相关的功能

Hystrix注解相关的官网https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration

乱言:
这礼拜很忙,学了很多;但是能写下来的很少。
这礼拜学习了一下aliyun相关的服务,edas(之前用过),云效(之前没用过现在熟悉一下),rocketMQ也看了一下但是还是不是很理解。
spring boot相关的内容:理解了一下jvm的内存模型,看了一下内存溢出和内存泄漏的相关内容,Hystrix

我去,感觉这礼拜很忙学了很多;为啥写下来也就这么点,还要继续努力了。哎。。。任重而道远,全怪阿里爸爸把东西封装了那么多,导致能学的很少,只要用就行了;感觉不努力就随时失业了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值