使用多线程提高rest服务性能

tomcat管理线程数量有限,当达到一定请求数量时,无法继续接受请求,使用多线程的方式,可以调用一个异步线程来执行。

执行逻辑如下图,tomcat就收http请求,调用一个副线程进行处理,副线程处理后,将结果返回给主线程。在副线程处理整个业务逻辑的过程中,主线程可以空闲出来,去处理其他请求。使得服务器的吞吐量可以有一个很大的提升。

用同步方式和异步方式编写两个请求(github地址

package com.ustc.reed.controller.async;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author reed fan
 *
 */
import java.util.concurrent.Callable;

@RestController
public class AsyncController {
    private static Logger logger  = LoggerFactory.getLogger(AsyncController.class);

    @GetMapping("/sync")
    public String sync() throws Exception{
        logger.info("主线程开始");
        Thread.sleep(1000);

        logger.info("主线程结束");
        return "success";
    }

    @GetMapping("/async")
    public Callable<String> async() throws Exception{
        logger.info("主线程开始");
        Callable<String> result = new Callable<String>() {
            @Override
            public String call() throws Exception {
                logger.info("副线程开始");
				Thread.sleep(1000);
				logger.info("副线程返回");
				return "success";
            }
        };
        logger.info("主线程结束");
        return result;
    }
}

在浏览器中输入http://localhost:8082/reed/sync

在浏览器中输入http://localhost:8082/reed/async

---

在实际开发中,可能遇到接收请求和响应请求不是同一个线程的场景。如下图,此时使用callable无法满足业务需求。

可以使用 DeferredResult来处理此类业务场景。

以下单处理场景为例,应用服务器1的线程1收到下单请求,将下单信息发送给消息队列。应用服务器2消费消息,进行下单处理。下单完成后,将结果返回给消息队列, 应用服务器有另外一个线程2来监听消息队列,当发现有订单处理结果的消息,根据消息的结果返回HTTP响应。 

线程1和线程2是完全隔离的,谁也不知道对方的存在。

-----

限于篇幅,新建一个MockQueue,来模拟下单处理。

package com.ustc.reed.service.sync;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
 * created by reedfan on 2019/1/6
 */

@Component
public class MockQueue {
    private String placeOrder;   //下单消息

    private String completeOrder;   //下单成功消息

    private Logger logger = LoggerFactory.getLogger(getClass());

    public String getPlaceOrder() {
        return placeOrder;
    }

    public void setPlaceOrder(String placeOrder) throws Exception {
        new Thread(() -> {
            logger.info("接到下单请求, " + placeOrder);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.completeOrder = placeOrder;
            logger.info("下单请求处理完毕," + placeOrder);
        }).start();
    }

    public String getCompleteOrder() {
        return completeOrder;
    }

    public void setCompleteOrder(String completeOrder) {
        this.completeOrder = completeOrder;
    }
}

------

每个订单号会有一个处理结果。DeferredResultHolder可以在图示的线程1和线程2之间传递DeferredResult这个对象map的key可以理解为订单号。

package com.ustc.reed.service.sync;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.HashMap;
import java.util.Map;

/**
 * created by reedfan on 2019/1/6
 */
@Component
public class DeferredResultHolder {

    private Map<String,DeferredResult<String>> map = new HashMap<>();

    public Map<String, DeferredResult<String>> getMap() {
        return map;
    }

    public void setMap(Map<String, DeferredResult<String>> map) {
        this.map = map;
    }
}

-------

监听下单是否完成,完成则返回订单结果。

package com.ustc.reed.service.sync;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * created by reedfan on 2019/1/6
 */

@Component
public class QueueListener implements ApplicationListener<ContextRefreshedEvent> {

    private Logger logger = LoggerFactory.getLogger(QueueListener.class);

    @Autowired
    private MockQueue mockQueue;

    @Autowired
    private DeferredResultHolder deferredResultHolder;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true){

                            if(StringUtils.isNotBlank(mockQueue.getCompleteOrder())){
                                String orderNumber = mockQueue.getCompleteOrder();
                                logger.info("返回订单结果:"+orderNumber);
                                deferredResultHolder.getMap().get(orderNumber).setResult("place order success");
                                mockQueue.setCompleteOrder(null);
                            }else {
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    }
                }

        ).start();

    }
}

请求代码如下。

package com.ustc.reed.controller.async;


import com.ustc.reed.service.sync.DeferredResultHolder;
import com.ustc.reed.service.sync.MockQueue;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
/**
 * @author reed fan
 *
 */
import java.util.concurrent.Callable;

@RestController
public class AsyncController {
    private static Logger logger  = LoggerFactory.getLogger(AsyncController.class);

    @Autowired
    private MockQueue mockQueue;

    @Autowired
    private DeferredResultHolder deferredResultHolder;

   
    @GetMapping("/mqasync")
    public DeferredResult<String> mqasync() throws Exception{
        logger.info("主线程开始");
        String orderNumber = RandomStringUtils.randomNumeric(8);
        mockQueue.setPlaceOrder(orderNumber);

        DeferredResult<String> result = new DeferredResult<>();
        deferredResultHolder.getMap().put(orderNumber, result);
        return result;

    }
}

在浏览器中输入http://localhost:8082/reed/mqasync ,模拟出编号为05804777的订单的处理过程,结果如下。

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务