昨天我们介绍了如何使用hystrix,并且对其有了初步的认识,今天我们进一步来了解hystrix.
下面是对hystrx 的设计目标和原则的一些描述,摘自网络:
https://www.cnblogs.com/cjsblog/p/9391819.html,也是hystrix在github 上的wiki翻译:
Hystrix被设计的目标是:
Hystrix设计原则是什么
Hystrix是如何实现它的目标的
通过上面的介绍,我们知道hystrix 有多种方式来实现服务的隔离,可以通过线程和信号量来控制,默认是线程池隔离实现。下面我们来看下@HystrixCommand注解都有哪些属性,并且如何来设置属性值:
我们先看下他的源码:
/**
* Copyright 2012 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.contrib.javanica.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation used to specify some methods which should be processes as hystrix commands.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
/**
* The command group key is used for grouping together commands such as for reporting,
* alerting, dashboards or team/library ownership.
*
* default => the runtime class name of annotated method
*
* @return group key
*/
String groupKey() default "";
/**
* Hystrix command key.
*
* default => the name of annotated method. for example:
*
* ...
* @HystrixCommand
* public User getUserById(...)
* ...
* the command name will be: 'getUserById'
*
*
* @return command key
*/
String commandKey() default "";
/**
* The thread-pool key is used to represent a
* HystrixThreadPool for monitoring, metrics publishing, caching and other such uses.
*
* @return thread pool key
*/
String threadPoolKey() default "";
/**
* Specifies a method to process fallback logic.
* A fallback method should be defined in the same class where is HystrixCommand.
* Also a fallback method should have same signature to a method which was invoked as hystrix command.
* for example:
*
* @HystrixCommand(fallbackMethod = "getByIdFallback")
* public String getById(String id) {...}
*
* private String getByIdFallback(String id) {...}
*
* Also a fallback method can be annotated with {@link HystrixCommand}
*
* default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()}
*
* @return method name
*/
String fallbackMethod() default "";
/**
* Specifies command properties.
*
* @return command properties
*/
HystrixProperty[] commandProperties() default {};
/**
* Specifies thread pool properties.
*
* @return thread pool properties
*/
HystrixProperty[] threadPoolProperties() default {};
/**
* Defines exceptions which should be ignored.
* Optionally these can be wrapped in HystrixRuntimeException if raiseHystrixExceptions contains RUNTIME_EXCEPTION.
*
* @return exceptions to ignore
*/
Class extends Throwable>[] ignoreExceptions() default {};
/**
* Specifies the mode that should be used to execute hystrix observable command.
* For more information see {@link ObservableExecutionMode}.
*
* @return observable execution mode
*/
ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;
/**
* When includes RUNTIME_EXCEPTION, any exceptions that are not ignored are wrapped in HystrixRuntimeException.
*
* @return exceptions to wrap
*/
HystrixException[] raiseHystrixExceptions() default {};
/**
* Specifies default fallback method for the command. If both {@link #fallbackMethod} and {@link #defaultFallback}
* methods are specified then specific one is used.
* note: default fallback method cannot have parameters, return type should be compatible with command return type.
*
* @return the name of default fallback method
*/
String defaultFallback() default "";
}
源码中对属性的意思已经做了详尽的注释,这里不再累述,下面我们就来做一些我们实际当中大概率需要用到的配置做实验,比如设定使用线程池隔离,设置线程池的参数,以及服务执行超时时间;
先看代码:
@HystrixCommand(commandKey = "hello",fallbackMethod = "fallback",threadPoolProperties = {
@HystrixProperty(name = "coreSize",value = "2"), // 线程核心数目
@HystrixProperty(name = "maxQueueSize",value="3") // 线程池队列大小
},commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"), // 使用线程池隔离
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000" ) // hystrix 超时时间
}
)
public String hello(String name) throws InterruptedException {
Stopwatch stopwatch = Stopwatch.createStarted();
Random random = new Random();
int r = random.nextInt(3000);
Thread.sleep(r);
log.info("服务耗时:{}",stopwatch.stop());
return "hello world!" + name;
}
/**
* 错误回调方法参数签名必须与原方法一致
*
* @param name
* @return
*/
public String fallback(String name, Throwable e) {
log.info("这是造成服务降级的异常信息:{}",name, e);
return "this is fallback message! ";
}
我们设置服务启用线程池进行隔离,并且设置线程池大小为2,线程等待队列为3,以及超时时间1s。我们让服务线程随机睡眠3s内,来测试超时,我们启动服务并发送请求,查看控制台:
我们看到超时,fallback 方法被执行,并返回兜底数据。我们再修改线程睡眠时间来验证线程隔离。
同时我们再设置启动并发线程来调用接口来验证线程池配置的参数:
@RestController
public class Controller {
@Autowired
private HelloServiceWithFallback helloServiceWithFallback;
@GetMapping("test")
public String test(Integer times) throws InterruptedException {
for (int i = 0;i
我们设置请求参数time=4,也就是我们同时启动5个线程去执行服务,理论上可能会出现超时情况:
果然超时了,但是为什么会出现超时呢,因为我们的线程数目设置的是2,超过2个的请求会进入队列等待,所以就会出现超时情况。
下面我们设置times=5 也就是会生成6个线程去请求,我们看下测试结果:
看到后面的请求被拒绝执行了,请求数目大于(coreSize+maxQueueSize)数目就会直接拒绝请求返回fallback方法结果。
我们再来修改配置,测试断路器的属性配置;
@HystrixCommand(commandKey = "hello",fallbackMethod = "fallback",threadPoolProperties = {
@HystrixProperty(name = "coreSize",value = "2"), // 线程核心数目
@HystrixProperty(name = "maxQueueSize",value="3") // 线程池队列大小
},commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"), // 使用线程池隔离
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000" ) ,// hystrix 超时时间
@HystrixProperty(name = "circuitBreaker.enabled",value = "true" ), //开启熔断器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "3" ) // 窗口时间内多少个请求失败了,会打开断路器
}
)
public String hello(String name) throws InterruptedException {
log.info("hello start!");
Stopwatch stopwatch = Stopwatch.createStarted();
// Random random = new Random();
// int r = random.nextInt(5);
Thread.sleep(1200);
log.info("服务耗时:{}",stopwatch.stop());
return "hello world!" + name;
}
/**
* 错误回调方法参数签名必须与原方法一致
*
* @param name
* @return
*/
public String fallback(String name, Throwable e) {
log.info("这是造成服务降级的异常信息:{}",name, e);
return "this is fallback message! ";
}
这次我们修改了线程睡眠时间,让其超时,新增参数:
开启短路器,并且设置窗口时间内5次请求失败则打开断路器,也就是说,只要窗口时间内5次请求都失败了,则后面直接进入fallback方法(该服务被熔断了),设置times=5,再看控制台日志输出:
2019-07-17 17:27:17.333 INFO 3532 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'2019-07-17 17:27:17.333 INFO 3532 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'2019-07-17 17:27:17.339 INFO 3532 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms2019-07-17 17:27:17.586 INFO 3532 --- [ Thread-19] c.e.hystrix.HelloServiceWithFallback : 这是造成服务降级的异常信息:ethan!!java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@cab906f rejected from java.util.concurrent.ThreadPoolExecutor@206f407[Running, pool size = 1, active threads = 1, queued tasks = 3, completed tasks = 0] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047) ~[na:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823) ~[na:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369) ~[na:1.8.0_131] at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112) ~[na:1.8.0_131] at com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler$ThreadPoolWorker.schedule(HystrixContextScheduler.java:172) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler$HystrixContextSchedulerWorker.schedule(HystrixContextScheduler.java:106) ~[hystrix-core-1.5.18.jar:1.5.18] at rx.internal.operators.OperatorSubscribeOn.call(OperatorSubscribeOn.java:50) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OperatorSubscribeOn.call(OperatorSubscribeOn.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8] at rx.Observable.subscribe(Observable.java:10423) [rxjava-1.3.8.jar:1.3.8] at rx.Observable.subscribe(Observable.java:10390) [rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.BlockingOperatorToFuture.toFuture(BlockingOperatorToFuture.java:51) [rxjava-1.3.8.jar:1.3.8] at rx.observables.BlockingObservable.toFuture(BlockingObservable.java:410) [rxjava-1.3.8.jar:1.3.8] at com.netflix.hystrix.HystrixCommand.queue(HystrixCommand.java:378) [hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.HystrixCommand.execute(HystrixCommand.java:344) [hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.contrib.javanica.command.CommandExecutor.execute(CommandExecutor.java:52) [hystrix-javanica-1.5.18.jar:1.5.18] at com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.methodsAnnotatedWithHystrixCommand(HystrixCommandAspect.java:101) [hystrix-javanica-1.5.18.jar:1.5.18] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131] at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) [spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE] at com.example.hystrix.HelloServiceWithFallback$$EnhancerBySpringCGLIB$$680f4c58.hello(
我们看到“hello start!” 输出了三次,后面就没有输出了,而是直接输出了fallback 里的日志。证明实验成果。
本周关于hystrix我们就介绍到这里,如果不对之处欢迎指正交流!