HTTP/2 Java 8, Jetty and ALPN

2019-04-13 15:31发布

问题:

I went thru this page: https://www.eclipse.org/jetty/documentation/9.3.x/alpn-chapter.html to have an ALPN boot jar in my classpath and still I can't get it working.

I am confused as to know if I need an Open SDK Java 8 than Oracle Java 8.

My Java version is:

java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

And I'm using the following versions of Jetty and ALPN boot:

<jetty-version>9.4.0.M1</jetty-version>
<alpn-version>8.1.9.v20160720</alpn-version>    
<dependency>
        <groupId>org.mortbay.jetty.alpn</groupId>
        <artifactId>alpn-boot</artifactId>
        <version>${alpn-version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-client</artifactId>
    <version>${jetty-version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.http2</groupId>
    <artifactId>http2-client</artifactId>
    <version>${jetty-version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.http2</groupId>
    <artifactId>http2-http-client-transport</artifactId>
    <version>${jetty-version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.http2</groupId>
    <artifactId>http2-common</artifactId>
    <version>${jetty-version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty.http2</groupId>
    <artifactId>http2-hpack</artifactId>
    <version>${jetty-version}</version>
</dependency>

Also I tried using different versions of Jetty and ALPN which I found here https://mvnrepository.com/artifact/org.mortbay.jetty.alpn/alpn-boot and here https://mvnrepository.com/search?q=org.eclipse.jetty

Whichever ways I tried I never got it working submitting a POST request to an HTTP/2 endpoint.

However with Netty and the following dependency my tests were successful:

<dependency>
           <groupId>io.netty</groupId>
           <artifactId>netty-tcnative-boringssl-static</artifactId>
           <version>1.1.33.Fork22</version>
       </dependency>

Where do I find the documentation to see which version of ALPN boot is compatible with Oracle JDK? Is ALPN boot only compatible with OpenJDK?

I read that starting Java 9 support for ALPN will be native.

On a side note, which one is better? Netty or Jetty for HTTP/2 calls.

回答1:

Jetty's ALPN boot jar works with both OpenJDK and Oracle's JDK (which is based on OpenJDK).

Jetty's ALPN boot jar must be in the boot classpath, not the regular classpath, like the documentation you linked says.

As such, you must not declare it as a dependency in your pom.xml files (there is no need to, like there is no need for you to specify a dependency on the JDK classes).

JDK 9 will have ALPN support native, there is already some work in that direction.



回答2:

Netty is preferred for several reasons for making Http/2 connections. Few advantages I have seen using it are :

  • No need for the ALPN jar to be added to the boot classpath. Adding maven dependency "netty-tcnative-boringssl-static" does the job
  • It inherently support a asynchronous model of using the API. Hence it makes it simpler to handle data pushed by the server in case of HTTP/2