SFA官方译文:使用 Spring Boot 2.1 介绍 Servlet 4.0 的服务器推送

Posted SpringForAll社区

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SFA官方译文:使用 Spring Boot 2.1 介绍 Servlet 4.0 的服务器推送相关的知识,希望对你有一定的参考价值。

原文链接:https://dzone.com/articles/introducing-servlet-40-server-push-using-spring-bo

译者:Darren Luo

想要了解有关新 Servlet 4.0 的更多信息?查看这篇文章,了解更多关于在 Spring Boot 2.1 中使用服务器推送的信息。

在本文,我们将讨论服务器推送技术,它实际上是 HTTP/2 规范的一部分。

因为 HTTP/2,Servlet 最重要的特性是服务器推送功能的实现。该技术背后的概念:如果客户端/浏览器请求某个资源,服务器预先假设其他关联资源也可能很快被请求。由于这个假设,它在它们实际需要之前将它们推入缓存(称为“缓存推送”)。例如,很可能在网页加载时,它最终可能请求 CSS 文件或其他图片。同时服务器主动开始推送这些资源的字节,不再需要客户端发出明确的请求。

Servlet 4.0 是 Java EE 8 的一部分,因此,它需要 Java 9+ 以及 Spring 5.x。Tomcat 支持 HTTP/2,但是它必须配置为使用 TLS。Tomcat 9 只能在 Spring Boot 2.1.0 中使用,但是它尚未发布,我们需要在本文中使用里程碑版本。

在 Spring Boot 中开启 TLS 支持只是 application.properties 文件中的几个属性的问题。只需使用以下代码来启用它:

 
   
   
 
  1. #enable/diable https

  2. server.ssl.enabled=true

  3. server.ssl.key-store: classpath:keystore.jks

  4. server.ssl.key-store-password: tomcatssl

  5. server.ssl.keyStoreType: JKS

  6. server.ssl.keyAlias: tomcatssl

  7. server.port=8443

假如李不知道如何生成 keystore.jks,清点击此链接。

要在 Tomcat 中启用 HTTP/2 支持,需要添加以下属性。

 
   
   
 
  1. server.http2.enabled=true

在使用 TLS 配置我们的服务器之后,我们很高兴公开我们的 endpoint,其由 HTTP/2 技术提供支持。

 
   
   
 
  1. @GetMapping(path = "/serviceWithPush")

  2. public String serviceWithPush(HttpServletRequest request,PushBuilder pushBuilder) {

  3.    if (null != pushBuilder) {

  4.        pushBuilder.path("resources/OnlineJavaPapers.png")

  5.            .push();

  6.    }

  7.    return "index";

  8. }

我们还配置另一个类似于上面的 endpoint,它主要使用传统的 pull 技术,并尝试在客户端浏览器上找出差异。

 
   
   
 
  1. @GetMapping(path = "/serviceWithoutPush")

  2. public String serviceWithoutPush() {

  3.    return "index";

  4. }

使用 Firefox dev tool,我们可以确认,对于 serviceWIthPush endpoint

,只有一个请求是从浏览器发起的:

【SFA官方译文】:使用 Spring Boot 2.1 介绍 Servlet 4.0 的服务器推送

然而,当我们调用 serviceWithoutPush,会触发两个请求。

总而言之,当使用服务器推送技术结合适当的缓存技术,我们可以大大增强页面加载时间和我们网站的整体响应能力。

可在这里找到示例代码。

下面是译者我自己补充的一点点内容:

可能有部分读者不知道怎么使用 Spring Boot 的里程碑版本,我在这里贴一个 pom.xml 文件供大家参考使用:

 
   
   
 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.    <modelVersion>4.0.0</modelVersion>

  5.    <groupId>com.example</groupId>

  6.    <artifactId>myproject</artifactId>

  7.    <version>0.0.1-SNAPSHOT</version>

  8.    <parent>

  9.        <groupId>org.springframework.boot</groupId>

  10.        <artifactId>spring-boot-starter-parent</artifactId>

  11.        <version>2.1.0.M4</version>

  12.    </parent>

  13.    <dependencies>

  14.        <dependency>

  15.            <groupId>org.springframework.boot</groupId>

  16.            <artifactId>spring-boot-starter-web</artifactId>

  17.        </dependency>

  18.    </dependencies>

  19.    <build>

  20.        <plugins>

  21.            <plugin>

  22.                <groupId>org.springframework.boot</groupId>

  23.                <artifactId>spring-boot-maven-plugin</artifactId>

  24.            </plugin>

  25.        </plugins>

  26.    </build>

  27.    <repositories>

  28.        <repository>

  29.            <id>spring-snapshots</id>

  30.            <url>https://repo.spring.io/snapshot</url>

  31.            <snapshots><enabled>true</enabled></snapshots>

  32.        </repository>

  33.        <repository>

  34.            <id>spring-milestones</id>

  35.            <url>https://repo.spring.io/milestone</url>

  36.        </repository>

  37.    </repositories>

  38.    <pluginRepositories>

  39.        <pluginRepository>

  40.            <id>spring-snapshots</id>

  41.            <url>https://repo.spring.io/snapshot</url>

  42.        </pluginRepository>

  43.        <pluginRepository>

  44.            <id>spring-milestones</id>

  45.            <url>https://repo.spring.io/milestone</url>

  46.        </pluginRepository>

  47.    </pluginRepositories>

  48. </project>

PS:图片在 google 的服务器上,可能看不到,该怎么做大家都知道的啦~


推荐: 

上一篇: 

关注公众号


以上是关于SFA官方译文:使用 Spring Boot 2.1 介绍 Servlet 4.0 的服务器推送的主要内容,如果未能解决你的问题,请参考以下文章

SFA官方翻译:使用Spring 5引导Web应用程序

SFA官方翻译Spring WebFlux和Spring Cloud进行响应式微服务开发

你的项目应当使用Spring Boot吗?(译文)

Spring Boot 2从入门到入坟 | 基础入门篇:你会看Spring Boot的官方文档吗?

Spring Boot 2从入门到入坟 | 基础入门篇:你会看Spring Boot的官方文档吗?

Spring boot 官方文档链接