Hystrix-快速开始
- 接口提供者编写
- Pom文件
<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>org.crazyit.cloud</groupId> <artifactId>first-hy-server</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.4.RELEASE</version> </dependency> </dependencies> </project>
- 项目启动类
package org.crazyit.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
- 被调用的接口代码
package org.crazyit.cloud; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(value = "/normalHello", method = RequestMethod.GET) public String normalHello() { return "Hello World"; } @RequestMapping(value = "/errorHello", method = RequestMethod.GET) public String errorHello() throws Exception { Thread.sleep(10000); return "Error Hello World"; } }
/errorHello,异常情况,此请求需要10秒才能处理完毕。
/normalHello,正常情况,处理快速。
运行MyApplication的main方法,使接口可以被调用。
- 接口调用者编写
Hystrix是在接口调用方的技术,故hystrix的相关代码如下
- Pom文件-引入相关依赖
引入com.netflix.hystrix的core包<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>org.crazyit.cloud</groupId> <artifactId>first-hy-client</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>1.5.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies> </project>
- 调用正常接口代码
- 编写命令类
package org.crazyit.cloud; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; public class HelloCommand extends HystrixCommand<String> { public HelloCommand() { super(HystrixCommandGroupKey.Factory.asKey("TestGroup")); } @Override protected String run() throws Exception { String url = "http://localhost:8080/normalHello"; HttpGet httpget = new HttpGet(url); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpResponse response = httpclient.execute(httpget); return EntityUtils.toString(response.getEntity()); } }
- 编写main方法执行命令
package org.crazyit.cloud; public class NormalMain { public static void main(String[] args) { HelloCommand command = new HelloCommand(); String result = command.execute(); System.out.println(result); } }
- 编写命令类:执行错误命令的命令类
package org.crazyit.cloud.error; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; public class ErrorCommand extends HystrixCommand<String> { public ErrorCommand() { super(HystrixCommandGroupKey.Factory.asKey("TestGroup")); } @Override protected String run() throws Exception { String url = "http://localhost:8080/errorHello"; HttpGet httpget = new HttpGet(url); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpResponse response = httpclient.execute(httpget); return EntityUtils.toString(response.getEntity()); } @Override protected String getFallback() { System.out.println("fall back method"); return "fall back hello"; } }
- 编写main方法执行命令
package org.crazyit.cloud.error; public class ErrorMain { public static void main(String[] args) { ErrorCommand command = new ErrorCommand(); String result = command.execute(); System.out.println(result); } }
- 总结
- 命令类需要继承HystrixCommand<T>
- 命令类的构造方法里设置命令所属的组:HystrixCommandGroupKey.Factory.asKey("TestGroup")
- 命令类重写run方法:真正用于执行的命令内容
- 命令类重写getFallback方法:当run方法内报错或者执行HTTP请求超时时,执行此方法