当前位置: 首页 > article >正文

使用Hystrix实现请求合并,降低服务器并发压力

1.引入Hystrix 

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

2.在启动类上开启Hystrix功能

@EnableHystrix

3.请求合并实现代码

import com.bzcst.bop.oms.orm.model.po.Product;
import com.bzcst.bop.oms.orm.service.ProductService;
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 com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author 向振华
 * @date 2023/10/26 14:37
 */
@Slf4j
@Service
public class BatchService {

    @Resource
    private ProductService productService;

    @HystrixCollapser(
            batchMethod = "getByIds",
            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
            collapserProperties = {
                    @HystrixProperty(name = HystrixPropertiesManager.MAX_REQUESTS_IN_BATCH, value = "10"),
                    @HystrixProperty(name = HystrixPropertiesManager.TIMER_DELAY_IN_MILLISECONDS, value = "20")
            }
    )
    public Future<Product> getById(Long id) {
        return null;
    }

    @HystrixCommand
    public List<Product> getByIds(List<Long> ids) {
        log.info("批查询 ---> " + ids);
        return productService.listByIds(ids);
    }
}

其中getById(Long id)方法并不会进入。 

其中ProductService 是基于mybatisplus的查询接口。

import com.bzcst.bop.oms.orm.model.po.Product;
import com.baomidou.mybatisplus.extension.service.IService;

public interface ProductService extends IService<Product> {

}

 4.调用

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.bzcst.bop.oms.orm.model.po.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author 向振华
 * @date 2023/10/26 14:32
 */
@RestController
public class TestController {

    @Resource
    private BatchService batchService;

    @GetMapping("/product/{id}")
    public Object product(@PathVariable Long id) throws Exception {
        Future<Product> productFuture = batchService.getById(id);
        return productFuture.get();
    }

    public static void main(String[] args) {
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        // 模拟20个并发请求
        CountDownLatch countDownLatch = new CountDownLatch(20);
        for (int i = 1; i <= 20; i++) {
            int fi = i;
            executorService.execute(() -> {
                try {
                    countDownLatch.await();
                    HttpRequest httpRequest = HttpRequest.get("http://localhost:8080/bop-oms/product/" + fi);
                    String body = httpRequest.execute().body();
                    System.out.println(fi + " ---> " + body);
                } catch (Exception e) {
                }
            });
            countDownLatch.countDown();
        }
    }
}

日志记录可以看出是批量处理

2023-10-26 17:54:31.708  INFO 10524 --- [-BatchService-9] c.bzcst.bop.oms.controller.BatchService  : 批查询 ---> [10, 5, 9, 15, 4, 12, 11, 16, 8, 14]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58dfec5b] was not registered for synchronization because synchronization is not active
parser sql: SELECT id, xxx FROM oms_product WHERE id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2023-10-26 17:54:31.712  INFO 10524 --- [-BatchService-8] c.bzcst.bop.oms.controller.BatchService  : 批查询 ---> [3, 18, 1, 13, 7, 19, 20, 2, 6, 17]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4235ea19] was not registered for synchronization because synchronization is not active
parser sql: SELECT id, xxx FROM oms_product WHERE id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
JDBC Connection [HikariProxyConnection@935862209 wrapping com.mysql.cj.jdbc.ConnectionImpl@130542a5] will not be managed by Spring
==>  Preparing: SELECT id, xxx FROM oms_product WHERE id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
==> Parameters: 10(Long), 5(Long), 9(Long), 15(Long), 4(Long), 12(Long), 11(Long), 16(Long), 8(Long), 14(Long)
JDBC Connection [HikariProxyConnection@205926351 wrapping com.mysql.cj.jdbc.ConnectionImpl@67b600cc] will not be managed by Spring
==>  Preparing: SELECT id, xxx FROM oms_product WHERE id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
==> Parameters: 3(Long), 18(Long), 1(Long), 13(Long), 7(Long), 19(Long), 20(Long), 2(Long), 6(Long), 17(Long)
<==    Columns: id, tenant_id, category_id, name, code, price, ladder_price, is_ladder_price, unit, is_enabled, description, version, create_by, create_time, update_by, update_time, delete_flag
<==        Row: 4, 1, 0, 转供电, 4, 4.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 5, 1, 0, 通下水道, XSD, 5.00, , 0, , 1, , 1,  , 2023-05-23 18:24:30,  , 2023-05-23 18:24:30, 0
<==        Row: 8, 1, 0, 门卡押金, 0302, 8.00, , 0, , 1, , 1,  , 2023-03-04 10:57:50,  , 2023-04-14 09:53:17, 0
<==        Row: 9, 1, 0, 阶梯单价, 12321, 9.00, , 1, , 1, , 1,  , 2023-10-13 16:39:09,  , 2023-10-13 16:39:09, 0
<==        Row: 10, 1, 0, 阿萨德, 123333, 10.00, , 0, , 1, , 1,  , 2023-09-14 10:43:01,  , 2023-09-14 10:43:01, 0
<==        Row: 11, 1, 0, 补贴空置费, 444, 11.00, , 0, , 1, , 1,  , 2023-03-02 17:05:10,  , 2023-03-02 17:05:10, 0
<==        Row: 12, 1, 0, 路灯费, B<N, 12.00, , 0, , 1, , 1,  , 2023-03-03 10:55:23,  , 2023-03-03 10:55:23, 0
<==        Row: 14, 1, 0, 零星物业, 3, 14.00, , 0, , 1, , 1,  , 2023-03-02 16:50:05,  , 2023-03-02 16:50:05, 0
<==        Row: 15, 1, 0, 零星电费, 2, 15.00, , 0, , 1, , 1,  , 2023-03-02 16:49:46,  , 2023-03-02 16:49:46, 0
<==        Row: 16, 1, 0, 非生活垃圾清运费, 16, 16.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==      Total: 10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58dfec5b]
<==    Columns: id, tenant_id, category_id, name, code, price, ladder_price, is_ladder_price, unit, is_enabled, description, version, create_by, create_time, update_by, update_time, delete_flag
<==        Row: 1, 1, 0, 专项维修金, 1, 1.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 2, 1, 0, 车位空置费, 2, 2.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 3, 1, 0, 车位费, CWF01, 3.00, , 0, , 1, , 1,  , 2023-10-10 09:57:07,  , 2023-10-10 09:57:07, 0
<==        Row: 6, 1, 0, 服务费, 6, 6.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 7, 1, 0, 销售配合费, s, 7.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 13, 1, 0, 零星水费, 1, 13.00, , 0, , 1, , 1,  , 2023-03-02 16:49:18,  , 2023-03-02 16:49:18, 0
<==        Row: 17, 1, 0, 顾问费, 17, 17.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 18, 1, 0, 餐饮垃圾费, 18, 18.00, , 0, , 1, , 1,  , 2022-12-29 17:01:09,  , 2022-12-29 17:01:09, 0
<==        Row: 19, 1, 0, 租赁广告费用, 1000123, 19.00, [], 0, , 1, , 1,  , 2023-06-08 10:51:16,  , 2023-06-08 10:51:16, 0
<==        Row: 20, 1, 0, 租金, ZJ002, 20.00, [], 0, , 1, , 1,  , 2023-03-15 17:40:12,  , 2023-03-15 17:40:12, 0
<==      Total: 10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4235ea19]

或者这样请求也可以

    @GetMapping("/product")
    public void product() throws Exception {
        List<Future> list = new ArrayList<>();
        for (long i = 1; i <= 20; i++) {
            Future<Product> productFuture = batchService.getById(i);
            list.add(productFuture);
        }

        for (Future future : list) {
            Object o = future.get();
            System.out.println(o);
        }
    }

如果scope是REQUEST方式,则需要使用HystrixRequestContext

    @GetMapping("/product")
    public void product() throws Exception {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            List<Future> list = new ArrayList<>();
            for (long i = 1; i <= 20; i++) {
                Future<Product> productFuture = batchService.getById(i);
                list.add(productFuture);
            }

            for (Future future : list) {
                Object o = future.get();
                System.out.println(o);
            }
        } finally {
            context.shutdown();
        }
    }

5.@HystrixCollapser参数介绍

batchMethod,请求合并方法

scope,请求合并方式,REQUEST:请求范围,GLOBAL:全局范围,默认com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL

collapserProperties
HystrixPropertiesManager.MAX_REQUESTS_IN_BATCH 最大请求合并数量,默认Integer.MAX_VALUE
HystrixPropertiesManager.TIMER_DELAY_IN_MILLISECONDS 请求合并间隔毫秒值,默认10ms

注意,达到请求合并间隔毫秒值,没达到最大请求合并数量时,也会进行请求合并

@HystrixCollapser是基于Spring的AOP实现的

com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect

6.请求合并工作流程图


http://www.kler.cn/a/107499.html

相关文章:

  • 卷积神经网络之Yolo详解
  • git入门环境搭建
  • Ruby编程语言全景解析:从基础到进阶
  • 学法减分交管12123模拟练习小程序源码前端和后端和搭建教程
  • 文件夹被占用了无法删除怎么办?强制粉碎文件夹你可以这样操作
  • 利用阿里云下载 WebRTC 源码
  • Jquery 如何获取子元素。如何找到所有 HTML select 标签的选中项。jQuery 里的 ID 选择器和 class 选择器有何不同
  • 科聪协作(复合)移动机器人整体解决方案
  • 【JVM】类加载器
  • 指针进阶(1)
  • ce从初阶到大牛(两台主机免密登录)
  • 22、Python -- 创建对象和使用对象
  • 【394.字符串解码】
  • element-ui vue2 iframe 嵌入外链新解
  • STM32F4X SDIO(一) SD卡介绍
  • 星闪技术 NearLink 一种专门用于短距离数据传输的新型无线通信技术
  • 工业自动化产品抗干扰笔记
  • 【Elasticsearch】es脚本编程使用详解
  • OpenCV学习(六)——图像算术运算(加法、融合与按位运算)
  • 计算机视觉 激光雷达结合无监督学习进行物体检测的工作原理
  • [Unity]给场景中的3D字体TextMesh增加描边方案一
  • 指针仪表读数YOLOV8NANO
  • GPT出大错了!原来GPT不是万能的!这就是人类存在的意义!
  • sqoop和flume简单安装配置使用
  • Systemverilog中使用interface连接testbench和dut的端口
  • 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)rust解法