【问题标题】:Jetty HTTP/2 client receive server push exampleJetty HTTP/2 客户端接收服务器推送示例
【发布时间】:2015-07-12 15:38:09
【问题描述】:

Jetty 支持服务器推送的 HTTP/2 客户端已在 Jetty 9.3 RC (Link) 中实现。但是,我还没有找到与此相关的任何文档或示例代码。任何人都可以提供一个示例代码,例如从该站点接收推送的资源:@​​987654322@(具有enabled http2 server push的公共服务器)

---更新 1--- 正如 sbordet 所说,我试图测试this file。但是,执行此行后

mvn compile exec:java

我遇到了这个错误

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ http2client ---
2015-05-05 01:52:47.808:INFO::com.example.Client.main(): Logging initialized @3096ms
[WARNING]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.concurrent.TimeoutException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130)
    at com.example.Client.main(Client.java:55)
    ... 6 more

这是我的pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>http2client</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>http2client</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty.http2</groupId>
        <artifactId>http2-client</artifactId>
        <version>9.3.0.M2</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty.alpn</groupId>
        <artifactId>alpn-boot</artifactId>
        <version>8.1.3.v20150130</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.example.Client</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

这是我的项目目录

|-- pom.xml
|-- src
|   `-- main
|       `-- java
|           `-- com
|               `-- example
|                   `-- Client.java
`-- target
    |-- classes
    |   `-- com
    |       `-- example
    |           |-- Client$1.class
    |           `-- Client.class
    `-- maven-status
        `-- maven-compiler-plugin
            `-- compile
                `-- default-compile
                    |-- createdFiles.lst
                    `-- inputFiles.lst

---更新 2---

将我的pom.xml&lt;build&gt; 标记更改为:(显式使用JDK 8 并添加-Xbootclasspath 以指向Jetty 提供的alpn-boot.jar)。我正在使用 Java 8 更新 31

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-Xbootclasspath/p:/path/to/alpn-boot-8.1.3.v20150130.jar</argument>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>com.example.Client</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

执行此命令后:

mvn clean compile exec:exec

我在尝试连接到https://webtide.com/(Client.java 文件中的默认主机)时收到此错误

[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ http2client ---
2015-05-05 13:19:25.499:INFO::main: Logging initialized @153ms
Exception in thread "main" java.util.concurrent.TimeoutException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:130)
    at com.example.Client.main(Client.java:55)

连接https://nghttp2.org/时出现这个错误

[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ http2client ---
2015-05-05 13:29:12.106:INFO::main: Logging initialized @196ms
Exception in thread "main" java.util.concurrent.TimeoutException
    at java.util.concurrent.Phaser.awaitAdvanceInterruptibly(Phaser.java:800)
    at com.example.Client.main(Client.java:90)

---更新 3---

采取不同的方法:当我拉出整个码头项目的所有主分支,然后在 jetty.project/jetty-http2/http2-client 创建一个 Intellij 项目时,它适用于公共服务器 https://webtide.comhttps://nghttp2.org 。但是当我在我的自签名证书 http2 服务器上测试它时(使用 nghttp2 + nginx,驻留在我的虚拟机中)然后我得到这个错误

2015-05-05 19:05:25.094:INFO::main: Logging initialized @220ms
Exception in thread "main" java.util.concurrent.ExecutionException: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:138)
    at org.eclipse.jetty.http2.client.Client.main(Client.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.http2.HTTP2Flusher.append(HTTP2Flusher.java:110)
    at org.eclipse.jetty.http2.HTTP2Session.frame(HTTP2Session.java:577)
    at org.eclipse.jetty.http2.HTTP2Session.frames(HTTP2Session.java:559)
    at org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory$HTTP2ClientConnection.onOpen(HTTP2ClientConnectionFactory.java:121)

【问题讨论】:

    标签: jetty server-push http2 nghttp2


    【解决方案1】:

    问题中报告的链接为 Jetty 的 HttpClient 提供了 HTTP/2 传输

    Jetty 的 HttpClient 向必须使用 HTTP 1.0、1.1 和 2.0 的应用程序公开了一个通用 HTTP API,因此HttpClient 没有公开任何 API 来接收 HTTP/2 推送资源,因为这些是一种特殊的机制仅限 HTTP/2。

    如果您真的想与 HTTP/2 API 进行交互,可以使用 Jetty 的 HTTP2Client,它向应用程序公开了特定于 HTTP/2 的较低级别的 API。

    您可以找到连接到推送资源的网站的完整示例(在本例中为 https://webtide.comhere

    【讨论】:

    • 谢谢您,我已经尝试了该文件,但一直出错。请查看我的更新答案
    • 您必须将 ALPN 引导 jar 放入引导类路径中,指定为 here。你这样做了吗?您还必须使用 JDK 8。如果使用了,您要访问哪个服务器?
    • 是的,我使用的是 JDK 8,现在我添加了 ALPN 启动 jar,但仍然出现错误。我试图连接到webtide.comnghttp2.org 请检查我的更新2。
    • 你使用的是 Jetty master 分支的最新代码吗?
    • 我只是使用了9.3.0.M2版本的maven依赖,将master分支中的Client.java复制到我的项目中(改包为package com.example后加上import org.eclipse.jetty.http2.client.*就是这样正确与否?
    【解决方案2】:

    更新 3 的答案:

    使用 SslContextFactory 的另一个构造函数,它有一个布尔参数 trustAll 证书:

    SslContextFactory sslContextFactory = new SslContextFactory(true);
    

    整个代码:

    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    client.addBean(sslContextFactory);
    client.start();
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 2020-08-22
      • 2021-12-04
      • 2010-10-03
      • 1970-01-01
      • 2020-06-26
      • 2016-04-20
      • 1970-01-01
      相关资源
      最近更新 更多