Spring Boot 3.3 五種異步處理提升系統(tǒng)吞吐量的策略與實(shí)踐,徹底愛了!
在現(xiàn)代應(yīng)用程序中,系統(tǒng)吞吐量和響應(yīng)速度是關(guān)鍵的性能指標(biāo)。在高并發(fā)環(huán)境下,傳統(tǒng)的同步處理方式可能會(huì)成為系統(tǒng)瓶頸,導(dǎo)致資源浪費(fèi)和響應(yīng)延遲。為了應(yīng)對(duì)這種挑戰(zhàn),Spring Boot 3.3 提供了強(qiáng)大的異步處理能力,使我們能夠通過異步接口顯著提高系統(tǒng)的吞吐量和響應(yīng)速度。
異步接口的必要性
異步接口的核心在于能夠并行處理多個(gè)請(qǐng)求,而不阻塞主線程。這對(duì)于提高系統(tǒng)吞吐量和用戶體驗(yàn)至關(guān)重要。傳統(tǒng)的同步處理方式需要等待每個(gè)請(qǐng)求完成才能處理下一個(gè)請(qǐng)求,這種模型在處理大量請(qǐng)求時(shí)容易造成資源的緊張和延遲的積累。異步處理則通過將任務(wù)分配給后臺(tái)線程,允許主線程繼續(xù)處理其他請(qǐng)求,從而有效減少了請(qǐng)求的等待時(shí)間和系統(tǒng)的負(fù)載。
異步接口的實(shí)現(xiàn)方式
在 Spring Boot 中,實(shí)現(xiàn)異步接口主要有以下幾種方式:
- 使用 @Async 注解:Spring 的 @Async 注解可以將方法標(biāo)記為異步執(zhí)行。被注解的方法將會(huì)在一個(gè)獨(dú)立的線程中運(yùn)行,允許主線程繼續(xù)執(zhí)行其他操作。
- 使用 CompletableFuture: CompletableFuture 提供了一種更靈活的方式來處理異步任務(wù)。它不僅支持簡(jiǎn)單的異步操作,還提供了豐富的 API 來處理任務(wù)的組合、異常處理和結(jié)果回調(diào)。
- 使用 WebFlux:對(duì)于更復(fù)雜的異步場(chǎng)景,Spring WebFlux 提供了反應(yīng)式編程模型,支持高效的非阻塞操作。雖然本文主要集中于傳統(tǒng)的異步處理方式,WebFlux 也是一種值得關(guān)注的異步解決方案。
- 使用 WebAsyncTask:是 Spring MVC 提供的一種異步處理機(jī)制,允許在后臺(tái)線程中執(zhí)行長(zhǎng)時(shí)間運(yùn)行的任務(wù),并在任務(wù)完成后將結(jié)果返回給客戶端。
- 使用 DeferredResult:是 Spring MVC 提供的一種異步處理機(jī)制,可以在后臺(tái)線程中執(zhí)行任務(wù)并將結(jié)果返回給客戶端。它提供了更靈活的結(jié)果處理方式。
運(yùn)行效果:
圖片
若想獲取項(xiàng)目完整代碼以及其他文章的項(xiàng)目源碼,且在代碼編寫時(shí)遇到問題需要咨詢交流,歡迎加入下方的知識(shí)星球。
項(xiàng)目結(jié)構(gòu)
本文將展示如何使用 Spring Boot 3.3 的異步功能來創(chuàng)建一個(gè)示例項(xiàng)目,通過簡(jiǎn)單的示例來說明如何配置異步接口,并在前端使用 jQuery 實(shí)現(xiàn)異步請(qǐng)求的觸發(fā)和結(jié)果展示。項(xiàng)目結(jié)構(gòu)如下:
async-demo
│
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── icoderoad
│ │ │ └── async
│ │ │ ├── AsyncDemoApplication.java
│ │ │ ├── controller
│ │ │ │ └── AsyncController.java
│ │ │ └── service
│ │ │ └── AsyncService.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── templates
│ │ └── index.html
└── pom.xml
Maven 配置
首先,配置 pom.xml 文件,添加所需的依賴:
<?xml versinotallow="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.icoderoad</groupId>
<artifactId>async-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>async-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 application.yml
配置文件 application.yml 用于設(shè)置應(yīng)用程序?qū)傩裕?/p>
server:
port: 8080
spring:
application:
name: async-demo
thymeleaf:
cache: false
web:
resources:
add-mappings: false
配置異步支持
在 Spring Boot 中啟用異步支持,需要在主應(yīng)用類上添加 @EnableAsync 注解:
package com.icoderoad.async;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class AsyncDemoApplication {
public static void main(String[] args) {
SpringApplication.run(AsyncDemoApplication.class, args);
}
}
創(chuàng)建異步服務(wù)
創(chuàng)建一個(gè)服務(wù)類 AsyncService,實(shí)現(xiàn)以下五種異步處理方式:
使用 @Async 注解
package com.icoderoad.async.service;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncTask;
import reactor.core.publisher.Mono;
@Service
public class AsyncService {
private final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
/**
* 使用 @Async 注解的異步任務(wù)
* @return CompletableFuture<String> 異步任務(wù)的結(jié)果
* @throws InterruptedException 如果線程被中斷
*/
@Async
public CompletableFuture<String> asyncTaskWithAsyncAnnotation() throws InterruptedException {
int delay = ThreadLocalRandom.current().nextInt(1000, 5000); // 隨機(jī)生成任務(wù)延遲時(shí)間
Thread.sleep(delay); // 模擬任務(wù)執(zhí)行時(shí)間
return CompletableFuture.completedFuture("使用 @Async 注解的任務(wù)完成,耗時(shí) " + delay + " 毫秒");
}
/**
* 使用 CompletableFuture 進(jìn)行異步任務(wù)
* @return CompletableFuture<String> 異步任務(wù)的結(jié)果
* @throws InterruptedException 如果線程被中斷
*/
public CompletableFuture<String> asyncTaskWithCompletableFuture() throws InterruptedException {
int delay = ThreadLocalRandom.current().nextInt(1000, 5000); // 隨機(jī)生成任務(wù)延遲時(shí)間
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(delay); // 模擬任務(wù)執(zhí)行時(shí)間
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 恢復(fù)中斷狀態(tài)
}
return "使用 CompletableFuture 任務(wù)完成,耗時(shí) " + delay + " 毫秒";
});
}
/**
* 使用 WebFlux 進(jìn)行異步任務(wù)
* @return Mono<String> 異步任務(wù)的結(jié)果
*/
public Mono<String> asyncTaskWithWebFlux() {
return Mono.fromCallable(() -> {
int delay = ThreadLocalRandom.current().nextInt(1000, 5000); // 隨機(jī)生成任務(wù)延遲時(shí)間
Thread.sleep(delay); // 模擬任務(wù)執(zhí)行時(shí)間
return "使用 WebFlux 任務(wù)完成,耗時(shí) " + delay + " 毫秒";
}).delayElement(Duration.ofMillis(1000)); // 添加延遲以模擬任務(wù)
}
/**
* 使用 WebAsyncTask 進(jìn)行異步任務(wù)
* @return WebAsyncTask<String> 異步任務(wù)的結(jié)果
*/
public WebAsyncTask<String> asyncTaskWithWebAsyncTask() {
Callable<String> callable = () -> {
int delay = ThreadLocalRandom.current().nextInt(1000, 5000); // 隨機(jī)生成任務(wù)延遲時(shí)間
Thread.sleep(delay); // 模擬任務(wù)執(zhí)行時(shí)間
return "使用 WebAsyncTask 任務(wù)完成,耗時(shí) " + delay + " 毫秒";
};
return new WebAsyncTask<>(callable); // 創(chuàng)建并返回 WebAsyncTask
}
/**
* 使用 DeferredResult 進(jìn)行異步任務(wù)
* @return DeferredResult<String> 異步任務(wù)的結(jié)果
*/
public DeferredResult<String> asyncTaskWithDeferredResult() {
DeferredResult<String> deferredResult = new DeferredResult<>();
executor.submit(() -> {
try {
int delay = ThreadLocalRandom.current().nextInt(1000, 5000); // 隨機(jī)生成任務(wù)延遲時(shí)間
Thread.sleep(delay); // 模擬任務(wù)執(zhí)行時(shí)間
deferredResult.setResult("使用 DeferredResult 任務(wù)完成,耗時(shí) " + delay + " 毫秒");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 恢復(fù)中斷狀態(tài)
deferredResult.setErrorResult("發(fā)生錯(cuò)誤"); // 設(shè)置錯(cuò)誤結(jié)果
}
});
return deferredResult; // 返回 DeferredResult
}
}
創(chuàng)建控制器
創(chuàng)建控制器 AsyncController 來處理 HTTP 請(qǐng)求:
package com.icoderoad.async.controller;
import java.util.concurrent.CompletableFuture;
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.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncTask;
import com.icoderoad.async.service.AsyncService;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/api")
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/async-annotation")
public CompletableFuture<String> asyncTaskWithAsyncAnnotation() throws InterruptedException {
return asyncService.asyncTaskWithAsyncAnnotation();
}
@GetMapping("/completable-future")
public CompletableFuture<String> asyncTaskWithCompletableFuture() throws InterruptedException {
return asyncService.asyncTaskWithCompletableFuture();
}
@GetMapping("/webflux")
public Mono<String> asyncTaskWithWebFlux() {
return asyncService.asyncTaskWithWebFlux();
}
@GetMapping("/webasync")
public WebAsyncTask<String> webAsyncTask() {
return asyncService.asyncTaskWithWebAsyncTask();
}
@GetMapping("/deferredresult")
public DeferredResult<String> deferredResult() {
return asyncService.asyncTaskWithDeferredResult();
}
}
視圖控制器
package com.icoderoad.async.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/")
public String index() {
return "index";
}
}
創(chuàng)建前端頁面
在src/main/resources/templates目錄下創(chuàng)建 index.html 頁面來展示五種異步請(qǐng)求的結(jié)果,并使用 jQuery 實(shí)現(xiàn)異步請(qǐng)求的觸發(fā):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot 異步接口示例</title>
<link rel="stylesheet" >
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
<h1 class="mt-5">Spring Boot 異步接口示例</h1>
<button id="async-annotation" class="btn btn-primary mt-3">調(diào)用 @Async 注解接口</button>
<p id="result-async-annotation"></p>
<button id="completable-future" class="btn btn-primary mt-3">調(diào)用 CompletableFuture 接口</button>
<p id="result-completable-future"></p>
<button id="webflux" class="btn btn-primary mt-3">調(diào)用 WebFlux 接口</button>
<p id="result-webflux"></p>
<button id="webasync" class="btn btn-primary mt-3">調(diào)用 WebAsyncTask 接口</button>
<p id="result-webasync"></p>
<button id="deferredresult" class="btn btn-primary mt-3">調(diào)用 DeferredResult 接口</button>
<p id="result-deferredresult"></p>
</div>
<script>
$(document).ready(function () {
$('#async-annotation').click(function () {
$.get('/api/async-annotation', function (data) {
$('#result-async-annotation').text(data);
});
});
$('#completable-future').click(function () {
$.get('/api/completable-future', function (data) {
$('#result-completable-future').text(data);
});
});
$('#webflux').click(function () {
$.get('/api/webflux', function (data) {
$('#result-webflux').text(data);
});
});
$('#webasync').click(function () {
$.get('/api/webasync', function (data) {
$('#result-webasync').text(data);
});
});
$('#deferredresult').click(function () {
$.get('/api/deferredresult', function (data) {
$('#result-deferredresult').text(data);
});
});
});
</script>
</body>
</html>
總結(jié)
通過本文介紹的五種異步處理方式,包括 @Async 注解、CompletableFuture、WebFlux、WebAsyncTask 和 DeferredResult,我們可以有效地提升 Spring Boot 應(yīng)用的性能。每種方式都有其特定的適用場(chǎng)景和優(yōu)缺點(diǎn),開發(fā)者可以根據(jù)實(shí)際需求選擇合適的方式來優(yōu)化應(yīng)用的吞吐量和響應(yīng)速度。希望這些示例能夠幫助你更好地理解和應(yīng)用 Spring Boot 中的異步處理功能。