Earlier articles have introduced various scenarios for configuring the HTTP/2 protocol in Spring boot 2.0, but they are all introduced.
h2protocol. There are two versions of the HTTP/2 protocol:h2withh2c,h2cYesh2The plain text version is not based on TLS and has no security. It is precisely because there is no TLS layer encryption and decryption related steps, it is more suitable for communication between backend services, gRPC is to support both versions of the HTTP/2 protocol.
An HTTP/2 connection is an application layer protocol built on top of a TCP connection, and the client is the initiator of a TCP connection.
HTTP/2 uses the same URI schemes as HTTP/1.1: "http" and "https", and still shares the same default port: http for 80, 443 for https. This means that the way "http" and "https" determine whether they support the HTTP/2 protocol is different.
inOfficial documentIn the definition, two versions are defined for the HTTP/2 protocol:h2 with h2c:
Since there are two versions in the official introduction of the HTTP/2 protocol, we havearticleIntroducing how Spring boot 2.0 is implementedh2, this article will introduce how to make Spring boot 2.0 supporth2cprotocol.
Since the h2c protocol is simpler than h2, it should not be difficult to implement, but from the official documentation of Spring boot 2.0, it clearly states that "Spring boot does not support h2c - the plain text version of the HTTP/2 protocol". So I would like other ways to support h2c.
First of all, the idea is to support h2c through the external tomcat configuration, because the server.xml configuration in tomcat8.5 has HTTP/2 protocol related configuration:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
This shows that you want to add a certificate. The obvious support is based on the h2 version of the HTTP/2 protocol on the TLS layer, but you can disable SSL from the configuration, so I tried it and it succeeded. Configured as an example below, it supports h2c:
<Connector port="5080" protocol="HTTP/1.1" connectionTimeout="20000">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
</Connector>
The tomcat container itself supports multiple connectors configuration. I want to configure multiple connectors at the same time in Spring boot 2.0. See the official documentation of Spring boot and find the following configuration:
The example in the documentation is to configure a https connector via java configure. It is not supported in application.yaml to configure multiple connectors.
Therefore, I will imitate the way to configure h2c for external tomcat. In the built-in tomcat of Spring boot 2.0, configure h2c protocol through java configure. The specific code is as follows:
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createH2cConnector());
return tomcat;
}
private Connector createH2cConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http2Protocol upgradeProtocol = new Http2Protocol();
connector.addUpgradeProtocol(upgradeProtocol);
//connector.setScheme("http");
connector.setPort(5080);
return connector;
}
At this point start our Spring boot application, we will find the last boot log, we start three ports at the same time, that is, three Connector:
Tomcat started on port(s): 8080 (http) 8443 (https) 5080 (http) with context path '/demo-h2c'
First you need to check if your curl tool supports the HTTP/2 protocol.
➜ ~ curl -V
curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy
From the Features information above, I found that my curl tool supports the HTTP/2 protocol.
Let's start by verifying that my Spring boot service is h2c on port 5080, as follows:
➜ ~ curl -v --http2 http://127.0.0.1:5080/demo-h2c/h2c/hello\?name\=guoyankui
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5080 (#0)
> GET /demo-h2c/h2c/hello?name=guoyankui HTTP/1.1
> Host: 127.0.0.1:5080
> User-Agent: curl/7.54.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
>
< HTTP/1.1 101
< Connection: Upgrade
< Upgrade: h2c
< Date: Sun, 06 May 2018 13:12:37 GMT
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< content-type: text/plain;charset=UTF-8
< date: Sun, 06 May 2018 13:12:37 GMT
<
* Connection #0 to host 127.0.0.1 left intact
Hello h2c: null%
OK, the verification passed.
In the previous section, we used the Curl tool to verify the h2c protocol for Spring boot 2.0 services, but in the Java language, we also need a Java client that supports h2c. At present, the general java client only supports h2, some do not support HTTP/2 protocol, and browsers generally do not support h2c.
Now it is not easy to find a java client that supports h2c. Currently find the latest version of okhttp3.3.10.0Does not support h2c, but found in the latest github code3.11.0-SNAPSHOTThe version is already supportedh2c, code example:
public static void main(String[] args) throws Exception {
testH2C();
}
public static void testH2C() throws Exception {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.protocols(Arrays.asList(Protocol.H2C))
.build();
Request request = new Request.Builder()
.url("http://127.0.0.1:5080/demo-h2c/h2c/hello?name=guoyankui")
.build();
Response response = okHttpClient.newCall(request).execute();
System.out.println(response.protocol());
System.out.println(response.body().string());
}
After executing this main function, the output is:
h2c
Hello h2c: guoyankui
From the output results, verifiedh2cThe client and server of the protocol.
Currently, I found okhttp33.11.0Version supporth2cThere is a Java client that supports h2c. We also implemented the h2c server in Spring boot 2.0, so we have support.h2cThe complete program of the agreement.
customizeDistributeExceptionHandlerNote, the annotation receives a parameterattachmentId。 The annotation is used in the method, using the annotation as the cut point to implement the unified processin...
First, what is Spring Boot Spring Boot Is a lightweight development framework to simplifySpring The development work of the app. Compared to beforexmlConfiguration method,s...
1. About taking over mvc is different from springboot1.+ version 2 has abandoned WebMervcConfigurerAdapter but uses the new interface standard WebMervcConfigurer, so I can't see it, so the method chan...
Introduction If spring security is set on the classpath, the web is safe by default. Springboot relies on spring's security claim policy to decide to usehttpBasicOrformLogin, add a security level meth...
Spring-boot 2.0 + WebSocket Why use WebSocket? In order to do something, the server can take the initiative to prompt the client fails result, but only the HTTP protocol is a client sends a request to...
Configure POM dependencies Check whether the spring-boot-starter-webflux dependency is configured in the project POM file. If it was automatically generated above, the configuration is as follows: The...
1. Spring Boot Overview This passage probably says: Spring Boot is the starting point of all Spring-based projects. Spring Boot is designed to let you run Spring applications as fast as possible and r...
Spring Boot 2.0 detailed 2018.2.22 Copyright statement: This article is the original article of the blogger chszs, and may not be reproduced without the permission of the blogger. Spring Boot 2.0 is a...
Reference documentation Official documentation http://spring.io/projects/spring-data-neo4j#learn https://docs.spring.io/spring-data/neo4j/docs/5.1.2.RELEASE/reference/html/ https://neo4j.com/docs/ htt...
How to make Spring Boot into war package The project written by SpringBoot embeds tomcat, so the jar package can be run directly. However, every time the jar package is started, a new tomcat is create...