Simultaneous Spring WebClient Calls – 同时进行的Spring WebClient调用

最后修改: 2019年 10月 12日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Typically when making HTTP requests in our applications, we execute these calls sequentially. However, there are occasions when we might want to perform these requests simultaneously.

通常,在我们的应用程序中进行HTTP请求时,我们会按顺序执行这些调用。然而,在某些情况下,我们可能想同时执行这些请求。

For example, we may want to do this when retrieving data from multiple sources or when we simply want to try giving our application a performance boost.

例如,当我们从多个来源检索数据时,或者当我们只是想尝试给我们的应用程序一个性能提升时,我们可能想这样做。

In this quick tutorial, we’ll take a look at several approaches to see how we can accomplish this by making parallel service calls using the Spring reactive WebClient.

在这个快速教程中,我们将看一下几种方法,看看我们如何通过 使用Spring反应式WebClient来完成这个任务。

2. Recap on Reactive Programming

2.反应式编程的回顾

To quickly recap WebClient was introduced in Spring 5 and is included as part of the Spring Web Reactive module. It provides a reactive, non-blocking interface for sending HTTP requests.

快速回顾一下,WebClient是在Spring 5中引入的,作为Spring Web Reactive模块的一部分。它为发送HTTP请求提供了一个反应式、非阻塞的接口

For an in-depth guide to reactive programming with WebFlux, check out our excellent Guide to Spring 5 WebFlux.

有关使用 WebFlux 进行反应式编程的深入指南,请查看我们优秀的Guide to Spring 5 WebFlux

3. A Simple User Service

3.一个简单的用户服务

We’re going to be using a simple User API in our examples. This API has a GET method that exposes one method getUser for retrieving a user using the id as a parameter.

我们将在我们的例子中使用一个简单的User API。这个API有一个GET方法,暴露了一个方法getUser,用于使用id作为参数检索一个用户

Let’s take a look at how to make a single call to retrieve a user for a given id:

让我们来看看如何进行一个单一的调用来检索一个给定id的用户。

WebClient webClient = WebClient.create("http://localhost:8080");
public Mono<User> getUser(int id) {
    LOG.info(String.format("Calling getUser(%d)", id));

    return webClient.get()
        .uri("/user/{id}", id)
        .retrieve()
        .bodyToMono(User.class);
}

In the next section, we’ll learn how we can call this method concurrently.

在下一节中,我们将学习如何并发地调用这个方法。

4. Making Simultaneous WebClient Calls

4.同时进行WebClient调用

In this section, we’re going see several examples for calling our getUser method concurrently. We’ll also take a look at both publisher implementations Flux and Mono in the examples as well.

在本节中,我们将看到几个并发调用getUser方法的例子。我们还将在示例中看看两个发布者的实现FluxMono

4.1. Multiple Calls to the Same Service

4.1.对同一服务的多次呼叫

Let’s now imagine that we want to fetch data about five users simultaneously and return the result as a list of users:

现在让我们设想一下,我们想同时获取五个用户的数据,并将结果作为一个用户列表返回

public Flux fetchUsers(List userIds) {
    return Flux.fromIterable(userIds)
        .flatMap(this::getUser);
}

Let’s decompose the steps to understand what we’ve done:

让我们分解一下步骤,以了解我们做了什么。

We begin by creating a Flux from our list of userIds using the static fromIterable method.

我们首先使用静态fromIterable方法从我们的userIds列表中创建一个Flux。

Next, we invoke flatMap to run the getUser method we created previously. This reactive operator has a concurrency level of 256 by default, meaning it executes at most 256 getUser calls simultaneously. This number is configurable via method parameter using an overloaded version of flatMap.

接下来,我们调用flatMap来运行我们之前创建的getUser方法。这个反应式运算符的默认并发级别为256,意味着它最多同时执行256个getUser调用。这个数字可以通过使用flatMap的重载版本的方法参数进行配置。

It’s worth noting, that since operations are happening in parallel, we don’t know the resulting order. If we need to maintain the input order, we can use flatMapSequential operator instead.

值得注意的是,由于操作是平行进行的,我们不知道结果的顺序。如果我们需要保持输入顺序,我们可以使用flatMapSequential操作符代替。

As Spring WebClient uses a non-blocking HTTP client under the hood, there is no need to define any Scheduler by the user. WebClient takes care of scheduling calls and publishing their results on appropriate threads internally, without blocking.

由于Spring WebClient使用了一个无阻塞的HTTP客户端,因此用户不需要定义任何调度器。WebClient负责调度调用,并在内部适当的线程上发布其结果,没有阻塞。

4.2. Multiple Calls to Different Services Returning the Same Type

4.2.对不同服务的多次调用返回相同的类型

Let’s now take a look at how we can call multiple services simultaneously.

现在让我们来看看我们如何同时调用多个服务

In this example, we’re going to create another endpoint which returns the same User type:

在这个例子中,我们将创建另一个端点,它返回相同的User类型。

public Mono<User> getOtherUser(int id) {
    return webClient.get()
        .uri("/otheruser/{id}", id)
        .retrieve()
        .bodyToMono(User.class);
}

Now, the method to perform two or more calls in parallel becomes:

现在,并行执行两个或更多调用的方法变成了。

public Flux fetchUserAndOtherUser(int id) {
    return Flux.merge(getUser(id), getOtherUser(id));
}

The main difference in this example is that we’ve used the static method merge instead of the fromIterable method. Using the merge method, we can combine two or more Fluxes into one result.

本例的主要区别在于我们使用了静态方法merge而不是fromIterable方法。使用merge方法,我们可以将两个或多个Fluxes合并成一个结果。

4.3. Multiple Calls to Different Services Different Types

4.3.对不同服务的多次呼叫 不同类型

The probability of having two services returning the same thing is rather low. More typically we’ll have another service providing a different response type and our goal is to merge two (or more) responses.

两个服务返回相同内容的概率相当低。更典型的是,我们会有另一个服务提供不同的响应类型,我们的目标是合并两个(或多个)响应

The Mono class provides the static zip method which lets us combine two or more results:

Mono类提供了静态的zip方法,让我们可以结合两个或更多的结果。

public Mono fetchUserAndItem(int userId, int itemId) {
    Mono user = getUser(userId);
    Mono item = getItem(itemId);

    return Mono.zip(user, item, UserWithItem::new);
}

The zip method combines the given user and item Monos into a new Mono with the type UserWithItem. This is a simple POJO object which wraps a user and item.

zip方法将给定的useritem Monos组合成一个新的Mono,类型为UserWithItem。这是一个简单的POJO对象,它封装了一个用户和项目。

5. Testing

5.测试

In this section, we’re going to see how we can test the code we’ve already seen and, in particular, verify that service calls are happening in parallel.

在本节中,我们将看到如何测试我们已经看到的代码,特别是验证服务调用是否是并行的。

For this, we’re going to use Wiremock to create a mock server and we’ll test the fetchUsers method:

为此,我们将使用Wiremock来创建一个模拟服务器,我们将测试fetchUsers方法。

@Test
public void givenClient_whenFetchingUsers_thenExecutionTimeIsLessThanDouble() {
        
    int requestsNumber = 5;
    int singleRequestTime = 1000;

    for (int i = 1; i <= requestsNumber; i++) {
        stubFor(get(urlEqualTo("/user/" + i)).willReturn(aResponse().withFixedDelay(singleRequestTime)
            .withStatus(200)
            .withHeader("Content-Type", "application/json")
            .withBody(String.format("{ \"id\": %d }", i))));
    }

    List<Integer> userIds = IntStream.rangeClosed(1, requestsNumber)
        .boxed()
        .collect(Collectors.toList());

    Client client = new Client("http://localhost:8089");

    long start = System.currentTimeMillis();
    List<User> users = client.fetchUsers(userIds).collectList().block();
    long end = System.currentTimeMillis();

    long totalExecutionTime = end - start;

    assertEquals("Unexpected number of users", requestsNumber, users.size());
    assertTrue("Execution time is too big", 2 * singleRequestTime > totalExecutionTime);
}

In this example, the approach we’ve taken is to mock the user service and make it respond to any request in one second. Now if we make five calls using our WebClient we can assume that it shouldn’t take more than two seconds as the calls happen concurrently.

在这个例子中,我们采取的方法是模拟用户服务,让它在一秒钟内响应任何请求。现在,如果我们使用我们的WebClient进行五次调用,我们可以假设它不应该超过两秒,因为这些调用是同时发生的

To learn about other techniques for testing WebClient check out our guide to Mocking a WebClient in Spring.

要了解测试WebClient的其他技术,请查看我们的Mocking a WebClient in Spring指南。

6. Conclusion

6.结语

In this tutorial, we’ve explored a few ways we can make HTTP service calls simultaneously using the Spring 5 Reactive WebClient.

在本教程中,我们探讨了使用Spring 5 Reactive WebClient同时进行HTTP服务调用的几种方法。

First, we showed how to make calls in parallel to the same service. Later, we saw an example of how to call two services returning different types. Then, we showed how we can test this code using a mock server.

首先,我们展示了如何对同一服务进行并行调用。后来,我们看到了一个例子,说明如何调用两个返回不同类型的服务。然后,我们展示了如何使用一个模拟服务器来测试这段代码。

As always, the source code for this article is available over on GitHub.

一如既往,本文的源代码可在GitHub上获得