hystrix是奈飞开源的熔断限流组件,虽然线上停止了维护,但是使用的用户还是挺多的
很多线上的系统还在使用它。
它的原理是基于rxjava启动线程池(信号量)去处理每个请求,做到服务的隔离
package cn.beckbi;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class Example1 extends HystrixCommand<String>
{
private String name;
public Example1(String name) {
super(
HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(name))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD
)
).andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties.Setter()
.withCoreSize(10)
.withMaxQueueSize(100)
.withMaximumSize(100)
)
);
this.name = name;
}
@Override
protected String run() {
/*
try{
TimeUnit.MICROSECONDS.sleep(1);
}catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("get data");
return "HystrixCommandName:"+this.name+"## currentThreadName="+Thread.currentThread().getName();
}
@Override
protected String getFallback() {
return Thread.currentThread().getName()+"失败了";
}
@Override
protected String getCacheKey() {
return String.valueOf(this.name);
}
public static void main(String[] args) throws Exception{
HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
Example1 example1 = new Example1("test1");
String result = example1.execute();
System.out.println("## currentThreadName="+Thread.currentThread().getName());
System.out.println(result);
Future<String> future = new Example1("test2").queue();
System.out.println("future:"+future.get());
result = new Example1("test3").execute();
future = new Example1("test3").queue();
System.out.println("future:"+future.get());
hystrixRequestContext.shutdown();
}
}
服务中使用
@GetMapping("/user/{uid}")
@HystrixCommand(fallbackMethod = "defaultCall",
commandProperties = {
@HystrixProperty(
name = "execution.isolation.strategy",
value = "THREAD"
)
})
public String info(@PathVariable long uid) throws JsonProcessingException {
@Data
class IdData {
private String id;
private Long uid;
}
IdData idData = new IdData();
idData.setUid(uid);
idData.setId("123");
return mapper.writeValueAsString(idData);
}
public String defaultCall(long uid) {
return "failed:"+uid;
}
也可以和feigin一起使用
访问线上端口http://127.0.0.1:7330/hystrix
输入监控页面http://127.0.0.1:7330/actuator/hystrix.stream
配置页面
http://127.0.0.1:7330/hystrix/monitor?stream=http%3A%2F%2F127.0.0.1%3A7330%2Factuator%2Fhystrix.stream
服务监控
spring.application.name=khystrix-spring
server.port=7330
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
hystrix.dashboard.proxy-stream-allow-list=localhost
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。