REST API In HTTP/2
HTTP/2 has a binary framing layer. Its backward compatibility with HTTP/1.x clients and servers.
New binary framing :
HTTP/1.x : Uses newline delimited text to transfer the data. It is more prone to error compared to binary.
HTTP/2 : It uses binary format. In the transfer layer.
2. Multiplexing: HTTP/1.x was fundamentally created over the principle that only 1 response can be delivered at a time. This resulted into response queueing and blocking, slowing down the TCP connection. This meant until now, the client had to guess the correct order of responses it needs to fetch to be able to process things quickly.
3. Header Compression: HTTP/2 uses the HPACK compression technique to compress header data across requests and responses.
In today’s average requests, we can see headers crossing several kilobytes with cookies, and header compression greatly improves efficiency.
4. Server Push: In HTTP/1.x, a server can communicate to the client only when a request is made to it, and it can communicate only by responding to that particular request. In HTTP/2 however, a server can send multiple responses to a single request. Thus, in addition to responding, the server can push other useful information to the client.
For example, while loading a webpage, the server can start sending the javascript files and stylesheets right away, without waiting for the client to ask for them. It may also be used to activity update the client’s cache for faster rendering.
How to move to HTTP/2?
No real changes need to be made to the application layer to transition to HTTP/2, as the changes are in the encoding and information transfer mechanisms.
If you are using cURL , just check the current version with
curl --versionYou will be responded with a list of protocols : dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp . If you do not see http2 included there, you can simply update your cURL version.
Mac users can simply do
brew reinstall curl --with-openssl --with-nghttp2Tomcat version 8.5.x . All we need to do is add the HTTP 2 upgrade protocol to Tomcat’s connector. We can do that by customizing the embedded Tomcat container:
@Bean
public EmbeddedServletContainerCustomizer tomcatCustomizer() {
return (container) -> {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
((TomcatEmbeddedServletContainerFactory) container)
.addConnectorCustomizers((connector) -> {
connector.addUpgradeProtocol(new Http2Protocol());
});
}
};
}
You need to add the HTTP 2 upgrade protocol to Tomcat's connector. You can do that by customizing the embedded Tomcat container:
Java 8:
@Bean
public EmbeddedServletContainerCustomizer tomcatCustomizer() {
return (container) -> {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
((TomcatEmbeddedServletContainerFactory) container)
.addConnectorCustomizers((connector) -> {
connector.addUpgradeProtocol(new Http2Protocol());
});
}
};
}
Java 7:
@Bean
public EmbeddedServletContainerCustomizer tomcatCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
((TomcatEmbeddedServletContainerFactory) container)
.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.addUpgradeProtocol(new Http2Protocol());
}
});
}
}
};
}



Comments
Post a Comment