目录
- 获取hystrix库
- "hello world"
- 构建hystrix
获取hystrix库
在 http://search.maven.org (国内访问较慢)找到hystrix关于 Maven, Ivy, Gradle等的二进制文件以及依赖信息。还可以通过 http://mvnrepository.com(推荐)获取。
maven的示例:
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>x.x.x</version> </dependency>
lvy如下:
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core --> <dependency org="com.netflix.hystrix" name="hystrix-core" rev="x.x.x"/>
Gradle如下:
// https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core compile group: 'com.netflix.hystrix', name: 'hystrix-core', version: 'x.x.x'
如果是需要下载jars而不使用构建系统,创建一个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>
在pom文件所在目录下,执行maven命令:
Note:执行maven命令需要有maven环境,可以通过度娘或者maven官网学习
mvn -f download-hystrix-pom.xml dependency:copy-dependencies
它就会下载 hystrix-core-*.jar 和它的依赖,他的依赖在 ./target/dependency/ 目录下。
java版本需要在java6(含)以上
Hello World!
简单使用hrstrix示例如下:
Note:本示例基于version 1.5.12
public class HystrixHelloWorld extends HystrixCommand<String> { private final String name; public HystrixHelloWorld(String name) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.name = name; } @Override protected String run() { return "Hello " + name + "!"; } }
查看源码以及单元测试
以上代码使用方式:
String s = new CommandHelloWorld("Bob").execute(); Future<String> s = new CommandHelloWorld("Bob").queue(); Observable<String> s = new CommandHelloWorld("Bob").observe();
更多的hystrix官方例子
构建Hystrix
hystrix基于Gradle构建,需要Gradle环境,可以通过度娘或者官网进行搭建学习.
git命令克隆:
git clone git@github.com:Netflix/Hystrix.git
或者点击链接直接下载
- 进入hystrix目录
- 执行gradle命令(没有vpn的话会很慢慢)
./gradlew 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