使用HystrixCommand封装http请求
1.引入依赖
要排除hystrix-core里的archaius-core,否则报错
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.</version>
<exclusions>
<exclusion>
<artifactId>archaius-core</artifactId>
<groupId>com.netflix.archaius</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.</version>
</dependency> <dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
2.定义HttpHystrixCommand类
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import lombok.extern.slf4j.Slf4j; @Slf4j
public class HttpHystrixCommand extends HystrixCommand<ResponseEntity<String>> { private org.springframework.http.HttpMethod httpMethod;
private HttpEntity httpEntity;
private String url;
private RestTemplate restTemplate = new RestTemplate(); public HttpHystrixCommand(String url, HttpMethod httpMethod, HttpEntity<?> httpEntity) {
// 设置3秒超时
super(HystrixCommandGroupKey.Factory.asKey("http_hystrix_command_group_key"), );
this.httpMethod = httpMethod;
this.httpEntity = httpEntity;
this.url = url;
} public HttpHystrixCommand(String url, HttpEntity<?> httpEntity) {
this(url, HttpMethod.POST, httpEntity);
} public HttpHystrixCommand(String url) {
this(url, HttpMethod.GET, null);
} public ResponseEntity<String> post() {
this.httpMethod = HttpMethod.POST;
return execute();
} public ResponseEntity<String> get() {
this.httpMethod = HttpMethod.GET;
this.httpEntity = null;
return execute();
} @Override
protected ResponseEntity<String> run() throws Exception {
// Thread.sleep(3000);
return restTemplate.exchange(url, httpMethod, (HttpEntity) httpEntity, String.class);
} @Override
protected ResponseEntity<String> getFallback() {
Throwable executionException = getExecutionException();
if (executionException instanceof HystrixTimeoutException) {
log.error("请求因超时融断!url:{} param:{}", this.url, this.httpEntity, executionException);
} else if (executionException instanceof HystrixRuntimeException) {
log.error("请求直接融断!url:{} param:{}", this.url, this.httpEntity, executionException);
} else {
log.error("请求异常!url:{} param:{}", this.url, this.httpEntity, executionException);
} return new ResponseEntity<String>(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
3.测试
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springside.modules.utils.mapper.JsonMapper;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j; /**
*
*/ @Slf4j
public class HttpHystrixCommandTest {
public static void main(String[] args) {
ResponseEntity<String> response = new HttpHystrixCommand(
"https://aaa.xxx.com/setting/get?key=key1&source=h5")
.get();
if (response.getStatusCode().value() == org.springframework.http.HttpStatus.OK.value()) {
log.info("请求成功");
}else{
log.info("请求失败");
}
}
private static HttpHeaders defaultRequestHeaders = new HttpHeaders(); static {
defaultRequestHeaders.setContentType(MediaType.APPLICATION_JSON);
defaultRequestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
} public static HttpEntity<String> getHttpEntity(Object postData) {
return new HttpEntity<>(JsonMapper.INSTANCE.toJson(postData), defaultRequestHeaders);
} public static HttpEntity<String> getHttpEntity(String postData) {
return new HttpEntity<>(postData, defaultRequestHeaders);
}
}
4.正常的返回如下
INFO HttpHystrixCommandTest - 请求成功
5.异常(增加代码Thread.sleep(3000);)的返回如下:
ERROR HttpHystrixCommand - 请求因超时融断!url:https://aaa.xxx.com/setting/get?key=depository&source=h5 param:null
com.netflix.hystrix.exception.HystrixTimeoutException: null
使用HystrixCommand封装http请求的更多相关文章
- WebApi系列~基于单请求封装多请求的设计
回到目录 怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能 ...
- [iOS微博项目 - 3.3] - 封装网络请求
github: https://github.com/hellovoidworld/HVWWeibo A.封装网络请求 1.需求 为了避免代码冗余和对于AFN框架的多处使用导致耦合性太强,所以把网 ...
- 微信小程序初体验--封装http请求
最近看了一下微信小程序,大致翻了一下,发现跟angular很相似的,但是比angular简单的很多具体可参考官方文档 https://mp.weixin.qq.com/debug/wxadoc/dev ...
- 结合prototype和xmlhttprequest封装ajax请求
由于拖延症的严重以及年前准备年会(借口*^__^*) 导致这个小的的思考 现在才算完成 再怎么说也算是上班以来带我的前辈第一次这么正式的给我出题 不管是出于尊重还是自我要求我都决定把它简要的记下来 ...
- App 组件化/模块化之路——如何封装网络请求框架
App 组件化/模块化之路——如何封装网络请求框架 在 App 开发中网络请求是每个开发者必备的开发库,也出现了许多优秀开源的网络请求库.例如 okhttp retrofit android-asyn ...
- 微信小程序教学第二章(含视频):小程序中级实战教程之预备篇 - 封装网络请求及 mock 数据
§ 封装网络请求及 mock 数据 本文配套视频地址: https://v.qq.com/x/page/i05544fogcm.html 开始前请把 ch2-3 分支中的 code/ 目录导入微信开发 ...
- 微信小程序之封装http请求
下面将封装http请求服务部分的服务以及引用部分 // 本服务用于封装请求 // 返回的是一个promisepromise var sendRrquest = function (url, metho ...
- 微信小程序开发——使用promise封装异步请求
前言: 有在学vue的网友问如何封装网络请求,这里以正在写的小程序为例,做一个小程序的请求封装. 关于小程序发起 HTTPS 网络请求的Api,详情可以参考官方文档:wx.request(Object ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
随机推荐
- ORM多表操作上
一.创建模型 例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一(on ...
- python学习笔记(十四)加密模块
import hashlib ybq_pwd='bugaosuni' m=hashlib.md5() bytes_ybq=ybq_pwd.encode()#把字符串转成bytes类型,中文字符在Pyt ...
- 04 全局配置,java 编意默认版本,1.6.修改配置
https://www.cnblogs.com/liu-s/p/5371289.html <!-- 修改Intellij Idea 创建maven项目默认Java编译版本 --> < ...
- springboot整合 thymeleaf 案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- php current()函数 语法
php current()函数 语法 作用:返回数组中的当前元素的值.直线电机工作原理 语法:current(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:返回数组中的 ...
- ResultSet用法集锦 (转)
转:http://soft-development.iteye.com/blog/1420323 结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是 ...
- vue获取v-on绑定事件的触发对象
<span @click="fn" id="foo">xxx</span> fn(e){ console.log(e);//展开查看e. ...
- cmath模块——复数域数学函数模块
cmath——复数域数学函数模块 转自:https://blog.csdn.net/zhtysw/article/category/7511293 该模块属于内置模块,随时可以调用.它提供了数学函数在 ...
- Visual Studio Code - 快捷键
默认快捷键 Visual Studio Code 默认快捷键 代码提示(自动补全,自动完成) 默认是快捷键是Ctrl+Space,与搜狗输入法切换中英文的快捷键冲突了..可以改搜狗输入法的快捷键(Sh ...
- Python专题三字符串的基础知识
Python专题三字符串的基础知识 在Python中最重要的数据类型包括字符串.列表.元组和字典等.该篇主要讲述Python的字符串基础知识. 一.字符串基础 字符串指一有序的字符序列集合,用单引号. ...