Spring Cloud(Dalston.SR5)--Hystrix 断路器-合并请求
在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的合并请求,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创建和销毁 Hystrix 的请求上下文,合并请求的注解需要用到 @HystrixCollapser 和 @HystrixCommand ,示例如下:
- 创建 Filter
和缓存一样,在 Filter 初始化时就创建 HystrixRequestContext,然后在每个请求调用 doFilter 方法时,将初始化的上下文赋值到当前线程存储,这样就能在全局使用 Hystrix 的缓存和合并请求
package org.lixue;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
importj ava.io.IOException;
@WebFilter(urlPatterns="/*",filterName="HystrixRequestContextFilter")
public class HystrixRequestContextFilter implements Filter{
HystrixRequestContext context=null;
@Override
public void init(FilterConfig filterConfig) throws ServletException{
context=HystrixRequestContext.initializeContext();
}
@Override
publicvoid doFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain) throws IOException,ServletException{
HystrixRequestContext.setContextOnCurrentThread(context);
try{
chain.doFilter(request,response);
}catch(Exceptionex){
ex.printStackTrace();
}
}
@Override
publicvoiddestroy(){
if(context!=null){
context.shutdown();
}
}
}
- 创建服务调用
注解 @HystrixCollapser 标注合并请求的方法,需要设置该注解的 batchMethod 属性,该属性设置用于处理批量请求的方法,可以设置 collapserProperties 属性,表示合并请求相关参数配置,这里配置了 timerDelayInMilliseconds 属性,表示合并 5 秒的请求;使用 @HystrixCommand 标注了实际批量请求的方法。
package org.lixue;
import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@Component
public class HelloWorldCollapseClient{
@Autowired
private RestTemplate restTemplate;
@HystrixCollapser(batchMethod="speakCollapse",collapserProperties={
@HystrixProperty(name="timerDelayInMilliseconds",value="5000")
})
public Future<String> speak(String name){
return null;
}
@HystrixCommand
private List<String> speakCollapse(List<String> names){
System.out.println("speakNamesiscall,namesis"+names);
String jsonResponse=
restTemplate.getForObject("http://HELLOWORLD-PROVIDER/speaks?names="+ StringUtils.join(names,',') ,String.class);
Map<String,String> mapResponse=(Map<String,String>)JSON.parse(jsonResponse);
List<String> list=new LinkedList<>();
for(int i=0;i<names.size();i++){
list.add(mapResponse.get(names.get(i)));
}
return list;
}
}
- 测试验证
由于我们使用了 Ribbon 因此首先启动 eureka-server 和 service-provider 服务,然后启动该项目,访问 http://localhost:8077/speak?name=abc 可以看到能正常返回信息,如下:
Hello World abc Port=8080
服务输出日志:
speaks names=abc
使用多个浏览器选项页面,访问 http://localhost:8077/speak?name=abc 多次,可以看到服务都是等待了 5秒后返回,并且都是同时返回,而服务端只被请求了一次。
Spring Cloud(Dalston.SR5)--Hystrix 断路器-合并请求的更多相关文章
- Spring Cloud(Dalston.SR5)--Hystrix 断路器-缓存
在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的缓存,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创建和 ...
- Spring Cloud(Dalston.SR5)--Hystrix 断路器
Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...
- Spring Cloud(Dalston.SR5)--Hystrix 监控
在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...
- Spring Cloud入门教程-Hystrix断路器实现容错和降级
简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...
- Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合
创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置
远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关
我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...
随机推荐
- ubuntu安装nodejs,npm live-server
sudo apt-get install curl 先安装的是curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/in ...
- git 服务器安装流程
参考:https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C% ...
- 【转载】 大龄码农那些事——也谈996.ICU
原文地址: https://www.cnblogs.com/helloyaren/p/10657414.html 请扫码关注!!! 您的关注将是您做的最正确的事情!!! 大龄码农那些事专注分享大龄码农 ...
- PHP安全之Web攻击(转)
一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...
- array的方法 没记住的
reserve() 是倒叙: sort() 拍序,按字符编码排序,可以传一个参数 reduce() 实例:判断一个数组里参数的个数 var arr = ["apple"," ...
- HDU 2048:神、上帝以及老天爷(错排公式,递推)
神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Light OJ 1199:Partitioning Game(SG函数模板)
Alice and Bob are playing a strange game. The rules of the game are: 1. Initially there are n p ...
- linux lamp编译环境安装
apache 安装:http://blog.csdn.net/wplblog/article/details/52172128 编译安装 mysql安装:http://www.centoscn.com ...
- C语言--第六周作业评分和总结(5班)
作业链接:https://edu.cnblogs.com/campus/hljkj/CS2017-5/homework/1250 一.评分要求 要求1 完成PTA第六周所有题,若存在抄袭现象,倒扣此题 ...
- 迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)
本题代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...