在 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 断路器-合并请求的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Hystrix 断路器-缓存

    在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的缓存,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创建和 ...

  2. Spring Cloud(Dalston.SR5)--Hystrix 断路器

    Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...

  4. Spring Cloud入门教程-Hystrix断路器实现容错和降级

    简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...

  5. Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合

    创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...

  6. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  7. Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置

    远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...

  8. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  9. Spring Cloud(Dalston.SR5)--Zuul 网关

    我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...

随机推荐

  1. linux 基本命令大全

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  2. 部署php的正确姿势

    1. 更新源 apt-get update 2.安装apache apt-get install apache2 ubuntu下apache2虚拟主机配置 cd /etc/apache2/sites- ...

  3. 20155219 2016-2017-2 《Java程序设计》第7周学习总结

    20155219 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 认识时间与日期 时间的度量 1.格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为 ...

  4. PSP Daily桌面软件Beta阶段WBS以及PSP【王者荣耀交流协会】

    一.WBS 工具:ProcessOn,请访问网址[https://www.processon.com/]. 分解思路:功能是什么/功能实现步骤?技术原型demo? 二.PSP

  5. HDU 4639 Hehe 2013 Multi-University Training Contest 4

    题意大致如下:屌丝找女神聊天,女神回了一句 hehe ,而我们都知道 Hehe 有两个意思,一个就是 Hehe ,另外一个则是 wqnmlgb (我去年买了个表) ,所以屌丝很纠结,于是开始思考到底女 ...

  6. random module

    import random # 方法返回随机生成的一个实数,它在[0,1)范围内print(random.random())运行结果:0.06435148447021877 # 方法返回随机生成的一个 ...

  7. c++期末考

    1. 谁不及格? Problem:A Time Limit:1000ms Memory Limit:65535K Description 聪聪的班主任王老师最近有点忙,可是他又是一位非常细心的老师,每 ...

  8. Mybatis(七)-- LRU LFU 算法

    这篇博客主要介绍LRU LFU 算法,因为在Mybatis的缓存中会用到,所以放到这个系列中了.此外,这是我翻译的一篇文章,觉得原文已经写的很好了,所以就直接翻译一下,留作知识整理. 英文原文出处如下 ...

  9. Javascript中的 “&” 和 “|” 详解

    转自:https://www.jb51.net/article/104394.htm 一.前言: 在文章开始之前,先出几个题目给大家看看: var num1 = 1 & 0; console. ...

  10. 浅谈malloc()和free()工作原理

    编程之路刚刚开始,错误难免,希望大家能够指出.  malloc()和free()是我经常需要用到的函数,一般情况下,C程序使用malloc()在堆上分配内存,free()释放内存,两者的参数和返回值就 ...