在 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. jar安装

    安装sdk jar 安装到本地 mvn install:install-file -Dfile=F:\workspace\api-cookbook\java\src\main\lib\sdk-1.10 ...

  2. 你知道怎么用Idea抽取方法、创建class吗?

    liJ IDEA的快捷键是进行重构的利器,坊间盛传,完全使用IDEA快捷键重构的代码,是不需要写测试用例保护的 本文就分享一个使用IDEA抽取方法及创建新的class的方法 工具/原料   Intel ...

  3. 【Python】进程3

    #练习: import time from multiprocessing import Pool def run(fn): #fn: 函数参数是数据列表的一个元素 time.sleep(1) ret ...

  4. 新建oracle数据库表空间及删除表空间和用户

    进入oracle的命令控制台,按具体情况执行以下命令: sqlplus 默认数据库普通用户登录sqlplus / as sysdba 默认数据库管理员登录sqlplus username/passwo ...

  5. 怎样用CMD命令强行删除文件?

    如果你要删除的整个文件夹以及文件夹里面的所有内容的话rd/s/q 盘符:\某个文件夹   (这样整个文件夹所有的文件和文件夹都删除了)比如我想删除D盘的123文件夹以及123文件夹里面所有的内容rd/ ...

  6. 将js和css文件装入localStorage加速程序执行

    原理如下: 一次批量加要加载的文件存入数组,采用Ajax方式异步载入各个文件,然后采用循环方式逐个执行下载下来的Js或者Css文件,如果已经被缓存(localStorage)的则省略下载过程. 由于J ...

  7. [LeetCode&Python] Problem 896. Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  8. music cube

    music cubehttps://www.youtube.com/watch?v=HBCdC7r7Mp4Blender Tutorial: Make anything react with musi ...

  9. [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]

    http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...

  10. 浮动IP(FLOAT IP)

    主要谈一谈关于浮动IP的东西,介绍下浮动IP是什么 1.为什么要有浮动IP这个东西       现在有一个场景,在一台Linux上部署一个web应用,应用跑在tomcat里面,linux网卡上的ip是 ...