Getting Binaries
可以在http://search.maven.org找到Maven,Ivy,Gradle等的二进制文件和依赖信息。Maven的示例:
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>x.y.z</version>
</dependency>
and for Ivy:
<dependency org="com.netflix.hystrix" name="hystrix-core" rev="x.y.z" />
如果您需要下载jar而不是使用构建系统,请使用所需的版本创建这样的Maven pom文件:
<?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及其依赖项下载到./target/dependency/中。您需要Java 6或更高版本。
Hello World!
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-examples模块中找到。
Building
要检查源和构建:
$ git clone git@github.com:Netflix/Hystrix.git
$ cd Hystrix/
$ ./gradlew build
要做一个干净的构建:
$ ./gradlew clean build
构建应如下所示:
$ ./gradlew build
:hystrix-core:compileJava
:hystrix-core:processResources UP-TO-DATE
:hystrix-core:classes
:hystrix-core:jar
:hystrix-core:sourcesJar
:hystrix-core:signArchives SKIPPED
:hystrix-core:assemble
:hystrix-core:licenseMain UP-TO-DATE
:hystrix-core:licenseTest UP-TO-DATE
:hystrix-core:compileTestJava
:hystrix-core:processTestResources UP-TO-DATE
:hystrix-core:testClasses
:hystrix-core:test
:hystrix-core:check
:hystrix-core:build
:hystrix-examples:compileJava
:hystrix-examples:processResources UP-TO-DATE
:hystrix-examples:classes
:hystrix-examples:jar
:hystrix-examples:sourcesJar
:hystrix-examples:signArchives SKIPPED
:hystrix-examples:assemble
:hystrix-examples:licenseMain UP-TO-DATE
:hystrix-examples:licenseTest UP-TO-DATE
:hystrix-examples:compileTestJava
:hystrix-examples:processTestResources UP-TO-DATE
:hystrix-examples:testClasses
:hystrix-examples:test
:hystrix-examples:check
:hystrix-examples:build
BUILD SUCCESSFUL
Total time: 30.758 secs
在干净的构建中,您将看到运行单元测试,看起来像这样:
> Building > :hystrix-core:test > 147 tests completed