简体   繁体   English

使用Grizzly的嵌入式Java服务器:如何启用http2

[英]Embedded Java server using Grizzly: How do you enable http2

So I am trying to create a REST API server using Grizzly / Jersey and it is working fine but I am not able to figure out how to enable http2. 所以我试图使用Grizzly / Jersey创建一个REST API服务器,它工作正常,但我无法弄清楚如何启用http2。 Documentation on this subject is all but non-existent. 关于这个主题的文档几乎不存在。 This is what I have: 这就是我所拥有的:

private static SSLContext getSSLContext(final String keystore) throws Exception {
    SSLContext sslContext = SSLContext.getInstance("TLSv1");
    final char[] passphrase = "changeit".toCharArray();
    final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    final KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream stream = Main.class.getResourceAsStream(keystore)) {
        ks.load(stream, passphrase);
    }
    kmf.init(ks, passphrase);
    sslContext.init(kmf.getKeyManagers(), null, null);
    return sslContext;
}

final ResourceConfig rc = new ResourceConfig().packages("path.to.package");
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);
final NetworkListener listener = server.getListeners().iterator().next();
listener.registerAddOn(new Http2AddOn());
listener.setSecure(true);
final SSLEngineConfigurator configurator =
    new SSLEngineConfigurator(
        getSSLContext("keystore.jks"),
        false,
        false,
        false
    );
listener.setSSLEngineConfig(configurator);

And, as far as I know, I have all the appropriate dependencies in my pom.xml file: 而且,据我所知,我的pom.xml文件中包含所有相应的依赖项:

<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http2</artifactId>
    <version>2.3.24</version>
</dependency>

<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-npn-bootstrap</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-npn-api</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>

When I test the server with curl -v --http2 ..., I get 当我用curl -v --http2测试服务器时,我明白了

  • ... ...
  • ALPN/NPN, server did not agree to a protocol ALPN / NPN,服务器不同意协议
  • SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 使用TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256进行SSL连接
  • ... ...

Does someone know what is missing ? 有人知道缺少什么吗?

I found what was the problem. 我发现了什么问题。 It is explained here: http://unrestful.io/2015/10/09/alpn-java.html 这里解释: http//unrestful.io/2015/10/09/alpn-java.html

Java 8 does not support ALPN. Java 8不支持ALPN。 grizzly-npm-bootstrap is there to override system classes to provide that support. grizzly-npm-bootstrap用于覆盖系统类以提供该支持。 As such, it should be added to Xbootclasspath like so: 因此,它应该添加到Xbootclasspath,如下所示:

java -Xbootclasspath/p:/path/to/grizzly-npn-bootstrap.jar

And since it overrides system classes, it needs to be adapted to new versions of the JRE. 由于它覆盖了系统类,因此需要适应新版本的JRE。 Since I am using Java 8 and grizzly-npn-bootstrap.jar is only available for OpenJDK 7, I am out of luck. 由于我使用的是Java 8,而且grizzly-npn-bootstrap.jar仅适用于OpenJDK 7,我运气不好。

One option is to switch to Jetty which provides alpn-boot for most (all ?) releases of OpenJDK. 一种选择是切换到Jetty,它为OpenJDK的大多数(所有?)版本提供alpn-boot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM