基础概念随便搜以下就有了,但是具体配置鲜有介绍,本文直接贴配置用法好了。
package com.example.sp;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
@RestController
public class HiController {
@Value("${server.port}")
private int port;
@Autowired
HiService hiService;
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError", commandProperties = {
// ------------ Execution相关的属性的配置 --------------- //
// 隔离策略,默认是THREAD, 可选THREAD|SEMAPHORE
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),
// 最大并发请求数,默认10,该参数当使用ExecutionIsolationStrategy.SEMAPHORE策略时才有效。
// 如果达到最大并发请求数,请求会被拒绝。理论上选择semaphoresize的原则和选择threadsize一致,
// 但选用semaphore时每次执行的单元要比较小且执行速度快(ms级别),否则的话应该用thread。semaphore应该占整个容器(tomcat)的线程池的一小部分。
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "10"),
// 是否启用超时,默认true
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_TIMEOUT_ENABLED, value = "true"),
// 发生超时是是否中断,默认true
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_INTERRUPT_ON_TIMEOUT, value = "true"),
// 执行超时时间,默认1000
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "3000"),
// ------------ Fallback相关的属性 --------------- //
// 如果fallback并发数达到该设置值,请求会被拒绝和抛出异常。默认10。
@HystrixProperty(name = HystrixPropertiesManager.FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "10"),
// 执行失败或者请求被拒绝,是否会尝试调用hystrixCommand.getFallback() 。默认true
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
// ------------- Circuit Breaker相关的属性 ------------------//
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true"),
// 滑动窗口的大小,指定时间内错误数触发熔断的阀值,默认为20
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
// 过多长时间,熔断器再次检测是否开启,默认为5000,即5s钟
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000"),
// 错误率,默认50%
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "60"),
// 强制打开熔断器,如果打开这个开关,那么拒绝所有request,默认false ???
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_FORCE_OPEN, value = "false"),
// 强制关闭熔断器 如果这个开关打开,circuit将一直关闭且忽略circuitBreaker.errorThresholdPercentage
@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_FORCE_CLOSED, value = "60"),
// ------------ Request Context 相关参数 --------------- //
// 默认true,需要重载getCacheKey(),返回null时不缓存
@HystrixProperty(name = HystrixPropertiesManager.REQUEST_CACHE_ENABLED, value = "true"),
// 默认true, 记录日志到HystrixRequestLog
@HystrixProperty(name = HystrixPropertiesManager.REQUEST_LOG_ENABLED, value = "true"),
// ------------- Metrics相关的属性 ------------------//
// 设置统计的时间窗口值的,毫秒值,默认60s
@HystrixProperty(name = HystrixPropertiesManager.METRICS_ROLLING_PERCENTILE_TIME_IN_MILLISECONDS, value = "60000"),
// 对窗口值进行分桶,默认6,即10s为一个分桶,桶内包含了success,failure,timeout,rejection的次数的统计信息
@HystrixProperty(name = HystrixPropertiesManager.METRICS_ROLLING_PERCENTILE_NUM_BUCKETS, value = "6"),
// 一个分桶的大小,默认100。例:bucketSize=100 window=10s,
// 若这10s里有500次执行,只有最后100次执行会被统计到bucket里去。增加该值会增加内存开销以及排序的开销。
@HystrixProperty(name = HystrixPropertiesManager.METRICS_ROLLING_PERCENTILE_BUCKET_SIZE, value = "100"),
// 记录health 快照(用来统计成功和错误绿)的间隔,默认500ms
@HystrixProperty(name = HystrixPropertiesManager.METRICS_HEALTH_SNAPSHOT_INTERVAL_IN_MILLISECONDS, value = "500"),
}, threadPoolProperties = {
/**
* 线程数默认值10适用于大部分情况(有时可以设置得更小),如果需要设置得更大,那有个基本得公式可以follow:<br>
* requests per second at peak when healthy × 99th percentile latency in seconds
* + some breathing room <br>
* 每秒最大支撑的请求数 (99%平均响应时间 + 缓存值) <br>
* 比如:每秒能处理1000个请求,99%的请求响应时间是60ms,那么公式是: 1000 (0.060+0.012)<br>
* 基本得原则时保持线程池尽可能小,他主要是为了释放压力,防止资源被阻塞。<br>
* 当一切都是正常的时候,线程池一般仅会有1到2个线程激活来提供服务
*/
// 核心线程数,默认10
@HystrixProperty(name = HystrixPropertiesManager.CORE_SIZE, value = "10"),
// 并发执行的最大线程数,默认10
@HystrixProperty(name = HystrixPropertiesManager.MAXIMUM_SIZE, value = "10"),
// 的最大队列数,当设为-1,会使用SynchronousQueue,值为正时使用LinkedBlcokingQueue。该设置只会在初始化时有效,之后不能修改threadpool的queue
// size,除非reinitialising thread executor。默认-1。
@HystrixProperty(name = HystrixPropertiesManager.MAX_QUEUE_SIZE, value = "-1"),
// 即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。因为maxQueueSize不能被动态修改,这个参数将允许我们动态设置该值。
// if maxQueueSize == -1,该字段将不起作用,默认5
@HystrixProperty(name = HystrixPropertiesManager.QUEUE_SIZE_REJECTION_THRESHOLD, value = "-1"),
// 默认1,空闲线程存活时间
@HystrixProperty(name = HystrixPropertiesManager.KEEP_ALIVE_TIME_MINUTES, value = "1"),
@HystrixProperty(name = "maximumSize", value = "100"), })
public String hi(@RequestParam String name) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
}
return Thread.currentThread().getName() + " hi, " + name;
}
public String hiError(String name) {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (Exception e) {
}
return Thread.currentThread().getName() + " was wrong, " + name;
}
}