0

我正在尝试创建一个 httpclient 5 示例,该示例使用 asynchttpclient(异步多路复用)加载 URL 我已将 tomcat 9 配置为使用 conf/server.xml 配置接受 http2 协议,如下所示

<Connector
       protocol="org.apache.coyote.http11.Http11NioProtocol"
       port="8443" maxThreads="200"
       scheme="https" secure="true" SSLEnabled="true"
       keystoreFile="conf/xxx.keystore" keystorePass="xxx#"
       clientAuth="false" sslProtocol="TLS"><UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"/></Connector>

我的程序如下,

    final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy()).build();
    final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(new H2TlsStrategy(sslContext, NoopHostnameVerifier.INSTANCE)).build();
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.FORCE_HTTP_2, H2Config.DEFAULT, null, ioReactorConfig, connectionManager);


    client.start();

    final HttpHost target = new HttpHost("localhost", 8084, "https");
    System.out.println(target.getPort());
    final Future<AsyncClientEndpoint> leaseFuture = client.lease(target, null);
    final AsyncClientEndpoint endpoint = leaseFuture.get(45, TimeUnit.SECONDS);
    try {
        final String[] requestUris = new String[] {"/index.html"};

        final CountDownLatch latch = new CountDownLatch(requestUris.length);
        for (final String requestUri: requestUris) {
            final SimpleHttpRequest request = SimpleHttpRequest.get(target, requestUri);

            endpoint.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {
                        @Override
                        public void completed(final SimpleHttpResponse response) {
                            latch.countDown();
                            System.out.println(requestUri + "->" + response.getCode());
                            System.out.println(response.getBody());
                        }

                        @Override
                        public void failed(final Exception ex) {
                            latch.countDown();
                            System.out.println(requestUri + "->" + ex);
                            ex.printStackTrace();
                        }

                        @Override
                        public void cancelled() {
                            latch.countDown();
                            System.out.println(requestUri + " cancelled");
                        }

                    });
        }
        latch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        endpoint.releaseAndReuse();
    }

    client.shutdown(ShutdownType.GRACEFUL);

tomcat 配置在使用 h2 协议在 borwser 中加载页面时工作,但它在 httpclient 5 中失败,并出现以下异常

/index.html->org.apache.hc.core5.http.ConnectionClosedException: Connection closed
org.apache.hc.core5.http.ConnectionClosedException: Connection closed
    at org.apache.hc.core5.http2.impl.nio.FrameInputBuffer.read(FrameInputBuffer.java:146)
    at org.apache.hc.core5.http2.impl.nio.AbstractHttp2StreamMultiplexer.onInput(AbstractHttp2StreamMultiplexer.java:415)
    at org.apache.hc.core5.http2.impl.nio.AbstractHttp2IOEventHandler.inputReady(AbstractHttp2IOEventHandler.java:63)
    at org.apache.hc.core5.http2.impl.nio.ClientHttp2IOEventHandler.inputReady(ClientHttp2IOEventHandler.java:38)
    at org.apache.hc.core5.reactor.InternalDataChannel.onIOEvent(InternalDataChannel.java:117)
    at org.apache.hc.core5.reactor.InternalChannel.handleIOEvent(InternalChannel.java:50)
    at org.apache.hc.core5.reactor.SingleCoreIOReactor.processEvents(SingleCoreIOReactor.java:173)
    at org.apache.hc.core5.reactor.SingleCoreIOReactor.doExecute(SingleCoreIOReactor.java:123)
    at org.apache.hc.core5.reactor.AbstractSingleCoreIOReactor.execute(AbstractSingleCoreIOReactor.java:80)
    at org.apache.hc.core5.reactor.IOReactorWorker.run(IOReactorWorker.java:44)
    at java.lang.Thread.run(Thread.java:748)

对此的任何帮助将不胜感激

提前致谢

在启用 tomcat 日志时,我可以看到以下内容,

org.apache.coyote.AbstractProtocol$ConnectionHandler.process 无法为协商协议创建处理器 [h2c]

当我将客户端代码更新到 java 9 时,即使这个问题也得到了解决,但是有可能使它与 java 8 一起工作。

ps:我知道 java 8 不支持 ALPN,但如果有什么方法可以让它与 java 8 一起工作,请告诉我。

4

2 回答 2

0

我遇到了类似的问题,发现这是由于请求协议被用作 https。由于我已经为客户端设置了 TLS 策略,将协议更改为简单的“http”解决了这个问题。替换行:final HttpHost target = new HttpHost("localhost", 8084, "https"); by final HttpHost target = new HttpHost("localhost", 8084, "http");

于 2018-05-07T05:43:26.767 回答
0

通过使用 conscrypt 作为默认提供程序并使用自定义 TLSStrategy 我们可以使其与 jdk1.8 一起使用,请参阅,

https://stackoverflow.com/a/53399363/2236001

于 2018-11-22T12:18:33.263 回答