hystrix文档翻译之开始使用
获取包
使用maven获取包。
<dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>x.y.z</version> </dependency>
使用lvy获取包
<dependency org="com.netflix.hystrix" name="hystrix-core" rev="x.y.z" />
如果希望使用maven下载包
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.netflix.hystrix.download</groupId> <artifactId>hystrix-download</artifactId> <version>1.0-SNAPSHOT</version> <name>Simple POM to download hystrix-core and dependencies</name> <url>http://github.com/Netflix/Hystrix</url> <dependencies> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>x.y.z</version> <scope/> </dependency> </dependencies> </project>
然后执行
mvn -f download-hystrix-pom.xml dependency:copy-dependencies
它将会下载hystrix-core-*.jar和他的依赖包。
使用hystrix的一个简单例子:
public class CommandHelloWorld extends HystrixCommand<String> { private final String name; public CommandHelloWorld(String name) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.name = name; } @Override protected String run() { // a real example would do work like a network call here return "Hello " + name + "!"; } }
有一下三种方式调用命令
String s = new CommandHelloWorld("Bob").execute(); Future<String> s = new CommandHelloWorld("Bob").queue(); Observable<String> s = new CommandHelloWorld("Bob").observe();
编译hystrix
checkout源码并编译
$ git clone git@github.com:Netflix/Hystrix.git $ cd Hystrix/ $ ./gradlew build
执行 clean build
$ ./gradlew clean build