服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot+虚拟线程,接口吞吐量成倍增加,太爽了!

SpringBoot+虚拟线程,接口吞吐量成倍增加,太爽了!

2023-12-18 15:31互联网架构小马哥 Java教程

我们将看到如何在spring-boot中利用loom虚拟线程。我们还将在JMeter的帮助下做一些负载测试,看看虚拟线程和普通线程的响应时间如何。 首先,虚拟线程是 Project Loom 的一部分。 此外,Loom 不会加速内存计算,例如并行流,这不是

我们将看到如何在spring-boot中利用loom虚拟线程。我们还将在JMeter的帮助下做一些负载测试,看看虚拟线程和普通线程的响应时间如何。

首先,虚拟线程是 Project Loom 的一部分。

此外,Loom 不会加速内存计算,例如并行流,这不是 Loom 的目标。

我们正在研究如何使用可用的相同硬件来提高应用程序吞吐量,即充分利用 CPU 的潜力,为此我们花费了大量精力。截至目前,我们能够利用 2% 到 3% 的 CPU。我在这篇博客中详细讨论了这一点:

https://medium.com/@anil.java.story/project-loom-virtual-threads-part-1-b17e327c8ba7

“我认为 Loom 项目将会消灭响应式编程”——Brian Goetz(Java 语言架构师)

让我们快速设置我们的 Spring Boot 项目。

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
  4.     <modelVersion>4.0.0</modelVersion> 
  5.     <parent> 
  6.         <groupId>org.springframework.boot</groupId> 
  7.         <artifactId>spring-boot-starter-parent</artifactId> 
  8.         <version>3.1.0</version> 
  9.         <relativePath/> <!-- lookup parent from repository --> 
  10.     </parent> 
  11.     <groupId>org.anil</groupId> 
  12.     <artifactId>virtualthread</artifactId> 
  13.     <version>0.0.1-SNAPSHOT</version> 
  14.     <name>virtualthread</name> 
  15.     <description>virtualthread</description> 
  16.     <properties> 
  17.         <java.version>20</java.version> 
  18.         <tomcat.version>11.0.0-M4</tomcat.version> 
  19.     </properties> 
  20.     <dependencies> 
  21.         <dependency> 
  22.             <groupId>org.springframework.boot</groupId> 
  23.             <artifactId>spring-boot-starter-data-jpa</artifactId> 
  24.         </dependency> 
  25.         <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> 
  26.         <dependency> 
  27.             <groupId>org.apache.commons</groupId> 
  28.             <artifactId>commons-lang3</artifactId> 
  29.             <version>3.12.0</version> 
  30.         </dependency> 
  31.  
  32.         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
  33.  
  34.         <dependency> 
  35.             <groupId>mysql</groupId> 
  36.             <artifactId>mysql-connector-java</artifactId> 
  37.             <scope>runtime</scope> 
  38.             <version>8.0.33</version> 
  39.         </dependency> 
  40.  
  41.  
  42.         <dependency> 
  43.             <groupId>org.springframework.boot</groupId> 
  44.             <artifactId>spring-boot-starter-web</artifactId> 
  45.         </dependency> 
  46.  
  47.         <dependency> 
  48.             <groupId>org.springframework.boot</groupId> 
  49.             <artifactId>spring-boot-starter-test</artifactId> 
  50.             <scope>test</scope> 
  51.         </dependency> 
  52.         <dependency> 
  53.             <groupId>org.projectlombok</groupId> 
  54.             <artifactId>lombok</artifactId> 
  55.             <version>1.18.24</version> 
  56.             <scope>compile</scope> 
  57.         </dependency> 
  58.     </dependencies> 
  59.  
  60.     <build> 
  61.         <plugins> 
  62.             <plugin> 
  63.                 <groupId>org.springframework.boot</groupId> 
  64.                 <artifactId>spring-boot-maven-plugin</artifactId> 
  65.             </plugin> 
  66.             <plugin> 
  67.                 <groupId>org.apache.maven.plugins</groupId> 
  68.                 <artifactId>maven-compiler-plugin</artifactId> 
  69.               <configuration> 
  70.                   <compilerArgs> 
  71.                       <arg>--enable-preview</arg> 
  72.                       </compilerArgs> 
  73.                   <source>20</source> 
  74.                   <target>20</target> 
  75.               </configuration> 
  76.  
  77.             </plugin> 
  78.         </plugins> 
  79.     </build> 
  80.  
  81. </project> 

由于 Project Loom 处于预览阶段,我们需要启用预览功能。

package org.anil.virtualthread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executors;

@SpringBootApplication
@Slf4j
public class VirtualthreadApplication {

    public static void main(String[] args) {
        SpringApplication.run(VirtualthreadApplication.class, args);
    }

    @Bean
    public TomcatProtocolHandlerCustomizer< ?> protocolHandlerVirtualThreadExecutorCustomizer() {
        return protocolHandler -> {
            log.info("Configuring " + protocolHandler + " to use VirtualThreadPerTaskExecutor");
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }

}

到目前为止,我们需要为 Tomcat 服务器配置虚拟线程设置。将来,这可能会在自动配置本身中得到解决。

package org.anil.virtualthread;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HomeController {

    @Autowired
    ProductRepository productRepository;


    @GetMapping("/thread")
    public List< Product> checkThread() throws InterruptedException {
        Thread.sleep(1000);
        return productRepository.findAll();
    }


    @PostMapping("/save")
    public String saveProduct() throws InterruptedException {
        for(int i=0; i< 1000; i++){
            Product product = new Product();
            product.setProductName(RandomStringUtils.randomAlphanumeric(5));
            product.setPrice(RandomUtils.nextLong(10,1000));
            product.setPrice(1L);
            productRepository.save(product);
        }
        return "anil";
    }
}

我们有一个GetMapping返回所有结果,我们的数据库中有 1000 条数据。我们已经让线程休眠 1 秒。让我们看看我们的Product实体和ProductRepository。

package org.anil.virtualthread;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String productName;
    private Long price;
}
package org.anil.virtualthread;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository< Product,Long> {
}

让我们看看我们的 application.yaml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    maxIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    hikari:
      connection-timeout: 60000
      maximum-pool-size: 10
      minimum-idle: 5
    url: jdbc:mysql://localhost:3306/todos
    testWhileIdle: true
    username: root
    password: root1234
    validationQuery: SELECT 1
  flyway:
    baseline-version: 0
    enabled: true
    validate-on-migrate: false
  jpa:
    database: mysql
    generate-ddl: true
    hibernate:
      ddl-auto: none
      format_sql: true
    show-sql: true

现在,我们首先通过注释以下行来运行应用程序,这将在普通线程上运行我们的应用程序

package org.anil.virtualthread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executors;

@SpringBootApplication
@Slf4j
public class VirtualthreadApplication {

    public static void main(String[] args) {
        SpringApplication.run(VirtualthreadApplication.class, args);
    }

//    @Bean
//    public TomcatProtocolHandlerCustomizer< ?> protocolHandlerVirtualThreadExecutorCustomizer() {
//        return protocolHandler -> {
//            log.info("Configuring " + protocolHandler + " to use VirtualThreadPerTaskExecutor");
//            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
//        };
//    }
}

现在让我们设置JMeter。我们将有 1000 个请求,该请求将在 3 秒内增加。并且这样的状态会持续200秒。每 3 秒,将触发 1000 个 GET (“/thread”) 请求。我们还添加了响应时间图侦听器。

SpringBoot+虚拟线程,接口吞吐量成倍增加,太爽了!

现在让我们运行测试并等待 200 秒。

SpringBoot+虚拟线程,接口吞吐量成倍增加,太爽了!

从图中我们可以看到,一旦Tomcat的整个线程池被利用,响应时间从3600毫秒猛增到5200毫秒。从那时起,只有当以前的线程被释放时,它才保持这种状态。

现在让我们在启用虚拟线程功能的情况下运行负载测试。

package org.anil.virtualthread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executors;

@SpringBootApplication
@Slf4j
public class VirtualthreadApplication {

    public static void main(String[] args) {
        SpringApplication.run(VirtualthreadApplication.class, args);
    }

    @Bean
    public TomcatProtocolHandlerCustomizer< ?> protocolHandlerVirtualThreadExecutorCustomizer() {
        return protocolHandler -> {
            log.info("Configuring " + protocolHandler + " to use VirtualThreadPerTaskExecutor");
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }

}

现在让我们运行测试并等待 200 秒。

SpringBoot+虚拟线程,接口吞吐量成倍增加,太爽了!

显然,现在并发 1000 个请求的响应时间几乎略高于 1000 毫秒,有时甚至会达到 1400 毫秒,这比我们使用普通线程时要好得多。

显然,当我们需要充分利用底层 CPU 时,我们应该开始在应用程序中采用虚拟线程,突然间我们可以看到,对于相同的硬件,应用程序的吞吐量增加了很多倍。

这比切换到反应式编程要好得多,反应式编程意味着重写所有代码,这很难先学习,然后编写,甚至更难调试和分析。

简而言之,更多用户可以使用该应用程序并与第一个用户同时获得响应。

原文地址:https://www.toutiao.com/article/7303799650279178786/

延伸 · 阅读

精彩推荐