SpringBoot2.0之Servlet容器變成Undertow
前言
預設情況下,Spring Boot 使用 Tomcat 來作為內嵌的 Servlet 容器
可以將 Web 伺服器切換到 Undertow 來提高應用效能。Undertow 是一個採用 Java 開發的靈活的高效能 Web 伺服器,提供包括阻塞和基於 NIO 的非堵塞機制。Undertow 是紅帽公司的開源產品,是 Wildfly 預設的 Web 伺服器。
一、測試吞吐量工具
apache-jmeter-5.3.zip
二、檔案目錄
三、檔案內容
1.AppMyBatis04.java
package com.zhongguancun;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.zhongguancun.mapper")
@SpringBootApplication
public class AppMyBatis04 {
public static void main(String[] args) {
SpringApplication.run(AppMyBatis04.class, args);
}
}
2.IndexController.java
package com.zhongguancun.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/index")
public String indexDev() {
//開啟次註釋可以後臺看到訪問痕跡
// System.out.println("index + 1");
return "index";
}
}
3.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongguancun</groupId>
<artifactId>zhongguancun_springboot_mybatis_pageHelper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot-web元件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 依賴資訊裡移除 Tomcat 配置 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- springboot 整合 pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
四、效果測試
執行專案
測試工具配置:
1.Undertow吞吐量
吞吐量8000多
2.Servlet吞吐量
停掉專案 修改pom.xml檔案
註釋掉undertow
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongguancun</groupId>
<artifactId>zhongguancun_springboot_mybatis_pageHelper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot-web元件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 依賴資訊裡移除 Tomcat 配置 -->
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<!-- 引入undertow -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency> -->
<!-- springboot 整合 pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
執行專案
然後執行測試
Servlet吞吐量5000多
總結
伺服器名稱 | 第一次執行 | 第二次執行 | 第三次執行 | 平均值 |
---|---|---|---|---|
Tomcat | 4773 | 5194 | 5334.7 | 5100 |
Undertow | 6666 | 6373 | 6451 | 6496 |
相關文章
- Spring Boot 內嵌容器 Tomcat / UndertowSpring BootTomcat
- JAVA網路程式設計基本功之Servlet與Servlet容器Java程式設計Servlet
- 為什麼要有 Servlet ,什麼是 Servlet 容器,什麼是 Web 容器?ServletWeb
- springboot2.0之安全Spring Boot
- Spring 容器與 Servlet互動SpringServlet
- SpringBoot 中的 Servlet Web 容器Spring BootServletWeb
- Java之ServletJavaServlet
- 作為servlet容器的hi-nginx-javaServletNginxJava
- SpringBoot2.0應用(四):SpringBoot2.0之spring-data-jpaSpring Boot
- Servlet/JSP 容器開發手記2 (轉)ServletJS
- Servlet 3.0 之 FilteringServletFilter
- 【SpringBoot2.0系列09】SpringBoot之rabbiSpring Boot
- Java中變數之區域性變數、本類成員變數、父類成員變數的訪問方法Java變數
- 反向Ajax之非同步Servlet非同步Servlet
- 十二、Spring Boot 嵌入式 Servlet 容器啟動原理Spring BootServlet
- SpringBoot2.0應用(五):SpringBoot2.0整合MyBatisSpring BootMyBatis
- SpringBoot2.0應用(二):SpringBoot2.0整合ActiveMQSpring BootMQ
- SpringBoot2.0應用(三):SpringBoot2.0整合RabbitMQSpring BootMQ
- laravel之容器Laravel
- Java之容器Java
- java EE開發之Servlet第一課:servlet的建立方式JavaServlet
- java EE開發之Servlet第二課:Servlet的三個hashMapJavaServletHashMap
- 「Golang成長之路」內建容器Golang
- Java之private關鍵字修飾成員變數Java變數
- 蛻變成蝶:Linux裝置驅動之DMALinux
- SpringBoot2.0應用(一):SpringBoot2.0簡單介紹Spring Boot
- 初識Javaweb之Servlet以及TomcatJavaWebServletTomcat
- java基礎學習:JavaWeb之ServletJavaWebServlet
- JavaWeb之Servlet獲取表單值JavaWebServlet
- 如何檢視Docker容器環境變數,如何向容器傳遞環境變數Docker變數
- 成員變數變數
- @Resource從spring容器中獲取例項注入到類的成員變數中Spring變數
- kubernetes容器編排之定義環境變數以及通過downwardapi把pod資訊作為環境變數傳入容器內變數API
- 「Golang成長之路」內建容器篇Golang
- springboot2.0整合webserviceSpring BootWeb
- springboot2.0整合rabbitmqSpring BootMQ
- JavaWeb三大元件之Servlet詳解JavaWeb元件Servlet
- Java Web之Servlet獲取表單值JavaWebServlet