hystrix
1.概述
hystrix是一个命令运行的隔离器,命令执行时可以提供容错,隔离和降级功能。
2.命令
命令由HystrixInvokableInfo接口定义
/** * Copyright 2014 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.netflix.hystrix; import java.util.List; public interface HystrixInvokableInfo<R> { HystrixCommandGroupKey getCommandGroup(); HystrixCommandKey getCommandKey(); HystrixThreadPoolKey getThreadPoolKey(); String getPublicCacheKey(); //have to use public in the name, as there's already a protected {@link AbstractCommand#getCacheKey()} method. HystrixCollapserKey getOriginatingCollapserKey(); HystrixCommandMetrics getMetrics(); HystrixCommandProperties getProperties(); boolean isCircuitBreakerOpen(); boolean isExecutionComplete(); boolean isExecutedInThread(); boolean isSuccessfulExecution(); boolean isFailedExecution(); Throwable getFailedExecutionException(); boolean isResponseFromFallback(); boolean isResponseTimedOut(); boolean isResponseShortCircuited(); boolean isResponseFromCache(); boolean isResponseRejected(); boolean isResponseSemaphoreRejected(); boolean isResponseThreadPoolRejected(); List<HystrixEventType> getExecutionEvents(); int getNumberEmissions(); int getNumberFallbackEmissions(); int getNumberCollapsed(); int getExecutionTimeInMilliseconds(); long getCommandRunStartTimeInNanos(); ExecutionResult.EventCounts getEventCounts(); }
接口中定义了命令的调用,命令的统计信息,命令的事件。这些属性最终时通过properties的一个子类获取,它的实现类HystrixCommand定义了基本的统计和熔断操作,具体的命令由用户自定义子类来实现。自定义子类中实现命令的具体调用和
熔断后的操作。
public class QueryOrderIdCommand extends HystrixCommand<Integer> { private final static Logger logger = LoggerFactory.getLogger(QueryOrderIdCommand.class); private OrderServiceProvider orderServiceProvider; public QueryOrderIdCommand(OrderServiceProvider orderServiceProvider) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("orderService")) .andCommandKey(HystrixCommandKey.Factory.asKey("queryByOrderId")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withCircuitBreakerRequestVolumeThreshold(10)//至少有10个请求,熔断器才进行错误率的计算 .withCircuitBreakerSleepWindowInMilliseconds(5000)//熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试 .withCircuitBreakerErrorThresholdPercentage(50)//错误率达到50开启熔断保护 .withExecutionTimeoutEnabled(true)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties .Setter().withCoreSize(10))); this.orderServiceProvider = orderServiceProvider; } @Override protected Integer run() { return orderServiceProvider.queryByOrderId(); } @Override protected Integer getFallback() { return -1; } }
熔断器配置
1)、circuitBreaker.enabled
是否启用熔断器,默认是TRUE。
2) 、circuitBreaker.forceOpen
熔断器强制打开,始终保持打开状态,不关注熔断开关的实际状态。默认值FLASE。
3)、circuitBreaker.forceClosed
熔断器强制关闭,始终保持关闭状态,不关注熔断开关的实际状态。默认值FLASE。
4)、circuitBreaker.errorThresholdPercentage
错误率,默认值50%,例如一段时间(10s)内有100个请求,其中有54个超时或者异常,那么这段时间内的错误率是54%,大于了默认值50%,这种情况下会触发熔断器打开。
5)、circuitBreaker.requestVolumeThreshold
默认值20。含义是一段时间内至少有20个请求才进行errorThresholdPercentage计算。比如一段时间了有19个请求,且这些请求全部失败了,错误率是100%,但熔断器不会打开,总请求数不满足20。
6)、circuitBreaker.sleepWindowInMilliseconds
半开状态试探睡眠时间,默认值5000ms。如:当熔断器开启5000ms之后,会尝试放过去一部分流量进行试探,确定依赖服务是否恢复。
当达到熔断条件时回调用fallcack来进行降级处理。
3.线程隔离
Hystrix通过命令模式对发送请求的对象和执行请求的对象进行解耦,将不同类型的业务请求封装为对应的命令请求。如订单服务查询商品,查询商品请求->商品Command;商品服务查询库存,查询库存请求->库存Command。并且为每个类型的Command配置一个线程池,当第一次创建Command时,根据配置创建一个线程池,并放入ConcurrentHashMap.