Hystrix系列-3-Hystrix的配置-通過代碼構造配置

一、首先我們來實現一個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))
發佈了129 篇原創文章 · 獲贊 371 · 訪問量 160萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章