使用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