教程:一起学习Hystrix--Hystrix入门

目录

  • 获取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

          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值