第一个Hystrix程序 Hystrix 一
1.导入jar包
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</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.5</version>
</dependency>
</dependencies>
2.编写hystrix测试类
(1)正常情况
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
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; public class HelloCommand extends HystrixCommand<String> { protected HelloCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
} @Override
protected String run() throws Exception {
String url = "http://localhost:8080/test/hello";
HttpGet httpGet = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpGet);
String result = EntityUtils.toString(response.getEntity());
return result;
}
}
(2)异常情况
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
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; public class HelloErrorCommand extends HystrixCommand<String> { protected HelloErrorCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
} @Override
protected String run() throws Exception {
String url = "http://localhost:8080/test/erroHello";
HttpGet httpGet = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpGet);
String result = EntityUtils.toString(response.getEntity());
return result;
} @Override
protected String getFallback() {
System.out.println("这是回退方法");
return "回退方法";
}
}
(3)测试方法
public class HelloMain {
public static void main(String[] args) {
HelloCommand command = new HelloCommand();
String result = command.execute();
System.out.println(result); HelloErrorCommand command1 = new HelloErrorCommand();
String result1 = command1.execute();
System.out.println(result1);
}
}
3.服务接口
@GetMapping(value = "/hello")
public String hello() {
return "hello world";
} @GetMapping(value = "/erroHello")
public String erroHello() throws Exception{
Thread.sleep(10000);
return "hello error";
}
第一个Hystrix程序 Hystrix 一的更多相关文章
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- DirectX游戏编程(一):创建一个Direct3D程序
一.环境 Visual Studio 2012,DirectX SDK (June 2010) 二.准备 1.环境变量(如没有配置请添加) 变量名:DXSDK_DIR 变量值:D:\Software\ ...
- 第一个python程序
一个python程序的两种执行方式: 1.第一种方式是通过python解释器: cmd->python->进入python解释器->编写python代码->回车. 2.第二种方 ...
- 编写第一个MapReduce程序—— 统计气温
摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...
- 1.3 第一个C#程序
几乎没一门编程语言的第一个程序都叫“你好,世界”,所以先在visual studio 中创建一个Helloworld程序. 各部分的详细内容: Main方法是程序运行的起点,最重要的代码就写在Main ...
- 一个.net程序员的安卓之旅-Eclipse设置代码智能提示功能
一个.net程序员的安卓之旅-代码智能提示功能 过完年回来就决心开始学安卓开发,就网上买了个内存条加在笔记本上(因为笔记本原来2G内存太卡了,装了vs2010.SQL Server 2008.orac ...
- MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(02)
1.3.1 应用程序对象 MFC应用程序的核心就是基于CWinApp类的应用程序对象,CWinApp提供了消息循环来检索消息并将消息调度给应用程序的窗口.当包含头文件<afxwin.h>, ...
- Go! new Hello World, 我的第一个Go程序
以下语句摘自百度百科: Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进 ...
- 搭建java开发环境、使用eclipse编写第一个java程序
搭建java开发环境.使用eclipse编写第一个java程序 一.Java 开发环境的搭建 1.首先安装java SDK(简称JDK). 点击可执行文件 jdk-6u24-windows-i586. ...
随机推荐
- sql语句查询去除重复语句的结果集
返回operation表中time列的唯一值 语句1 select time from operation group by time having count(time) > ...
- ABC:Meaningful Mean
题目描述 You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K. a has N(N+1)⁄ ...
- redis的集群:
集群策略:主从复制哨兵集群 参考:https://blog.csdn.net/q649381130/article/details/79931791 集群又分为如下:客户端分片基于代理的分片路由查询参 ...
- derby数据库
derby数据库 https://www.cnblogs.com/zuzZ/p/8107915.html Derby数据库的使用 https://www.cnblogs.com/wkfvawl/p/1 ...
- 牛客-Highway
题目传送门 sol:看了题意显然是最大生成树,但是任意两个点之间都有边,大概有n*n条边.用朴素的最小生成树算法显然不行.联想了一下树的直径还是不会.看了大佬的题解,懂了... 所以还是直接贴大佬博客 ...
- vivado操作基本问题
1.zynq开发板的构造以及推崇的设计理念 设计推崇的理念是设计有知识产权的可重用的IP模块. 2.操作过程中遇到的问题以及解决方法 (1)综合速度慢解决方案: 我们都知道Vivado编译起来相当的慢 ...
- Java IO: PipedOutputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) PipedOutputStream可以往管道里写入读取字节流数据,代码如下: 01 Outp ...
- 吴裕雄--天生自然python学习笔记:Python3 面向对象
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集 ...
- 吴裕雄--天生自然HTML学习笔记:HTML 简介
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Android的HttpClient调用,冲突的解决办法
只适用部分情况 题外话 攻关百度自动发贴作推广失败,但登录已拿下.全扔有点浪费. 在登录的基础上写了个百度的自动签到系统,功能已实现([java,android,nodejs,.net]+nodejs ...