`

Hystrix文档-开始使用(转)

 
阅读更多

 

转自:http://youdang.github.io/2016/02/01/translate-hystrix-wiki-getting-started/

如何获取

二进制包和使用 Maven, Ivy, Gradle 或其他项目管理工具所需的依赖配置信息放置在 http://search.maven.org

Maven 配置的例子:

1
2
3
4
5
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>x.y.z</version>
</dependency>

Ivy 配置的例子:

1
<dependency org="com.netflix.hystrix" name="hystrix-core" rev="x.y.z" />

若你仅仅需要下载 jar 包,而不需要使用构建工具,那么你需要创建如下所示的一个 Maven pom 文件,并指定需要下载的版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>

然后执行如下命令:

1
mvn -f download-hystrix-pom.xml dependency:copy-dependencies

hystrix-core-*.jar 及其依赖的包会被下载到 ./target/dependency/ 目录下。

Hystrix 依赖 Java 6 或更高版本。

Hello World!

下面是使用 Hystrix 最简单的一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 + "!";
}
}

上述代码定义的『命令』的使用方式如下:

1
2
3
String s = new CommandHelloWorld("Bob").execute();
Future<String> s = new CommandHelloWorld("Bob").queue();
Observable<String> s = new CommandHelloWorld("Bob").observe();

更多的例子和说明请移步 如何使用 一章。

从源码构建

从 GitHub 上获得源码,然后执行如下命令进行构建:

1
2
3
$ git clone git@github.com:Netflix/Hystrix.git
$ cd Hystrix/
$ ./gradlew build

若想重新开始构建:

1
$ ./gradlew clean build

构建可能的输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ ./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

重新构建的话,所有单元测试都会被执行,输出如下:

 

1
> Building > :hystrix-core:test > 147 tests completed
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics