Column catalog

  1. Time to upgrade Java11 -01- JDK11 advantages and JDK options
  2. Time to upgrade Java11 -02- Upgrade JDK11
  3. Time to upgrade Java11 -03 VIRTUAL machine Jvm parameter Settings
  4. Http2 Clear Text (H2C)
  5. Time to update java11 – obstacles and issues with h2c communication in 05 microservices

preface

Are those of you who have read the previous articles trying to upgrade the Jdk to Java11? Today I’m going to talk about the internal upgrade of microservices to HTTP/2.0. The advantages of HTTP2 are probably well known, but I won’t go into too much detail. HTTP/2.0: Clear Text HTTP/2.0: Clear Text HTTP/2.0: Clear Text HTTP/2.0: Clear Text HTTP/2.0

HTTP / 2.0 Clear Text

HTTP/2.0 Cleartext, or H2C for short (you’ll use this abbreviation later). Use 101 change protocol negotiation to upgrade to HTTP2 protocol, using HTTP2 without encryption, very suitable for microservices internal requests. Enjoy the multiplexing of HTTP2, reduce the strain of TLS on the server CPU (encryption and decryption are expensive CPU), and the internal TLS of microservices also increase the maintenance cost of certificates.

upgradeh2c

The author uses Undertow for the Spring Boot container, and The Spring Cloud Gateway for the gateway. Today, we mainly explain the upgrade of these two components to H2C.

  1. Undertow upgrade to H2C Undertow upgrade to H2C

Support for HTTP upgrades to allow multiplexing of multiple protocols over HTTP ports.

/** * Undertow http2 H2C configuration **@author L.cm
 */
@Configuration
@ConditionalOnClass(Undertow.class)
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
public class UndertowHttp2Configuration {

	@Bean
	public ServletWebServerFactory servletWebServerFactory(a) {
		UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
		// Enable undertow http2
		factory.addBuilderCustomizers(builder -> builder.setServerOption(ENABLE_HTTP2, true));
		returnfactory; }}Copy the code
  1. The Spring Cloud Gateway is upgraded toh2c spring cloud gatewayServer is also supportedh2cThe configuration is easier, just open the configuration, the specific logic can see the source code below.
server:
  ssl:
    enabled: false
  http2:
    enabled: true
Copy the code

testh2c

The previous section described how to enable H2C on the server, but the successful enablement is not known until you test it. The easiest way to do this is to test it using java11’s HttpClient.

public static void main(String[] args) throws IOException, InterruptedException {
	HttpClient client = HttpClient.newBuilder().build();
	HttpRequest request = HttpRequest.newBuilder()
		.uri(URI.create("http://localhost:8081"))
		.build();
	HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
	System.out.println(response.version()); // HTTP_2
	System.out.println(response.body()); // test
}
Copy the code

After the speech

You can see how excited you are here. Of course, many things have not gone so smoothly. The next article will continue to help. Wait, something’s wrong, isn’t our column java11? How did it get to H2C? Don’t worry about the following article will be decrypted for everyone, like our article friends remember to pay attention to us.

The statement

This series of articles is compiled and written by The author of micA, Ru Meng Technology. If there is any reference or reprint, please keep the original author and indicate the source.