第一个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. ...
随机推荐
- 浅谈Java中的泛型
泛型是Java自JDK5开始支持的新特性,主要用来保证类型安全.另外泛型也让代码含义更加明确清晰,增加了代码的可读性. 泛型的声明和使用 在类声明时在类名后面声明泛型,比如MyList<T> ...
- C段和旁注
踩点常用手段有哪些 1.旁站注入:利用同一 主机上面不同网站的漏洞得到 webshell,从而利用主机上的程序或者是服务所暴露的 用户所在的物理路径进行入侵.网站共享主机漏洞是更为严谨的学术叫法 2. ...
- Derby 命令
SHOW [ TABLES | VIEWS | PROCEDURES | FUNCTIONS | SYNONYMS ] { IN sche -- 列出表.视图.过程.函数或同义词 SHOW INDEX ...
- UAC table
CREATE TABLE `sys_dept` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(50) D ...
- HDU1233 还是畅通工程
题目: Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连 ...
- montagy
因为只是想分享ghcjs和webgl的使用经验,所以很多地方说的很粗,因为涉及的知识确实很多, 推荐两本书,一本haskell基础的 learn you a haskell for great goo ...
- Java集合 - 明的博客
"In this world there are only two tragedies. One is not getting what one wants, and the other i ...
- 分布式文件系统与HDFS
HDFS,它是一个虚拟文件系统,用于存储文件,通过目录树来定位文件:其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色. HDFS 的设计适合一次写入,多次读出的场景,且不 ...
- win7 任务栏 无法固定文件夹和文件 解决方法
2010年开始使用win7,一直都在用一个功能,把常用的文件夹和文件都直接锁定到任务栏,方便使用. 最近这个功能一直有问题,开始只是重启以后,以前固定的文件没有了,这个也懒的去弄它,没了在添加一次. ...
- SpringMVC之请求响应(上)
1.OutPutController package com.tz.controller; import java.util.Map; import org.springframework.stere ...