1. 依赖引入
pom.xml
<properties>
<hystrix-version> 1.4 . 22 </hystrix-version>
</properties>
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>${hystrix-version}</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-servo-metrics-publisher</artifactId>
<version>${hystrix-version}</version>
</dependency>
</dependencies>
|
applicationContext.xml:
<aop:aspectj-autoproxy/>
<bean
id= "hystrixAspect" class = "com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
|
web.xml:
<servlet>
<display-name>HystrixMetricsStreamServlet</display-name>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<servlet- class >com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet- class >
</servlet>
<servlet-mapping>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping>
|
jmonitor:
collector=jvm,appdata,http
|
2. 使用
这里只讲注解的使用方式以及比较重要的部分,如果需要了解全部查看:https://github.com/Netflix/Hystrix/wiki/How-To-Use
2.1 Hystrix command
2.1.1 同步执行
public class UserService
{
...
@HystrixCommand
public User
getUserById(String id) {
return userResource.getUserById(id);
}
}
|
2.1.2 异步执行
public class UserService
{
...
@HystrixCommand
public Future<User>
getUserByIdAsync( final String
id) {
return new AsyncResult<User>()
{
@Override
public User
invoke() {
return userResource.getUserById(id);
}
};
}
}
|
2.1.3 反应执行()
public class UserService
{
...
@HystrixCommand
public Observable<User>
getUserById( final String
id) {
return Observable.create( new Observable.OnSubscribe<User>()
{
@Override
public void call(Subscriber<? super User>
observer) {
try {
if (!observer.isUnsubscribed())
{
observer.onNext( userResource.getUserById(id));
observer.onCompleted();
}
} catch (Exception
e) {
observer.onError(e);
}
}
});
}
}
|
2.1.4 三种模式使用区别
- 同步执行:当执行到注解方法时,程序会顺序执行。
- 异步执行:当执行到注解方法时,会并发异步执行,返回一个Future对象,后面使用.get()方法来阻塞拿到结果。如果有多个方法时,执行时间就是其中最长的一个服务的执行时间。
- 反应执行:当执行到注解方法时,返回一个观察者。支持EAGER和LAZY模式。和同步异步执行的区别是,当对多个方法之间的返回结果不需要做合并而是希望当多个方法返回时触发一些事件时比较适合使用该模式。
反应执行没太明白,如果需要了解可以先参考下这个https://mcxiaoke.gitbooks.io/rxdocs/content/Intro.html
2.2 Fallback
@HystrixCommand (fallbackMethod
= "fallback1" )
User
getUserById(String id) {
throw new RuntimeException( "getUserById
command failed" );
}
@HystrixCommand (fallbackMethod
= "fallback2" )
User
fallback1(String id, Throwable e) {
assert "getUserById
command failed" .equals(e.getMessage());
throw new RuntimeException( "fallback1
failed" );
}
@HystrixCommand (fallbackMethod
= "fallback3" )
User
fallback2(String id) {
throw new RuntimeException( "fallback2
failed" );
}
|
注意点:
- fallback应该和注解方法在同一类下
- fallback的返回值和参数列表应该和注解方法一致,如果需要异常,则在末尾添加Throwable参数,对访问修饰符无要求
- fallback方法上可以继续添加fallback
command和fallback只支持以下几种组合:
- sync command, sync fallback
- async command, sync fallback
- async command, async fallback
2.3 Error Propagation
@HystrixCommand (ignoreExceptions
= {BadRequestException. class })
public User
getUserById(String id) {
return userResource.getUserById(id);
}
|
当遇到BadRequestException时不会进入fallback,而是直接抛出异常
2.4 Configuration
@HystrixCommand (groupKey= "UserGroup" ,
commandKey = "GetUserByIdCommand" ,
commandProperties
= {
@HystrixProperty (name
= "execution.isolation.thread.timeoutInMilliseconds" ,
value = "500" )
},
threadPoolProperties
= {
@HystrixProperty (name
= "coreSize" ,
value = "30" ),
@HystrixProperty (name
= "maxQueueSize" ,
value = "101" ),
@HystrixProperty (name
= "keepAliveTimeMinutes" ,
value = "2" ),
@HystrixProperty (name
= "queueSizeRejectionThreshold" ,
value = "15" ),
@HystrixProperty (name
= "metrics.rollingStats.numBuckets" ,
value = "12" ),
@HystrixProperty (name
= "metrics.rollingStats.timeInMilliseconds" ,
value = "1440" )
})
|
转自:http://blog.csdn.net/zheng0518/article/details/51713900