Home  >  Article  >  Java  >  Java 9’s HTTP2 protocol support and non-blocking HTTP API analysis

Java 9’s HTTP2 protocol support and non-blocking HTTP API analysis

王林
王林forward
2023-04-22 21:52:06977browse

1. Introduction to HTTP/2

HTTP/2 aims to alleviate the pain caused by maintaining the complex infrastructure of HTTP/1.1 and has good performance. Although HTTP/2 is still backwards compatible with HTTP/1.1, it is no longer a text-based protocol.

HTTP/2 multiplexing enables a single connection to handle multiple bidirectional streams, allowing clients to download multiple resources simultaneously over a single connection.

HTTP 1.x protocol is text-based, so the messages are very lengthy. Sometimes, the same set of HTTP headers are exchanged over and over again. HTTP/2 maintains HTTP Headers across requests, eliminating repeated exchange of data and greatly reducing the bandwidth required for data interaction.

HTTP/2 Data Push

You may think that HTTP/2’s server-side data push is some kind of continuation or upgrade to WebSockets, but this is not the case. While WebSockets are a method of full-duplex communication between a client and a server so that the server sends data to the client after a TCP connection is established, HTTP/2 offers a different solution.

HTTP/2 push is to actively send resources to the client without initiating resource requests from the client's perspective. This means that the server may know from a request what other resources the website will need further and can send them all together (in advance) long before the client requests them again.

Java HTTP client that currently supports HTTP/2

  • Jetty

  • Netty

  • OkHttp

  • ##Vert.x

  • Firefly

But in this article , we will not introduce these Java client software, but introduce the HTTP/2 support provided by Java9.

2. Java 9 HTTP/2 client

First use the Java 9 syntax to import the module.

jdk.incubator.httpclient

module com.springui.echo.client {
    requires jdk.incubator.httpclient;
}

Java 9’s new HTTP Cient API follows the builder pattern. HttpClient is the entry point used to operate HTTP requests. It is built first and then used.

HttpClient client = HttpClient
    .newBuilder()
    .version(Version.HTTP_2)  //支持HTTP2
    .build();

Sending requests in blocking mode

Once we have an HttpClient instance, we can use it to send HttpRequest. HttpRequest instances can also be created using the constructor.

HttpResponse<String> response = client.send(
    HttpRequest
        .newBuilder(TEST_URI)  //请求地址
        .POST(BodyProcessor.fromString("Hello world")) //POST报文数据
        .build(),
    BodyHandler.asString()  //请求响应数据处理,接收字符串
);

After the request is sent, the thread will block until the response data is obtained. This is the same as the HTTP API in JAVA 8 and before. However, Java 9 provides a method of asynchronous non-blocking sending and processing requests, which is more suitable for highly concurrent HTTP requests and processing.

Sending a request in non-blocking mode (Java 9)

In the following example, 10 random integers are sent asynchronously.

List<CompletableFuture<String>> responseFutures = 
        IntStream.of(1,2,3,4,5,6,7,8,9,10)  //10个整数形成IntStream,Java 8的语法
        .mapToObj(String::valueOf) //10个整数转换成字符串,Java 8的语法
        .map(message -> client.sendAsync( //将10个整数字符串作为内容,发送10个异步请求
                HttpRequest.newBuilder(TEST_URI)
                        .POST(HttpRequest.BodyProcessor.fromString(message))
                        .build(),
                HttpResponse.BodyHandler.asString()
             ).thenApply(HttpResponse::body)  //以CompletableFuture<HttpResponse.body()>作为流处理的返回值
        )
        .collect(Collectors.toList());  //将Stream转成List

The above example makes extensive use of Java 8’s Stream streaming API. If you are not familiar with it, you can read some articles I have written before.

The return value of the sendAsync method, CompletableFuture721c5bd4f0360fdcda122bb99928a389>, is further processed using thenApply(HttpResponse::body), and the final return value is CompletableFuturef7e83be87db5cd2d9a8a0b8117b38cd4.

CompletableFuture is the knowledge of Java asynchronous programming, which prints the results of concurrent asynchronous processing.

responseFutures.stream().forEach(future -> {
    LOGGER.info("Async response: " + future.getNow(null));
});

You will notice that the final print log may not be processed in the order 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 because all requests are asynchronous Sent out, the returned result is the result of CompletableFuture used for asynchronous processing.

3. Push-Promise Frames that support HTTP2

All the above examples can be completed under the HTTP/1.1 protocol, but a new non-blocking asynchronous API is added, and HTTP/ 2 features. Don't worry, the Java 9 Client API is most closely integrated with HTTP/2: you can use HTTP2 to send a request and get multiple asynchronous data results. (Some data is pushed in advance, of course, this requires the server to also support HTTP/2 for cooperation)

Map<HttpRequest,CompletableFuture<HttpResponse<String>>> responses =
        client.sendAsync(    //注意这里只发送一次请求
          HttpRequest.newBuilder(TEST_URI)
                  .POST(HttpRequest.BodyProcessor.fromString(TEST_MESSAGE))
                  .build(),
          HttpResponse.MultiProcessor.asMap(    //多个资源的响应结果
                  request -> Optional.of(HttpResponse.BodyHandler.asString())
          )
).join();
responses.forEach((request, responseFuture) -> {
  LOGGER.info("Async response: " + responseFuture.getNow(null));
});

From the perspective of Java 9, the new HTTP/2 client API looks good. However, the author feels that the current use of related technologies is not very mature yet. I think everyone can just give it a try for the time being.

The above is the detailed content of Java 9’s HTTP2 protocol support and non-blocking HTTP API analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete