Hystrix 熔断框架基础示例

1. 业务类,其在SpringBoot 场景下,将由IOC 生成对象实例:

//模拟实际业务类,其工作会有延迟
class MyService{
    public String work(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        return "工作结果:"+Math.random()+":"+Thread.currentThread().getName();
    }
}

2. 自定义Command类,在SpringCloud场景下,由注解生成实例:

//自定义的Command
class MyCommand extends HystrixCommand {
    private String result;
    private MyService service;
    public MyCommand(MyService service) {//业务对象传入到
        //设定线程池的名字
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Default"))
                .andThreadPoolPropertiesDefaults(
                        HystrixThreadPoolProperties.Setter()//线程池属性设定器
                        .withCoreSize(5)// 设置线程池大小
                        .withMaxQueueSize(2))// 设置最大等待队列大小,最终会有七个请求执行
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()//命令属性设定器

                        .withExecutionTimeoutInMilliseconds(3000)));// 设置timeout时长,默认1000,一个command运行超出这个时间,就被认为是timeout

       this.service=service;
    }
    @Override
    protected String run() throws Exception {
        //在线程池中的执行业务,如果超时,则进入降级
        result = service.work();
        return result;
    }
    @Override
    protected String getFallback() {
        return "降级处理后的结果";
    }
}

3. 启动测试类:

public class HyApp {
    public static void main(String[] args) {
        final MyService myService=new MyService();
        //打开10个线程(生产环境下,此线程由tomcat线程池分配)
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    MyCommand reject=new MyCommand(myService);
                    String rs=reject.execute();
                    System.out.println(Thread.currentThread().getName()+":"+rs);
                }
            }.start();

        }

    }
}

4. 完整的代码实例及运行结果:

package cn.johnyu.hystrix;

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

public class HyApp {
    public static void main(String[] args) {
        final MyService myService=new MyService();
        //打开10个线程(生产环境下,此线程由tomcat线程池分配)
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    MyCommand reject=new MyCommand(myService);
                    String rs=reject.execute();
                    System.out.println(Thread.currentThread().getName()+":"+rs);
                }
            }.start();

        }

    }
}
//模拟实际业务类,其工作会有延迟
class MyService{
    public String work(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        return "工作结果:"+Math.random()+":"+Thread.currentThread().getName();
    }
}
//自定义的Command
class MyCommand extends HystrixCommand {
    private String result;
    private MyService service;
    public MyCommand(MyService service) {//业务对象传入到
        //设定线程池的名字
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Default"))
                .andThreadPoolPropertiesDefaults(
                        HystrixThreadPoolProperties.Setter()//线程池属性设定器
                        .withCoreSize(5)// 设置线程池大小
                        .withMaxQueueSize(2))// 设置最大等待队列大小,最终会有七个请求执行
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()//命令属性设定器

                        .withExecutionTimeoutInMilliseconds(3000)));// 设置timeout时长,默认1000,一个command运行超出这个时间,就被认为是timeout

       this.service=service;
    }
    @Override
    protected String run() throws Exception {
        //在线程池中的执行业务,如果超时,则进入降级
        result = service.work();
        return result;
    }
    @Override
    protected String getFallback() {
        return "降级处理后的结果";
    }
}

运行结果:

Thread-8:降级处理后的结果
Thread-4:降级处理后的结果
Thread-7:降级处理后的结果
Thread-9:工作结果:0.2588853436469356:hystrix-Default-5
Thread-6:工作结果:0.8096057483901064:hystrix-Default-2
Thread-2:工作结果:0.9524698952843108:hystrix-Default-1
Thread-1:工作结果:0.12272654787062287:hystrix-Default-4
Thread-0:工作结果:0.4341197447966666:hystrix-Default-3
Thread-5:工作结果:0.11165337665390496:hystrix-Default-4
Thread-3:工作结果:0.11519664052115941:hystrix-Default-3

5. 使用信号量的情况:

package cn.johnyu.hystrix;

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

public class HyApp {
    public static void main(String[] args) {
        final MyService myService=new MyService();
        //打开10个线程(生产环境下,此线程由tomcat线程池分配)
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    MyCommand reject=new MyCommand(myService);
                    String rs=reject.execute();
                    System.out.println(Thread.currentThread().getName()+":"+rs);
                }
            }.start();

        }

    }
}
//模拟实际业务类,其工作会有延迟
class MyService{
    public String work(){
        try {
            Thread.sleep(900);
        } catch (InterruptedException e) {

        }
        return "工作结果:"+Math.random()+":"+Thread.currentThread().getName();
    }
}
//自定义的Command
class MyCommand extends HystrixCommand {
    private String result;
    private MyService service;
    public MyCommand(MyService service) {//业务对象传入到
        //设定线程池的名字
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Default"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()//命令属性设定器
                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)//信号量的情况
                        .withExecutionIsolationSemaphoreMaxConcurrentRequests(5)
                        .withExecutionTimeoutInMilliseconds(1000)));// 设置timeout时长,默认1000,一个command运行超出这个时间,就被认为是timeout

        this.service=service;
    }
    @Override
    protected String run() throws Exception {
        //在线程池中的执行业务,如果超时,则进入降级
        result = service.work();
        return result;
    }
    @Override
    protected String getFallback() {
        return "降级处理后的结果";
    }
}

运行结果:

Thread-2:降级处理后的结果
Thread-0:降级处理后的结果
Thread-6:降级处理后的结果
Thread-9:降级处理后的结果
Thread-4:降级处理后的结果
Thread-8:工作结果:0.5972827654107769:Thread-8
Thread-5:工作结果:0.8957667328176903:Thread-5
Thread-1:工作结果:0.34745913383072813:Thread-1
Thread-7:工作结果:0.31546350244527444:Thread-7
Thread-3:工作结果:0.3582107684563951:Thread-3

你可能感兴趣的:(Hystrix 熔断框架基础示例)