深圳幻海软件技术有限公司 欢迎您!

SpringCloud Hystrix高并发下实现请求合并

2023-02-28

前言在高并发的场景下,前端会有大量的访问请求。如果一个请求就需要打开一个数据库连接,操作完数据库后再进行关闭,无形中对数据造成很大的开销。请求合并是将多个单个请求合并成一个请求,去调用服务提供者提供的服务接口,再遍历合并的结果为每个合并前的单个请求设置返回结果。SpringCloud通过Hystri

前言

在高并发的场景下,前端会有大量的访问请求。如果一个请求就需要打开一个数据库连接,操作完数据库后再进行关闭,无形中对数据造成很大的开销。请求合并是将多个单个请求合并成一个请求,去调用服务提供者提供的服务接口,再遍历合并的结果为每个合并前的单个请求设置返回结果。Spring Cloud通过Hystrix实现请求合并,减轻高并发时的请求线程消耗、降低请求响应时间的效果。今天就来聊一聊Hystrix请求合并的实现方式。

实现方式

由于是高并发场景,因此准备了SpringCloud微服务框架。准备了注册中心、网关、服务提供者、服务消费者等组件。

导入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.

启动类上增加注解

@SpringBootApplication
@EnableHystrix
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

实现请求合并,Service中代码如下:

//请求合并的方法  合并5s内的请求
@HystrixCollapser(batchMethod = "mergeGet", scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL, collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds", value = "5000")})
public Future<Item> get(String id) {
    log.info("======执行了get方法========" + id);
    return null;
}

//合并请求之后调用的方法
@HystrixCommand
public List<Item> mergeGet(List<String> ids) {
    log.info("===合并开始===");
    List<Item> items = ids.stream().map(
            x -> {
                Item item = new Item();
                item.setId(Integer.valueOf(x));
                item.setName("商品 :" + x);
                return item;
            }

    ).collect(Collectors.toList());
    log.info("===合并结束,合并 {} 条 请求====", ids.size());
    return items;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

​说明:调用get方法,如果5s内get有多次调用,则合并后mergeGet方法。

controller调用代码如下:

@RequestMapping(value = "/find/{id}")
public Item find(@PathVariable String id) throws InterruptedException, ExecutionException {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    Future<Item> items = itemService.get(id);
    System.out.println(items.get());
    context.close();
    return items.get();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

执行

执行127.0.0.1:8080/find/11,同时执行127.0.0.1:8080/find/22,保证两个请求在5s内发出。

返回结果

说明:

  • scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL:将所有线程中多次服务调用进行合并
  • scope = com.netflix.hystrix.HystrixCollapser.Scope.REQUEST:对一次请求的多次服务调用进行合并