In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what's the difference between springboot-starter-undertow and tomcat". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what's the difference between springboot-starter-undertow and tomcat?"
What is tomcat?
Before talking about the difference between undertow and tomcat, let's talk about what tomcat is (if you know it, you can skip it!)
Tomcat: free and open source, lightweight application server, widely used in small and medium-sized systems and not many concurrent access users, is the first choice for developing and debugging JSP programs.
The Tomcat part is actually an extension of the Apache server, but it runs independently, so when you run tomcat, it actually runs as a separate process from Apache.
Only the relevant specifications of JSP/Servlet are implemented, and EJB is not supported.
Although it is a tomcat server, it is not real hardware, it is a software service deployed on a computer.
The role of tomcat
It is said above that Tomcat is a container, but why do developed applications need to be loaded into the Tomcat container? Ignoring the jump between files, the web application is essentially a folder containing a lot of resources (java/html/jsp/js/css and other files in various formats). If we have a web application projectA, we hope that other devices can access our resources in some way after some computer A has written these files. One way is to access resources by entering URL in the browser's address bar.
So what do we need from writing a folder to a folder that can be accessed by other computers on computer A. First of all, we need our Internet. Computer B first finds computer A through the Internet.
The premise of doing this is that your computer must be in the Internet network, so that others can access you. In other words, a computer must have an IP address to be called a server. But this is only to find the IP address, we also need to find the corresponding host (Note: a general host refers to a computer, but in tomcat, a virtual host refers to a folder in the computer). But even if we find computer A, how do we know where to find the web app projectA. The Tomcat container is here to solve this problem. In my opinion, one of the important functions of Tomcat is "mapping" (implemented through configuration files).
All javaweb projects need tomcat?
In fact, you don't have to. Before, most Javaweb projects were jsp, but jsp needed jsp containers to explain, so you need web servers such as tomcat that contain jsp containers. When using jsp, jsp does not have a main method, so how to start the service? at this time, the tomcat container is very necessary.
But in recent years, with the separation of front and rear ends, there is no need for jsp containers to interpret jsp, so tomcat can be completely eliminated in the project, and pure Web application servers such as JBoss and Jetty can be used.
However, tomcat can also be used as a Web server, so you can continue to use tomcat in the project.
The core idea of separation of front and rear ends of Java
The front-end html page invokes the back-end restuful api interface through ajax and interacts with json data. Projects that are separated from the front and back ends can not use the tomcat container.
Tomcat built into springboot
It has to be said that the developers of SpringBoot are working for the benefit of public programmers, accustoming everyone to being lazy, not configuring xml, and even lazy configuring tomcat. Typical one-click startup system, then how does tomcat start in springboot?
Org.springframework.boot spring-boot-starter-web 2.1.6.RELEASE@SpringBootApplicationpublic class MySpringbootTomcatStarter {public static void main (String [] args) {Long time=System.currentTimeMillis (); SpringApplication.run (MySpringbootTomcatStarter.class);}}
Some companies do not use the tomcat that comes with springboot in the production environment, so they need to discharge it in the code.
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
Package the project into a war package (I'll talk about it below) and run it in the tomcat directory of the production environment.
The difference between undertow and tomcat
In the SpringBoot framework, Tomcat is the most commonly used, which is the default container technology of SpringBoot, and it is embedded Tomcat.
At the same time, SpringBoot also supports Undertow containers, we can easily replace Tomcat with Undertow, and Undertow is better than Tomcat in terms of performance and memory usage.
In high concurrency systems, Tomcat is relatively weak. Under the same machine configuration, simulating an equal number of requests, Undertow is optimal in terms of performance and memory usage. And the new version of Undertow uses persistent connections by default, which will further improve its concurrent throughput. Therefore, if it is a highly concurrent business system, Undertow is the best choice.
Use:
1. Exclude tomcat that comes with SpingBoot
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
two。 Add dependencies for Undertow
Org.springframework.boot spring-boot-starter-undertow
In this way, start the undertow server with the default parameters. If you need to modify the undertow parameter, move on.
Parameter settings for undertow:
Server: port: 8084 http2: enabled: true undertow: io-threads: 16 worker-threads: 256 buffer-size: 1024 buffers-per-region: 1024 direct-buffers: true
The number of io-threads:IO threads, which mainly perform non-blocking tasks, they will be responsible for multiple connections, the default setting of each CPU core one thread, can not be set too large, otherwise the startup project will report an error: too many files open.
Worker-threads: blocks the task thread pool from which undertow fetches threads when performing operations such as servlet request blocking IO. Its value depends on the blocking factor of the system thread executing the task, and the default value is io-threads*8.
The following configuration affects buffer, which is used for IO operations of server connections, somewhat similar to netty's pooled memory management.
Buffer-size: the space size of each buffer. The smaller the space is, the more fully utilized it is. Do not set it too large, so as not to affect other applications.
Buffers-per-region: the number of buffer allocated per zone, so the size of the pool is buffer-size * buffers-per-region
Direct-buffers: direct memory allocated or not (out-of-heap memory allocated directly by NIO)
3. Start the SpringBoot test
Undertow launch successful prompt: [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 launch successful prompt: [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''
Deploy jar and war packages
War is a web module, which needs to include WEB-INF, and is a WEB module that can be run directly. Jar generally includes only some class files, which can be run with the java command after declaring Main_class.
They are all compressed packages. In the case of Tomcat, put the war package in its\ webapps\ directory and start Tomcat. This package can be decompressed automatically, that is, your web directory, which is equivalent to a release.
Like the previous jsp page, the project must be packaged into a war and run in a tomcat container.
Spring Boot supports both traditional and more modern forms of deployment. Both jar and war are supported. When creating a springboot project, the default is the jar package, which can be packed into a war package and use what I said above.
The first step in comparing the performance of tomcat and undertow under springboot
Pom.xml configuration
If you are using a tomcat server, the configuration is as follows:
Org.springframework.boot spring-boot-starter-tomcat provided
If you use a undertow server, the configuration is as follows: because spring boot is configured as tomcat by default:
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
Then add dependency dependencies:
The second step of org.springframework.boot spring-boot-starter-undertow
And then customize the tomcat/undertow server
/ * * / 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 / * * Custom embedded container * * @ 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;/*** customized embedded tomcat container * / @ Beanpublic 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 (); / / set the maximum number of connections 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.compression = compression;} public int getCompressionMinSize () {return compressionMinSize;} public void setCompressionMinSize (int compressionMinSize) {this.compressionMinSize = compressionMinSize;} public String getCompressableMimeType () {return compressableMimeType } public void setCompressableMimeType (String compressableMimeType) {this.compressableMimeType = compressableMimeType;}}
Or undertow, just start one test.
/ / 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;//} /} step 3
Add:-- to application-runAs-run as configuratuion-Arguments for jconsole or visualVM monitoring, which is recommended
-Djava.rmi.server.hostname=127.0.0.1-ip address-Dcom.sun.management.jmxremote-Dcom.sun.management.jmxremote.port= "9093"-port number-Dcom.sun.management.jmxremote.authenticate= "false" step 4
Use visualVM to monitor the same service, and open the tomcat/undertow container respectively. Note that the parameters of both are the same as possible in application.propertites, so that the stability can be observed.
Step five
Open an interface of jemter stress test and observe heap memory, number of threads, cpu and other indicators.
At this point, I believe you have a deeper understanding of "what's the difference between springboot-starter-undertow and tomcat". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.