一、首先我们来实现一个HystrixCommand,示例代码如下:
package com.example.demo.hystrix.command;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.example.demo.utils.ObjectMapperInstance;
import com.example.demo.vo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolKey;
import lombok.Getter;
/**
* 只需要集成HystrixCommand即可,并覆写父类中的相应方法即可
* @author Administrator
*
*/
public class UserHystrixCommond extends HystrixCommand<User>{
@lombok.Setter @ Getter private String id;
public UserHystrixCommond(String id) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserCommandGroup"))
.andCommandKey(HystrixCommandKey.Factory.asKey("userCommand"))
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("userGroup")));
this.id = id;
}
/**
* 覆写run方法,此处写业务逻辑
*/
@Override
protected User run() throws Exception {
System.out.println("command user: "+Thread.currentThread().getName()+" is running......");
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("http://localhost:7901/user/"+id);
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
ObjectMapper mapper = ObjectMapperInstance.getInstance();
return mapper.readValue(body, User.class);
}
/**
* 服务降级方法,当调用服务发生异常时,会调用该降级方法
*/
@Override
protected User getFallback() {
System.out.println("进入fallback方法!");
User u = new User();
u.setUsername("刘先生");
u.setId(1l);
return u;
}
}
下面,我们通过代码来看下,如何在代码中进行Hystrix的配置。
Hystrix是用Setter静态类来实现配置的,Setter类源码如下:
final public static class Setter {
protected final HystrixCommandGroupKey groupKey;
protected HystrixCommandKey commandKey;
protected HystrixThreadPoolKey threadPoolKey;
protected HystrixCommandProperties.Setter commandPropertiesDefaults;
protected HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults;
……省略……
}
从上面的代码中,我们可以看到,Hystrix的代码中支持上面几种类型的配置,下面我们来逐步看下。
HystrixCommandGroupKey:该配置是用来配置Hystrix的Group的,关于Hystrix的Group后面会介绍到,配置方式如下:
Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserCommandGroup"))
HystrixCommandKey:用来配置Hystrix的commandKey,后面会做具体的介绍,配置方式如下:
Setter..andCommandKey(HystrixCommandKey.Factory.asKey("userCommand"))
HystrixThreadPoolKey:用来配置Hystrix的隔离资源池的名字,如果不配的话,默认为类名,配置方式如下:
Setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("userGroup"))
HystrixCommandProperties:用来配置Hystrix的服务降级策略以及metrics和circuitBreaker,配置方式如下:
Setter.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)// 配置资源隔离策略
.withExecutionIsolationSemaphoreMaxConcurrentRequests(15))// 配置信号量最大的并发请求数
Hystrix支持的配置项很多,可配置项如下:
public abstract class HystrixCommandProperties {
private static final Logger logger = LoggerFactory.getLogger(HystrixCommandProperties.class);
/* defaults */
/* package */ static final Integer default_metricsRollingStatisticalWindow = 10000;// default => statisticalWindow: 10000 = 10 seconds (and default of 10 buckets so each bucket is 1 second)
private static final Integer default_metricsRollingStatisticalWindowBuckets = 10;// default => statisticalWindowBuckets: 10 = 10 buckets in a 10 second window so each bucket is 1 second
private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;// default => statisticalWindowVolumeThreshold: 20 requests in 10 seconds must occur before statistics matter
private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;// default => sleepWindow: 5000 = 5 seconds that we will sleep before trying again after tripping the circuit
private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;// default => errorThresholdPercentage = 50 = if 50%+ of requests in 10 seconds are failures or latent then we will trip the circuit
private static final Boolean default_circuitBreakerForceOpen = false;// default => forceCircuitOpen = false (we want to allow traffic)
/* package */ static final Boolean default_circuitBreakerForceClosed = false;// default => ignoreErrors = false
private static final Integer default_executionTimeoutInMilliseconds = 1000; // default => executionTimeoutInMilliseconds: 1000 = 1 second
private static final Boolean default_executionTimeoutEnabled = true;
private static final ExecutionIsolationStrategy default_executionIsolationStrategy = ExecutionIsolationStrategy.THREAD;
private static final Boolean default_executionIsolationThreadInterruptOnTimeout = true;
private static final Boolean default_executionIsolationThreadInterruptOnFutureCancel = false;
private static final Boolean default_metricsRollingPercentileEnabled = true;
private static final Boolean default_requestCacheEnabled = true;
private static final Integer default_fallbackIsolationSemaphoreMaxConcurrentRequests = 10;
private static final Boolean default_fallbackEnabled = true;
private static final Integer default_executionIsolationSemaphoreMaxConcurrentRequests = 10;
private static final Boolean default_requestLogEnabled = true;
private static final Boolean default_circuitBreakerEnabled = true;
private static final Integer default_metricsRollingPercentileWindow = 60000; // default to 1 minute for RollingPercentile
private static final Integer default_metricsRollingPercentileWindowBuckets = 6; // default to 6 buckets (10 seconds each in 60 second window)
private static final Integer default_metricsRollingPercentileBucketSize = 100; // default to 100 values max per bucket
private static final Integer default_metricsHealthSnapshotIntervalInMilliseconds = 500; // default to 500ms as max frequency between allowing snapshots of health (error percentage etc)
@SuppressWarnings("unused") private final HystrixCommandKey key;
private final HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold; // number of requests that must be made within a statisticalWindow before open/close decisions are made using stats
private final HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds; // milliseconds after tripping circuit before allowing retry
private final HystrixProperty<Boolean> circuitBreakerEnabled; // Whether circuit breaker should be enabled.
private final HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage; // % of 'marks' that must be failed to trip the circuit
private final HystrixProperty<Boolean> circuitBreakerForceOpen; // a property to allow forcing the circuit open (stopping all requests)
private final HystrixProperty<Boolean> circuitBreakerForceClosed; // a property to allow ignoring errors and therefore never trip 'open' (ie. allow all traffic through)
private final HystrixProperty<ExecutionIsolationStrategy> executionIsolationStrategy; // Whether a command should be executed in a separate thread or not.
private final HystrixProperty<Integer> executionTimeoutInMilliseconds; // Timeout value in milliseconds for a command
private final HystrixProperty<Boolean> executionTimeoutEnabled; //Whether timeout should be triggered
private final HystrixProperty<String> executionIsolationThreadPoolKeyOverride; // What thread-pool this command should run in (if running on a separate thread).
private final HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests; // Number of permits for execution semaphore
private final HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests; // Number of permits for fallback semaphore
private final HystrixProperty<Boolean> fallbackEnabled; // Whether fallback should be attempted.
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout; // Whether an underlying Future/Thread (when runInSeparateThread == true) should be interrupted after a timeout
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnFutureCancel; // Whether canceling an underlying Future/Thread (when runInSeparateThread == true) should interrupt the execution thread
private final HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds; // milliseconds back that will be tracked
private final HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets; // number of buckets in the statisticalWindow
private final HystrixProperty<Boolean> metricsRollingPercentileEnabled; // Whether monitoring should be enabled (SLA and Tracers).
private final HystrixProperty<Integer> metricsRollingPercentileWindowInMilliseconds; // number of milliseconds that will be tracked in RollingPercentile
private final HystrixProperty<Integer> metricsRollingPercentileWindowBuckets; // number of buckets percentileWindow will be divided into
private final HystrixProperty<Integer> metricsRollingPercentileBucketSize; // how many values will be stored in each percentileWindowBucket
private final HystrixProperty<Integer> metricsHealthSnapshotIntervalInMilliseconds; // time between health snapshots
private final HystrixProperty<Boolean> requestLogEnabled; // whether command request logging is enabled.
private final HystrixProperty<Boolean> requestCacheEnabled; // Whether request caching is enabled.
}
上面的代码分为两部分,上面那部分是不配置的时候,系统设置的默认值,下面那部分,是我们自定义配置时,可以设置的属性。
HystrixThreadPoolProperties:用来设置线程池的配置。当我们使用Thread来进行资源隔离的时候,这部分配置会生效。配置方式如下:
Setter.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(15)
.withMaxQueueSize(5))
通过上面几个步骤,就完成了Hystrix的基本配置了,完整的示例如下:
Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserCommandGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)
.withExecutionIsolationSemaphoreMaxConcurrentRequests(15))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(15)
.withMaxQueueSize(5))