1、引入maven依赖
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core -->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>
2、创建一个继承HystrixCommand的类
package com.xueyou.hystrixdemo.v4;
import com.netflix.hystrix.HystrixCommand;
public class CommandHelloworldWithFallBackThread extends HystrixCommand<String> {
private String name;
private int timeIndex;
public CommandHelloworldWithFallBackThread(Setter setter, String name) {
super(setter);
this.name = name;
}
@Override
protected String getFallback() {
return "fall back " + name;
}
@Override
protected String run() {
try {
Thread.currentThread().sleep(400);
} catch (InterruptedException e) {
}
return "ok " + name;
}
}
3、创建一个测试类,测试命令
package com.xueyou.hystrixdemo.v4;
import com.netflix.hystrix.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HelloWorldFallBackThread {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
String s = new CommandHelloworldWithFallBackThread(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup1"))
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("threadpoolwithfallback"))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(3)
.withMaximumSize(6)
.withAllowMaximumSizeToDivergeFromCoreSize(true))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(1000)
.withCircuitBreakerSleepWindowInMilliseconds(5000)
.withCircuitBreakerErrorThresholdPercentage(50)
.withCircuitBreakerRequestVolumeThreshold(100))
, Thread.currentThread().getName()).execute();
System.out.println(s);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
};
ExecutorService executorService = Executors.newFixedThreadPool(10);
int i = 100;
while (i > 0) {
executorService.submit(runnable);
/* try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
*/
i--;
}
executorService.shutdown();
}
}
4、运行结果:
fall back pool-1-thread-8
fall back pool-1-thread-7
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-8
fall back pool-1-thread-7
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
ok pool-1-thread-10
ok pool-1-thread-2
ok pool-1-thread-9
ok pool-1-thread-1
ok pool-1-thread-4
ok pool-1-thread-6
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
fall back pool-1-thread-6
fall back pool-1-thread-2
fall back pool-1-thread-4
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
fall back pool-1-thread-6
fall back pool-1-thread-6
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
ok pool-1-thread-7
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
ok pool-1-thread-8
ok pool-1-thread-5
ok pool-1-thread-3
ok pool-1-thread-9
ok pool-1-thread-10
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-9
fall back pool-1-thread-10
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-3
fall back pool-1-thread-5
fall back pool-1-thread-10
ok pool-1-thread-4
ok pool-1-thread-7
ok pool-1-thread-2
ok pool-1-thread-6
ok pool-1-thread-1
ok pool-1-thread-8