记录一次提高接口qps的过程(初次体验Spring WebFlux)

预备知识:

wrk压测工具(一款简单的HTTP压测工具)

Usage: wrk <options> <url>
  Options:
    -c, --connections <N>  Connections to keep open
    -d, --duration    <T>  Duration of test
    -t, --threads     <N>  Number of threads to use

    -s, --script      <S>  Load Lua script file
    -H, --header      <H>  Add header to request
        --latency          Print latency statistics
        --timeout     <T>  Socket/request timeout
    -v, --version          Print version details

  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)

压测接口命令

./wrk -t16 -c1000 -d30s http://172.17.xxx.xxx:8080/xxx/process?imei=xxxx\&oaid=xxxx\&idfa=\&os=1

这里使用 Spring WebFlux 和自定义线程池来提高并发量

核心脱敏代码如下

package com.szlm.rtaserver.work.controller;


import com.szlm.rtaserver.work.Utils.R;
import com.szlm.rtaserver.work.entity.RtaRequestVo;
import com.szlm.rtaserver.work.handler.MessageHandler;
import com.szlm.rtaserver.work.service.TaoBaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Title: TaoBaoController
 * @Description:
 * @author: tangyao
 * @date: 2022/3/11 10:16
 * @Version: 1.0
 */
@Slf4j
@RestController()
@RequestMapping("/xxxx")
public class TaoBaoRtaController {


    @Autowired
    TaoBaoService taoBaoService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private MessageHandler messageHandler;


    @GetMapping("/process")
    public Mono<R> process(@RequestParam("imeiMd5") String imeiMd5,
                           @RequestParam("oaidMd5") String oaidMd5,
                           @RequestParam("idfaMd5") String idfaMd5,
                           @RequestParam(value = "os") int os) {
  		
        RtaRequestVo rtaRequestVo = new RtaRequestVo();
        rtaRequestVo.setImeiMd5(imeiMd5);
        rtaRequestVo.setOaidMd5(oaidMd5);
        rtaRequestVo.setIdfaMd5(idfaMd5);
        rtaRequestVo.setOs(os);

        if (StringUtils.isEmpty(imeiMd5) && StringUtils.isEmpty(oaidMd5) && StringUtils.isEmpty(idfaMd5)) {

            return Mono.create(monoSink -> {
                monoSink.success(R.ok().setData("imeiMd5 , oaidMd5 ,idfaMd5三者至少提供一个"));
            });
        }


        return this.messageHandler.rtaHandler(rtaRequestVo);

    }

}
package com.szlm.rtaserver.work.handler;

import com.szlm.rtaserver.work.Utils.R;
import com.szlm.rtaserver.work.entity.RtaRequestVo;
import com.szlm.rtaserver.work.service.TaoBaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Classname MessageHandler
 * @Description TODO
 * @Date 2022/3/14 9:13 下午
 * @Author by tangyao
 */
@Component
@Slf4j
public class MessageHandler {

    @Autowired
    TaoBaoService taoBaoService;
    @Autowired
    ThreadPoolExecutor threadPoolExecutor;


    public Mono<R> rtaHandler(RtaRequestVo rtaRequestVo) {

        return Mono.create(monoSink ->
                CompletableFuture.supplyAsync(
                                () -> taoBaoService.callInterfaceTaoBao(rtaRequestVo)
                                , threadPoolExecutor)
                        .whenComplete((aBoolean, e) -> {
                            if (e != null) {
                                log.error(e.getMessage());
                            }

                            R r;
                            if (aBoolean == null) {
                                String msg = "您的请求过于频繁,请稍后重试您的请求过于频繁,请稍后重试;";
                                log.info(msg);
                                r = R.error(msg);
                            } else {
                                r = R.ok().setData(aBoolean);
                            }
                            monoSink.success(r);
                        }));

    }


}

带宽100m

调用淘宝接口的connectionTimeout和readTimeout为300ms

压测时长 120s

测试直接调用淘宝接口的QPS为 16500左右

调用自己接口统计

总次数QPS接口超时返回值rsp的errcode为null或errcode!=0请求成功率
1563177130195984590797.06%
1563652130223713799897.55%
1559918129921094024696.99%
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值