4

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

 1 year ago
source link: https://www.51cto.com/article/745670.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

作者:九天银河聊编程 2023-02-03 15:16:42
Spring Cloud通过Hystrix实现请求合并,减轻高并发时的请求线程消耗、降低请求响应时间的效果。今天就来聊一聊Hystrix请求合并的实现方式。
475298028eb65ba8f4d5556865e786235a7744.png

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

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

启动类上增加注解

@SpringBootApplication
@EnableHystrix
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

实现请求合并,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;
}

​说明:调用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();
}

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

566d95e84838d68cee1364346b62c4bbf54b63.png

a6537bb83763a0596d4432c42a55e48e5f90d8.png
b82c9fd09b712f576197834c233ce03c1034f0.png
  • scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL:将所有线程中多次服务调用进行合并
  • scope = com.netflix.hystrix.HystrixCollapser.Scope.REQUEST:对一次请求的多次服务调用进行合并
责任编辑:姜华 来源: 今日头条

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK