Jetty HTTP/2 客户端接收服务器推送示例

6
Jetty 9.3 RC(链接)实现了支持服务器推送的Jetty HTTP/2客户端。但是,我没有找到任何相关文档或示例代码。请问是否有人可以提供一个接收来自此站点的推送资源的示例代码:https://nghttp2.org(启用了HTTP2服务器推送的公共服务器)。

---更新1--- 我已尝试测试此文件,如sbordet所说。然而,在执行此行后:

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文件中的<build>标签更改为以下内容:(明确使用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项目的主干分支并在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)
2个回答

3

问题中提到的链接为Jetty的HttpClient的HTTP/2传输。

Jetty的HttpClient向应用程序公开通用的HTTP API,其必须使用HTTP 1.0、1.1和2.0,并且因此HttpClient不公开任何API以接收HTTP/2推送资源,因为这些是仅限于HTTP/2的特殊机制。

如果您确实想与HTTP/2 API交互,可以使用Jetty的HTTP2Client,它向应用程序公开更低级别的HTTP/2特定API。

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


你必须将ALPN引导jar放入bootclasspath中,如此处所指定的那样。你做到了吗?你还必须使用JDK 8。如果你已经这样做了,你试图连接哪个服务器? - sbordet
是的,我正在使用JDK 8,并且现在已经添加了ALPN引导jar,但仍然出现错误。我试图连接https://webtide.com和https://nghttp2.org。请检查我的更新2。 - Hải Phong
你是否正在使用Jetty主分支的最新代码? - sbordet
如果我真正使用主分支,那意味着拉取所有代码并在http2-client文件夹中创建一个项目,那么它适用于公共服务器,但不适用于我的自签名证书http2服务器(参见更新3)。 - Hải Phong
自签名证书不适用于HTTP/2,因为它们不符合HTTP/2所需的安全要求,所以很难使其工作,除非您完全覆盖服务器行为并允许任何证书和任何密码。如果您这样做,在客户端上,您需要使用自签名证书配置密钥库。 - sbordet
显示剩余4条评论

1

UPDATE 3的答案:

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

SslContextFactory sslContextFactory = new SslContextFactory(true);

整个代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接