使用多线程的方式模拟高并发请求接口,用于自测接口的稳定性【项目】

java 复制代码
 package com.gitee.taven.test;

import com.gitee.taven.ApiResult;
import com.gitee.taven.aop.RepeatSubmitAspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class RunTest implements ApplicationRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(RunTest.class);

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("执行多线程测试");
        String url="http://localhost:8080/submit";
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for(int i=0; i<10; i++){
            String userId = "userId" + i;
            HttpEntity request = buildRequest(userId);
            executorService.submit(() -> {
                try {
                    countDownLatch.await();
                    System.out.println("Thread:"+Thread.currentThread().getName()+", time:"+System.currentTimeMillis());
                    ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
                    System.out.println("Thread:"+Thread.currentThread().getName() + "," + response.getBody());

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        countDownLatch.countDown();
    }

    private HttpEntity buildRequest(String userId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "yourToken");
        Map<String, Object> body = new HashMap<>();
        body.put("userId", userId);
        return new HttpEntity<>(body, headers);
    }

}

        countDownLatch.countDown();
    }
相关推荐
静若繁花_jingjing4 分钟前
JVM常量池
java·开发语言·jvm
David爱编程40 分钟前
为什么线程不是越多越好?一文讲透上下文切换成本
java·后端
A尘埃1 小时前
Redis在地理空间数据+实时数据分析中的具体应用场景
java·redis
csxin1 小时前
Spring Boot 中如何设置 serializer 的 TimeZone
java·后端
杨过过儿1 小时前
【Task02】:四步构建简单rag(第一章3节)
android·java·数据库
青云交1 小时前
Java 大视界 -- Java 大数据分布式计算在基因测序数据分析与精准医疗中的应用(400)
java·hadoop·spark·分布式计算·基因测序·java 大数据·精准医疗
荔枝爱编程1 小时前
如何在 Docker 容器中使用 Arthas 监控 Java 应用
java·后端·docker
喵手1 小时前
Java中Stream与集合框架的差异:如何通过Stream提升效率!
java·后端·java ee
JavaArchJourney1 小时前
PriorityQueue 源码分析
java·源码
喵手2 小时前
你知道,如何使用Java的多线程机制优化高并发应用吗?
java·后端·java ee