1. Introduction
1.介绍
The Server Push technology — part of HTTP/2 (RFC 7540) — allows us to send resources to the client proactively from the server-side. This is a major change from HTTP/1.X pull-based approach.
服务器推送技术–HTTP/2的一部分(RFC 7540)- 允许我们从服务器端主动向客户端发送资源。这是对HTTP/1.X基于拉动的方法的一个重大改变。
One of the new features that Spring 5 brings – is the server push support that comes with Jakarta EE 8 Servlet 4.0 API. In this article, we’ll explore how to use server push and integrate it with Spring MVC controllers.
Spring 5带来的新功能之一–是Jakarta EE 8 Servlet 4.0 API中的服务器推送支持。在这篇文章中,我们将探讨如何使用服务器推送并将其与Spring MVC控制器集成。
2. Maven Dependency
2.Maven的依赖性
Let’s start by defining dependencies we’re going to use:
让我们从定义我们要使用的依赖性开始。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
The most recent versions of spring-mvc and servlet-api can be found on Maven Central.
spring-mvc和servlet-api的最新版本可以在Maven中心找到。
3. HTTP/2 Requirements
3.HTTP/2的要求
To use server push, we’ll need to run our application in a container that supports HTTP/2 and the Servlet 4.0 API. Configuration requirements of various containers can be found here, in the Spring wiki.
要使用服务器推送,我们需要在支持 HTTP/2 和 Servlet 4.0 API 的容器中运行我们的应用程序。各种容器的配置要求可在Spring wiki中找到。
Additionally, we’ll need HTTP/2 support on the client-side; of course, most current browsers do have this support.
此外,我们将需要在客户端支持HTTP/2;当然,大多数当前浏览器都有这种支持。
4. PushBuilder Features
4.PushBuilder功能
The PushBuilder interface is responsible for implementing server push. In Spring MVC, we can inject a PushBuilder as an argument of the methods annotated with @RequestMapping.
PushBuilder接口负责实现服务器推送。在Spring MVC中,我们可以注入一个PushBuilder作为@RequestMapping注释的方法的参数。
At this point, it’s important to consider that – if the client doesn’t have HTTP/2 support – the reference will be sent as null.
在这一点上,重要的是要考虑到–如果客户端没有HTTP/2支持–引用将被发送为null。
Here is the core API offered by the PushBuilder interface:
这里是由PushBuilder接口提供的核心API。
- path (String path) – indicates the resource that we’re going to send
- push() – sends the resource to the client
- addHeader (String name, String value) – indicates the header that we’ll use for the pushed resource
5. Quick Example
5.快速实例
To demonstrate the integration, we’ll create the demo.jsp page with one resource — logo.png:
为了演示集成,我们将用一个资源–logo.png创建demo.jsp页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PushBuilder demo</title>
</head>
<body>
<span>PushBuilder demo</span>
<br>
<img src="<c:url value="/resources/logo.png"/>" alt="Logo"
height="126" width="411">
<br>
<!--Content-->
</body>
</html>
We’ll also expose two endpoints with the PushController controller — one that uses server push and another that doesn’t:
我们还将用PushController控制器公开两个端点–一个使用服务器推送,另一个则不使用。
@Controller
public class PushController {
@GetMapping(path = "/demoWithPush")
public String demoWithPush(PushBuilder pushBuilder) {
if (null != pushBuilder) {
pushBuilder.path("resources/logo.png").push();
}
return "demo";
}
@GetMapping(path = "/demoWithoutPush")
public String demoWithoutPush() {
return "demo";
}
}
Using the Chrome Development tools, we can see the differences by calling both endpoints.
使用Chrome开发工具,我们可以通过调用这两个端点看到差异。
When we call the demoWithoutPush method, the view and the resource is published and consumed by the client using the pull technology:
当我们调用demoWithoutPush方法时,视图和资源被发布并由客户端使用拉动技术消费。
When we call the demoWithPush method, we can see the use of the push server and how the resource is delivered in advance by the server, resulting in a lower load time:
当我们调用demoWithPush方法时,我们可以看到推送服务器的使用,以及资源如何被服务器提前交付,从而降低了加载时间。
The server push technology can improve the load time of the pages of our applications in many scenarios. That being said, we do need to consider that, although we decrease the latency, we can increase the bandwidth – depending on the number of resources we serve.
服务器推送技术可以在很多情况下改善我们应用程序页面的加载时间。也就是说,我们确实需要考虑,虽然我们减少了延迟,但我们会增加带宽–这取决于我们服务的资源数量。
It’s also a good idea to combine this technology with other strategies such as Caching, Resource Minification, and CDN, and to run performance tests on our application to determine the ideal endpoints for using server push.
将该技术与其他策略相结合也是一个好主意,如缓存、资源最小化和CDN,并对我们的应用程序进行性能测试以确定使用服务器推送的理想端点。
6. Conclusion
6.结论
In this quick tutorial, we saw an example of how to use the server push technology with Spring MVC using the PushBuilder interface, and we compared the load times when using it versus the standard pull technology.
在这个快速教程中,我们看到了一个如何使用PushBuilder接口在Spring MVC中使用服务器推送技术的例子,我们还比较了使用它与标准拉动技术时的加载时间。
As always, the source code is available over on GitHub.
像往常一样,源代码可在GitHub上获得。