Hystrix-快速开始

Hystrix-快速开始

  1. 接口提供者编写
  1. 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>
  2. 项目启动类
    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);
    	}
    
    }
  3. 被调用的接口代码
    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方法,使接口可以被调用。

  1. 接口调用者编写

Hystrix是在接口调用方的技术,故hystrix的相关代码如下

  1. 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-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>
    引入com.netflix.hystrix的core包
  2. 调用正常接口代码
  • 编写命令类
    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);
    	}
    
    }
    

     

  1. 总结
  1. 命令类需要继承HystrixCommand<T>
  2. 命令类的构造方法里设置命令所属的组:HystrixCommandGroupKey.Factory.asKey("TestGroup")
  3. 命令类重写run方法:真正用于执行的命令内容
  4. 命令类重写getFallback方法:当run方法内报错或者执行HTTP请求超时时,执行此方法

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值