好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

关于springboot-starter-undertow和tomcat的区别说明

什么是tomcat

在说undertow和tomcat区别之前,先说下tomcat是什么(如果知道了可以跳过哦!)

Tomcat:免费开源,轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。

实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。

只实现了JSP/Servlet的相关规范,不支持EJB。

虽说是tomcat服务器,但是并不是真正的硬件,它是部署在电脑上的软件服务。

tomcat的作用

上面说过Tomcat是一个容器,但为什么开发出来的应用需要装进Tomcat这个容器呢。忽略各个文件之间的跳转,web应用本质只是一个装有很多资源(java/html/jsp/js/css等各种格式文件)的文件夹。假如我们有一个web应用projectA,我们在某台计算机A把这些文件写好后,就希望其他设备能够通过一些方式来访问我们的资源。一种方法是通过在浏览器地址栏输入URL来实现资源的访问。

那么从我们在计算机A上写好某个文件夹到文件夹能够被其他计算机所访问,需要什么呢。首先需要我们的互联网。计算机B先通过互联网找到计算机A。

而这样做的前提是你这个电脑必须在互联网这个网络里面,这样别人才能访问到你。也就是说一台电脑必须要有IP地址才能称为服务器。但这样也只是找到了IP地址而已,我们还需要找到对应的主机(注:一般主机是指一台电脑,但在tomcat中,虚拟主机指的是计算机中的某个文件夹)。但就算找到了计算机A,我们怎么知道要去哪里寻找web应用projectA呢。Tomcat容器就是来解决这个问题的。在我看来,Tomcat的一个重要的功能就在于[映射](通过配置文件实现)。

javaweb项目都需要tomcat?

其实可以不要,之前Javaweb项目多为jsp,而jsp需要jsp容器来解释,所以需要tomcat等含有jsp容器的web服务器。使用jsp的时候,jsp没有main方法,怎么把服务启动呢,这个时候tomcat容器就很有必要了。

但随着近些年了,前后端分离导致不需要jsp容器来解释jsp,于是tomcat在项目中完全可以不要的,可以使用JBoss、Jetty等单纯Web应用服务器。

但tomcat也可以做Web服务器,所以项目中还是可以继续使用tomcat。

Java前后端分离的核心思想

前端html页面通过ajax调用后端的restuful api接口并使用json数据进行交互。前后端分离的项目就可以不使用tomcat容器。

springboot内置的tomcat

不得不说SpringBoot的开发者是在为大众程序猿谋福利,把大家都惯成了懒汉,xml不配置了,连tomcat也懒的配置了,典型的一键启动系统,那么tomcat在springboot是怎么启动的呢?

?

1

2

3

4

5

< dependency >

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

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

    < version >2.1.6.RELEASE</ version >

</ dependency >

?

1

2

3

4

5

6

7

@SpringBootApplication

public class MySpringbootTomcatStarter{

    public static void main(String[] args) {

        Long time=System.currentTimeMillis();

        SpringApplication.run(MySpringbootTomcatStarter. class );

    }

}

有的公司在生产环境不使用springboot自带的tomcat,则需要在代码中排出

?

1

2

3

4

5

6

7

8

9

10

11

< 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 >

将项目打成war包(下面会讲==),放到生产环境tomcat目录下运行

undertow和tomcat的区别

在 SpringBoot 框架中,使用最多的是 Tomcat,这是 SpringBoot 默认的容器技术,而且是内嵌式的 Tomcat。

同时,SpringBoot 也支持 Undertow 容器,我们可以很方便的用 Undertow 替换 Tomcat,而 Undertow 的性能和内存使用方面都优于 Tomcat。

在高并发系统中,Tomcat 相对来说比较弱。在相同的机器配置下,模拟相等的请求数,Undertow 在性能和内存使用方面都是最优的。并且 Undertow 新版本默认使用持久连接,这将会进一步提高它的并发吞吐能力。所以,如果是高并发的业务系统,Undertow 是最佳选择。

使用:

1.排除SpingBoot中自带的tomcat

?

1

2

3

4

5

6

7

8

9

10

11

      <!--springboot web-->

        < dependency >

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

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

            < exclusions >

                < exclusion >

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

                    < artifactId >spring-boot-starter-tomcat</ artifactId >

                </ exclusion >

            </ exclusions >

        </ dependency >

2.添加Undertow的依赖

?

1

2

3

4

5

        <!--undertow-->

        < dependency >

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

            < artifactId >spring-boot-starter-undertow</ artifactId >

        </ dependency >

这样即可,使用默认参数启动undertow服务器。如果需要修改undertow参数,继续往下看。

undertow的参数设置:

?

1

2

3

4

5

6

7

8

9

10

server:  

    port: 8084  

    http2:  

        enabled: true  

    undertow:  

        io-threads: 16  

        worker-threads: 256  

        buffer-size: 1024  

        buffers-per-region: 1024  

        direct-buffers: true

io-threads :IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接,默认设置每个CPU核心一个线程,不可设置过大,否则启动项目会报错:打开文件数过多。

worker-threads :阻塞任务线程池,当执行类似servlet请求阻塞IO操作,undertow会从这个线程池中取得线程。它的值取决于系统线程执行任务的阻塞系数,默认值是 io-threads*8

以下配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理。

buffer-size :每块buffer的空间大小,越小的空间被利用越充分,不要设置太大,以免影响其他应用,合适即可

buffers-per-region :每个区分配的buffer数量,所以pool的大小是buffer-size * buffers-per-region

direct-buffers :是否分配的直接内存(NIO直接分配的堆外内存) 

3. 启动SpringBoot测试

Undertow启动成功提示语:[INFO ] 2020-08-13 10:38:32 [main] o.s.b.w.e.u.UndertowServletWebServer - Undertow started on port(s) 80 (http) with context path ‘’

Tomcat启动成功提示语: [INFO ] 2020-08-13 10:41:35 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 80 (http) with context path ‘’

部署jar和war包

war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块。而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运行的.

它们都是压缩的包,拿Tomcat来说,将war文件包放置它的\webapps\目录下,启动Tomcat,这个包可以自动进行解压,也就是你的web目录,相当于发布了。

像之前jsp页面,项目必须打包成war,放置到tomcat容器中运行。

Spring Boot支持传统部署和更现代的部署形式。jar跟war都支持,在创建springboot项目时,默认是jar包,打成war包使用我上面说的即可

springboot下比较tomcat与undertow性能

第一步

pom.xml配置

如果使用tomcat服务器,则配置如下:

?

1

2

3

4

5

6

7

  < dependencies >

          < dependency >

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

            < artifactId >spring-boot-starter-tomcat</ artifactId >

            < scope >provided</ scope >

      </ dependency >

  </ dependencies >

如果使用undertow服务器,则配置如下:因为spring boot默认配置为tomcat:

?

1

2

3

4

5

6

7

8

9

10

11

< dependency >

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

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

        <!-- 若使用log4j2 -->

        < exclusions >

< exclusion > 

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

                          < artifactId >spring-boot-starter-tomcat</ artifactId > 

                        </ exclusion > 

</ exclusions >

    </ dependency >

再添加dependency依赖:

?

1

2

3

  < dependency >

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

            < artifactId >spring-boot-starter-undertow</ artifactId >

第二步

再在定制tomcat/undertow服务器

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

/**

  * 

  */

package com.lz.ovuola.general.util.tomcat;

import org.apache.catalina.connector.Connector;

import org.apache.coyote.http11.Http11NioProtocol;

import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;

import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;

import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

  * 编程方式自定义内嵌容器

  * 

  * @author fz

  *

  */

@Configuration

@ConfigurationProperties (prefix = "tomcat" )

public class CustomTomcatEmbeddedCustomizer {

private int maxThreads;

private int minSpareThreads;

private int acceptCount;

private int connectionTimeout;

private String URIEncoding = "UTF-8" ;

private boolean disableUploadTimeout;

private boolean enableLookups;

private String compression;

private int compressionMinSize;

private String compressableMimeType;

/**

* 订制内嵌tomcat容器

*/

@Bean

public EmbeddedServletContainerFactory servletContainer() {

TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

factory.addConnectorCustomizers( new MyTomcatConnectorCustomizer());

return factory;

}

class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {

public void customize(Connector connector) {

Http11NioProtocol protocol = (Http11NioProtocol) connector

.getProtocolHandler();

// 设置最大连接数

protocol.setMaxThreads(maxThreads);

protocol.setConnectionTimeout(connectionTimeout);

protocol.setMinSpareThreads(minSpareThreads);

protocol.setAcceptorThreadCount(acceptCount);

protocol.setDisableUploadTimeout(disableUploadTimeout);

protocol.setCompression(compression);

protocol.setCompressionMinSize(compressionMinSize);

protocol.setCompressableMimeType(compressableMimeType);

// connector.setURIEncoding(URIEncoding);

connector.setEnableLookups(enableLookups);

}

}

public int getMaxThreads() {

return maxThreads;

}

public void setMaxThreads( int maxThreads) {

this .maxThreads = maxThreads;

}

public int getMinSpareThreads() {

return minSpareThreads;

}

public void setMinSpareThreads( int minSpareThreads) {

this .minSpareThreads = minSpareThreads;

}

public int getAcceptCount() {

return acceptCount;

}

public void setAcceptCount( int acceptCount) {

this .acceptCount = acceptCount;

}

public int getConnectionTimeout() {

return connectionTimeout;

}

public void setConnectionTimeout( int connectionTimeout) {

this .connectionTimeout = connectionTimeout;

}

public String getURIEncoding() {

return URIEncoding;

}

public void setURIEncoding(String uRIEncoding) {

URIEncoding = uRIEncoding;

}

public boolean isDisableUploadTimeout() {

return disableUploadTimeout;

}

public void setDisableUploadTimeout( boolean disableUploadTimeout) {

this .disableUploadTimeout = disableUploadTimeout;

}

public boolean isEnableLookups() {

return enableLookups;

}

public void setEnableLookups( boolean enableLookups) {

this .enableLookups = enableLookups;

}

public String getCompression() {

return compression;

}

public void setCompression(String compression) {

this 测试数据pression = compression;

}

public int getCompressionMinSize() {

return compressionMinSize;

}

public void setCompressionMinSize( int compressionMinSize) {

this 测试数据pressionMinSize = compressionMinSize;

}

public String getCompressableMimeType() {

return compressableMimeType;

}

public void setCompressableMimeType(String compressableMimeType) {

this 测试数据pressableMimeType = compressableMimeType;

}

}

或者是 undertow,测试只要启动一个就行

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

//package com.lz.ovuola.general.util.tomcat;

//

//import io.undertow.Undertow.Builder;

//

//import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;

//import org.springframework.boot.context.embedded.undertow.UndertowBuilderCustomizer;

//import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;

//import org.springframework.boot.context.properties.ConfigurationProperties;

//import org.springframework.context.annotation.Bean;

//import org.springframework.context.annotation.Configuration;

//

//@Configuration

//public class CustomUndertowEmbeddedCustomizer {

//

//  @Bean

//  public EmbeddedServletContainerFactory servletContainer() {

//  UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();

//  factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {

//

//  @Override

//  public void customize(Builder builder) {

//  builder.addHttpListener(8080, "127.0.0.1");

//  }

//

//  });

//  return factory;

//  }

//

// }

第三步

在application -runAs -run as configuratuion-Arguments添加:--用于jconsole或者是visualVM监控,推荐使用后者

?

1

2

3

4

-Djava.rmi.server.hostname=127.0.0.1   --ip地址

-Dcom.sun.management.jmxremote

-Dcom.sun.management.jmxremote.port="9093"   --端口号

-Dcom.sun.management.jmxremote.authenticate="false"

第四步

采用visualVM监控同一个服务,分别开启tomcat/undertow容器,注意两者在application.propertites参数尽量相同,以便观察稳定性

第五步

打开jemter压力测试某一接口,观察堆内存、线程数、cpu等指标。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_49456013/article/details/110877262

查看更多关于关于springboot-starter-undertow和tomcat的区别说明的详细内容...

  阅读:25次