第一个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. ...
随机推荐
- hybrid|Conform the norm of|Mollusk|uncanny|canny|Canvas|documentary
hybrid混合物 Conform the norm of 符合规范 Mollusk贝类 uncanny诡异的 canny精明的 Canvas帆布 documentary纪录片
- python学习笔记(13)常用模块列表总结
os模块: os.remove() 删除文件 os.unlink() 删除文件 os.rename() 重命名文件 os.listdir() 列出指定目录下所有文件 os.chdir() 改变当前工作 ...
- VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】
VSTO中的自定义功能区和自定义任务窗格需要用到各种命名空间.添加所需文件,才能实现.后来我发现可以把所有代码都写在ThisAddin.vb这个默认文件中. 大家可以在Visual Studio中创建 ...
- 关于用struts2框架中iframe对应的jsp页面的不到action的值的问题
我们做web项目经常会用到frameset.frame以及iframe,这大大方便了我们页面的构建以及模块功能的划分.但是,再使用这些技术的同时也会遇到各种各样的问题,其中一个就是子页面中得不到str ...
- iOS 中UIWebView的cookie
有关cookie是什么,大家可以自行百度,本文我获得cookie的目的是得到一个userID. 下面的是代码. - (void)getUserIDFromCookie { NSHTTPCookieSt ...
- [LC] 139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 吴裕雄--天生自然python学习笔记:python爬虫PM2.5 实时监测显示器
PM2.5 对人体的健康影响很大,所以空气中的 PM2.5 实时信息受到越来越多的关注. Python 的 Pandas 套件不但可以自动读取网页中的表格 数据 , 还可对数据进行修改.排序等处理,也 ...
- 前端-js-长期维护
############### JS简介和JS引入 ################ <!DOCTYPE html> <html lang="en" ...
- Java IO: 字符流的Buffered和Filter
作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍缓冲与过滤相关的reader和writer,主要涉及BufferedReader.B ...
- OpenCV 读取、修改、保存图像
代码如下: #include <cv.h> #include <highgui.h> using namespace cv; int main( int argc, char* ...