1

我试图在 Tomcat 上运行 Webflux 并尝试使用 Apache Http 客户端创建 Sping WebClient。

参考文档指出有内置支持: https ://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-client-builder-http-components

private ClientHttpConnector getApacheHttpClient(){
    HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
    clientBuilder.setDefaultRequestConfig(RequestConfig.DEFAULT);
    CloseableHttpAsyncClient client = clientBuilder.build();
    ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);
    return connector;
}

但是 Springs HttpComponentsClientHttpConnector 不接受 org.apache.http.impl.nio.client.CloseableHttpAsyncClient。它需要 org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient。所以似乎有一个包重命名,我找不到具有所需类的 Maven 依赖项。有谁知道该类的正确 Maven 依赖关系。或者我怎样才能让它工作?

4

1 回答 1

1

Apache HTTP Client 5 是一个单独的工件。您需要将以下依赖项添加到您的pom.xml:

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.core5</groupId>
    <artifactId>httpcore5-reactive</artifactId>
    <version>5.1</version>
</dependency>
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.springframework.http.client.reactive.HttpComponentsClientHttpConnector;

public class ApacheHttp {
    public static void main(String[] args) {
        new HttpComponentsClientHttpConnector(HttpAsyncClients.custom().build())
    }
}
于 2021-05-19T12:06:25.847 回答