springboot + vue+ redis 多线程查询外部接口,数据量大崩溃问题完美解决。

需求描述

自己的系统要接入几个外部系统的数据,即同一个查询条件需要同时从不同的局域网系统中获取数据并汇总到当前系统中展示。数据量大的时候,查询经常会崩掉。

解决办法是前端调用后端服务时,立刻返回一个任务id(因为前端响应时间大概是5s,超过5s就报错,立刻返回不会让用户有错觉),同时设置一个定时器setInterval(),根据返回的任务id按时查询redis缓存的进度状态,如果缓存状态为ongoing ,就继续定时任务,如果为end,则查询redis中返回的结果,并停止定时任务clearInterval()。如果为error,则停止定时任务clearInterval(),并通知用户任务出错。

后端通过多线程异步调用各个外部系统数据,进行中时设置redis的key为任务id,并设置进度状态为ongoing,出错时设置redis的key为任务id,进度状态设置为error,查询完成后设置redis的key为任务id,进度状态为end。 同时将结果缓存到redis中,这个redis的key可以设置为任务id + “:result” 同状态区分一下。

vue 前端

		watch: {
                      'listLoading': function(newVal, oldVal) {

                if (newVal && !oldVal) {
                    let _this = this;
                    var data = {
                        zbc: this.zbcName ? this.zbcName : '',
                        trainType : this.trainType,
                        ch : this.ch,
                        kssj: this.kssj,
                        jssj: this.jssj,
                        jcnr: this.jcnr
                    };
                    this.getProgressTimer = setInterval(async ()=>{
                        try {

                            if (this.zbcTaskId){
                            	// 获取查询进度
                                let resp = await  getZbProgress(this.zbcTaskId,data);
                                let progress = resp.bodyText;
                                // 进度在进行中
                                if (progress == 'ongoing'){
								
								// 进度为已结束
                                }else if (progress == 'end'){
                                	// 清除定时任务
                                    if (_this.getProgressTimer) {
                                        clearInterval(_this.getProgressTimer);
                                        _this.getProgressTimer = null;
                                    }
                                    // 获取错误信息并提示
                                    let errMessge = await getZbErrMessage(this.zbcTaskId);
                                    if (errMessge.bodyText){
                                        this.$message({
                                            message: errMessge.bodyText + '连接超时,请确认是否存在!',
                                            type:'warning'
                                        });
                                    }
                                    // 从redis中获取查询的结果
                                    let result = await getZbccrkResult(this.zbcTaskId,data);
                                    // 格式化查询的结果
                                    _this.formateTableData(result);
								// 进度已查询,并且全部为error
                                }else if (progress == 'error'){
									// 清除定时任务
                                    if (_this.getProgressTimer) {
                                        clearInterval(_this.getProgressTimer);
                                        _this.getProgressTimer = null;
                                    }
                                    this.zbcTaskId = '';
                                    this.$message.error('数据获取出错!');
                                    this.listLoading = false;
                                }
                            }

                        } catch (e) {
                            if (_this.getProgressTimer) {
                                clearInterval(_this.getProgressTimer);
                                _this.getProgressTimer = null;
                            }
                            this.zbcTaskId = '';
                            //this.$message.error('数据获取出错!');
                            this.listLoading = false;
                        }
                    }, 1000);
                } else if (!newVal && oldVal) {
                    if (this.getProgressTimer) {
                        clearInterval(this.getProgressTimer);
                        this.getProgressTimer = null;
                    }
                    this.zbcTaskId = '';
                    this.listLoading = false;
                }
            }
        },
		mounted() {
          this.queryZbInfo();
        },
        methods: {
            formateTableData(result){
                this.tableData = result.body;
                this.listLoading = false;
            },
     
            // 查询信息
            queryZbInfo: function (flg) {
                var data = {
                    zbc: this.zbcName ? this.zbcName : '',
                    cx : this.cx,
                    ch : this.ch,
                    kssj: this.kssj,
                    jssj: this.jssj,
                    jcnr: this.jcnr
                };
                zbToInOut(data).then(
                    (response) => {
                        this.zbcTaskId = response.bodyText;//获取的任务id
                        // 开启动画并监听
                        this.listLoading = true; 
                    },
                    (response) => {
                        this.$message.error('获取数据失败!');
                        this.listLoading = false;
                    }
                )
            },
        }

后端开发

参考的博文https://zhuanlan.zhihu.com/p/134636915

1. 异步设置

工程的application中添加@EnableAsync;
添加async的配置文件AsyncConfiguration 。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfiguration {

    @Bean("zbcrkExecutor")
    public Executor doSomethingExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数:线程池创建时候初始化的线程数
        executor.setCorePoolSize(10);
        // 最大线程数:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
        executor.setMaxPoolSize(20);
        // 缓冲队列:用来缓冲执行任务的队列
        executor.setQueueCapacity(500);
        // 允许线程的空闲时间60秒:当超过了核心线程之外的线程在空闲时间到达之后会被销毁
        executor.setKeepAliveSeconds(60);
        // 线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
        executor.setThreadNamePrefix("zbc-do-something-");
        // 缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        executor.initialize();
        return executor;
    }

}

2.controller

因为我的多线程调用是通过for循环进行,所以调用放在contrller中。

   @PostMapping("/zbToJx/returnJccrk")
       public String selectZbToJxJccrk(@RequestBody final CheckInOut checkinout) throws Exception{
        String taskId = "zbccrk_Task_" + new Long((new Date()).getTime()).toString();
        this.redisUtil.del("num");
        try{
            List<Zbc> zbcList = repairStandardFeign.getAllZbc();
            this.redisUtil.set(taskId,"ongoing",600); //进行中
            if (StringUtils.isEmpty(checkinout.getZbc())) {
                // 未指定
                for (int i = 0; i < zbcList.size(); i++) {
                    Thread.sleep(1000);
                   reportWorkService.selectZbToJxJccrk(taskId,checkinout ,zbcList.get(i),i);
                }
            } else {
                // 指定
                Zbc zbcGoal = new Zbc();
                for (Zbc zbc : zbcList){
                    if (zbc.getName().equals(checkinout.getZbc())){
                        zbcGoal = zbc;
                        break;
                    }
                }
                reportWorkService.selectZbToJxJccrk(taskId,checkinout ,zbcGoal,0);
            }
        }catch(Exception e){
            e.printStackTrace();
            this.redisUtil.set(taskId,"error",600);
        }
        return taskId;
    }

// 获取错误信息
@GetMapping("/zb/errorMessage/{taskId}")
    public String getZbErrMessage(@PathVariable("taskId") String taskId) throws Exception {
        return reportWorkService.getZbErrMessage(taskId);
    }


    /**
     * 获取进度
     * @param taskId
     * @return
     * @throws Exception
     */
    @PostMapping("/progress/{taskId}")
    public String getZbProgress(@PathVariable("taskId") String taskId,@RequestBody final CheckInOut checkinout) throws Exception {
        return reportWorkService.getZbProgress(taskId,checkinout);
    }

    /**
     * 获取缓存结果
     * @param taskId
     * @return
     * @throws Exception
     */
    @PostMapping("/zbccrk/result/{taskId}")
    public List<Zbccrk> getZbccrkResult(@PathVariable("taskId") String taskId,@RequestBody final CheckInOut checkinout) throws Exception {
        return reportWorkService.getZbccrkResult(taskId,checkinout);
    }

3. service

CompletableFuture<String> selectZbToJxJccrk(CheckInOut checkinout, Zbc zbc) throws Exception;

4.serviceImpl

多线程调用

	private ToZbFeignDynamicUrl toZbFeignDynamicUrl;

	/**
	查询外部系统接口的数据,例如有3个接口,for循环一个一个查询,当有一个feign查询出错也要通过redis记录下来。
	redis中记录num ,num 用来记录总查询次数,进度查询时可根据次数判断是否都查询过。
	redis中记录taskId-error ,taskId-error用来记录出错的url的信息,可返回给前端出错的信息。
	**/
    @Override
    @Async("zbcrkExecutor")
    public void selectZbToJxJccrk(String taskId,CheckInOut checkinout, Zbc zbc,int j) throws Exception {
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName());
        String result = "";
        try{
            // 配置feign 通过feign来跟外部系统通讯
            toZbFeignDynamicUrl =  Feign.builder()
                    .encoder(new FeignEncoder())
                    .decoder(new FeignStringDecoder())
                    .target(ToZbFeignDynamicUrl.class, zbc.getUrl());
             result = toZbFeignDynamicUrl.selectZb(checkinout).getBody() ;
            List<Zbccrk> zbccrks = Lists.newArrayList();
            if (!"\"\"".equals(result) && result != null && StringUtils.isNotBlank(result)){
                String zbbhSingle = new String(result.getBytes("UTF-8"),"UTF-8").replace("\\","");
                zbbhSingle.toString().trim();
                zbbhSingle = zbbhSingle.substring(1,zbbhSingle.length() - 1);
                JSONArray jsonArray = new JSONArray(zbbhSingle); //真实数据需要替换?
                List<Zbccrk> zbccrkList = jsonArray.toList(Zbccrk.class);
                for (int i = 0; i < zbccrkList.size();i++){
                    zbccrkList.get(i).setZBC(zbc.getName());
                }
                zbccrks.addAll(zbccrkList);
            }
            // num 用来记录查询了几个url,进度查询时需要根据它来判断
            if (this.redisUtil.get("num") != null){
                int num = Integer.parseInt(this.redisUtil.get("num").toString()) + 1;
                System.out.println("num:"  + num);
                this.redisUtil.set("num",num,600);
            }else {
                this.redisUtil.set("num",1,600);
            }
             this.redisUtil.set(taskId + "-" + j , zbccrks);
       }catch (feign.RetryableException e){ // feign查询超时
   			// num 用来记录查询了几个url
            if (this.redisUtil.get("num") != null){
                int num = Integer.parseInt(this.redisUtil.get("num").toString()) + 1; // 下标+1
                System.out.println("error-num: " + num);
                this.redisUtil.set("num",num,600);
            }else {
                this.redisUtil.set("num",1,600);
            }
            // taskId-j 用来记录当前出错的url
            this.redisUtil.set(taskId+ "-" + j , "error");
            if (this.redisUtil.get(taskId + "-error" ) != null){
                String errMessage = this.redisUtil.get(taskId + "-error").toString();
                this.redisUtil.set(taskId + "-error",errMessage + "," + zbc.getName(),600);
            }else {
                this.redisUtil.set(taskId + "-error", zbc.getName(),600);
            }

       }

    }

// 根据taskId获取缓存在redis中的数据
@Override
 public List<Zbbh> getZbbhResult(String taskId,final CheckInOut checkinout) throws Exception{

        List<Zbbh> zbbhs = Lists.newArrayList();
        // 查询条件为空
        if (StringUtils.isEmpty(checkinout.getZbc())) {
            int num = reportWorkDao.getZbNum();
            for (int i = 0; i < num; i++){
            	// 从redis中获取数据
                Object object = this.redisUtil.get(taskId + "-" + i);
                if (object != null && !object.toString().equals("error")){
                    JSONArray jsonArray = new JSONArray(object);
                    zbbhs.addAll(jsonArray.toList(Zbbh.class));
                }
            }
            // 查询条件不为空的时候,默认查询task-0
        }else {
            Object object = this.redisUtil.get(taskId + "-" + 0);
            if (object != null && !object.toString().equals("error")){
                JSONArray jsonArray = new JSONArray(object);
                zbbhs.addAll(jsonArray.toList(Zbbh.class));
            }
        }
        // 格式化数据
        getZbbhCommon(zbbhs);
        return zbbhs;
    }

	// 格式化数据
    private void getZbbhCommon(List<Zbbh> zbbhs){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < zbbhs.size(); i++){
            try {
                Date date = sdf.parse(zbbhs.get(i).getFSTIME());
                String newSJ= formatDate(calendar,date);
                zbbhs.get(i).setFSTIME(newSJ);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
		
		// 按时间排序
        Collections.sort(zbbhs, new Comparator<Zbbh>() {
            @Override
            public int compare(Zbbh o1, Zbbh o2) {
                if (o1 != null && o2 != null && StringUtils.isNotEmpty(o1.getFSTIME()) && StringUtils.isNotEmpty(o2.getFSTIME())){
                    return o2.getFSTIME().compareTo(o1.getFSTIME());
                }
                return -1;
            }
        });
    }

	// 获取查询进度
    @Override
    public String getZbProgress(String taskId,final CheckInOut checkinout) throws Exception {
        if (StringUtils.isEmpty(checkinout.getZbc())) {
            List<Zbc> zbcList = repairStandardFeign.getAllZbc();
            if (this.redisUtil.get("num") != null){
                String numStr = this.redisUtil.get("num").toString();
                if (Integer.parseInt(numStr) == zbcList.size()){
                    return "end"; //结束
                }
            }
        }else {
            if (this.redisUtil.get("num") != null){
                String numStr = this.redisUtil.get("num").toString();
                if (Integer.parseInt(numStr) == 1){
                    return "end"; // 结束
                }
            }
        }
        String p = (String)this.redisUtil.get(taskId);
        return p != null ? p : "";
    }

ToZbFeignDynamicUrl .java

@FeignClient(name = "zb-service-dynamic-url")
public interface ToZbFeignDynamicUrl {
    @RequestLine("POST /work/zbToJx/returnJccrk")
    @Headers({"Content-Type: application/json"})
    ResponseEntity<String> selectZb(@RequestBody CheckInOut checkinout) throws Exception;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值