Spring Boot WebClient.Builder bean在传统servlet多线程应用中的使用

Spring Boot WebClient.Builder bean usage in traditional servlet multi threaded application

我想要一个 http 客户端来从 Spring 启动 非反应性 应用程序调用其他微服务。由于 RestTemplate 将被弃用,我尝试使用 WebClient.Builder 和 WebClient。虽然我不确定线程​​安全。这里的例子:

@Service
public class MyService{
    @Autowired
    WebClient.Builder webClientBuilder;

    public VenueDTO serviceMethod(){
        //!!! This is not thread safe !!!
        WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();

        VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                retrieve().bodyToMono(VenueDTO.class).
                blockOptional(Duration.ofMillis(1000)).
                orElseThrow(() -> new BadRequestException(venueNotFound));
                return VenueDTO;
    }
}

本例中的 serviceMethod() 将从几个线程中调用,而 webClientBuilder 是单个 bean 实例。 WebClient.Builder class 包含状态:baseUrl,这似乎不是线程安全的,因为很少有线程可以同时调用此状态更新。同时 WebClient 本身似乎是线程安全的,如

的回答中所述

我应该使用 WebClient.Builder 中提到的 bean https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html

Spring Boot creates and pre-configures a WebClient.Builder for you; it is strongly advised to inject it in your components and use it to create WebClient instances.

我看到的解决方法之一是创建 WebClient 而不将任何状态传递给构建器,而不是:

WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();

我会做:

WebClient webClient = webClientBuilder.build();

并在 uri 方法调用中传递带有协议和端口的完整 url:

webClient.get().uri("full url here", MyDTO.class)

在我的案例中,正确的使用方法是什么?

你说得对,WebClient.Builder 不是 thread-safe。

Spring Boot 正在创建 WebClient.Builder 作为原型 bean,因此您将为每个注入点获得一个新实例。就您而言,我认为您的组件有点奇怪。

它应该看起来像这样:

@Service
public class MyService{

    private final WebClient webClient;

    public MyService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8000").build();
    }

    public VenueDTO serviceMethod(){
        VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                retrieve().bodyToMono(VenueDTO.class).
                blockOptional(Duration.ofMillis(1000)).
                orElseThrow(() -> new BadRequestException(venueNotFound));
                return VenueDTO;
    }
}

现在我猜这是一个代码片段,您的应用程序可能有不同的约束。

如果您的应用程序需要经常更改基础 URL,那么我认为您应该停止在构建器上配置它并传递问题中提到的完整 URL。如果您的应用程序有其他需求(自定义 headers 用于身份验证等),那么您也可以在构建器或每个请求的基础上执行此操作。

一般来说,您应该尝试为每个组件构建一个 WebClient 实例,因为为每个请求重新创建实例非常浪费。

如果您的应用程序具有非常具体的约束并且确实需要创建不同的实例,那么您始终可以调用 webClientBuilder.clone() 并获取可以改变的构建器的新实例,而不会出现线程安全问题。