一起学习Hystrix--Hystrix入门


声明:本文转载自https://my.oschina.net/u/2342969/blog/1811436,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

目录

  • 获取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环境,可以通过度娘或者官网进行搭建学习.

  • 在github克隆或者下载hystrix

        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

           

 

本文发表于2018年05月14日 10:04
(c)注:本文转载自https://my.oschina.net/u/2342969/blog/1811436,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1986 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1