https://blog.csdn.net/hanchao5272/article/details/99649252
Jetty和tomcat的比较
Tomcat和Jetty都是一种Servlet引擎
,他们都支持标准的Servlet规范
和JavaEE规范
。
架构比较
Jetty的架构比Tomcat的更为简单。
Jetty的架构是基于Handler
来实现的,主要的扩展功能都可以用Handler来实现,扩展简单
。
Tomcat的架构是基于容器
设计的,进行扩展是需要了解Tomcat的整体设计结构,不易扩展
。
性能比较
Jetty和Tomcat性能方面差异不大。
Jetty可以同时处理大量连接而且可以长时间保持连接
,适合于web聊天
应用等等。
Jetty的架构简单,因此作为服务器,Jetty可以按需加载组件,减少不需要的组件,减少了服务器内存
开销,从而提高服务器性能
。
Jetty默认采用NIO
(非阻塞IO),在处理I/O请求上更占优势,在处理静态资源时,性能较高
。
Tomcat适合处理少数非常繁忙
的链接,也就是说链接生命周期短
的话,Tomcat的总体性能更高。
Tomcat默认采用BIO
(阻塞IO)处理I/O请求,在处理静态资源时,性能较差。
Servlet规范支持方面
Jetty的应用更加快速,修改简单,对新的Servlet规范的支持较好。 GAE(谷歌应用引擎已经全面切换为Jetty)
Tomcat目前应用比较广泛,对JavaEE和Servlet的支持更加全面,很多特性会直接集成进来。(中小企业还再在用)
配置
maven配置
maven的pom.xml只需要排除tomcat并引入jetty即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Jetty适合长连接应用,就是聊天类的长连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
application.yml配置
配置方面,保持之前的内容即可。server.port
和server.servlet.context-path
的配置不变。
server:
port: 1314
servlet:
context-path: /xxxx
#jetty配置,主要是acceptors和selectors
jetty:
acceptors: 2
selectors: 4
#tomcat的配置可以保留,切换回来可用,反正不会生效
tomcat:
remote-ip-header: x-forward-for
uri-encoding: UTF-8
max-threads: 2000
#background-processor-delay: 30
max-http-header-size: 8096
basedir: ${user.home}/tomcat/tmp
max-connections: 5000
max-http-post-size: 10000000
connection-timeout: 600000
application.properties配置
如果properties可以配置如下:
####Jetty properties########
server.jetty.acceptors=2 # acceptor线程数
server.jetty.max-http-post-size=0 # put或post方法最大字节数
server.jetty.selectors=4 # selector线程数
Jetty参数解读
Jetty的线程架构模型非常简单,分为acceptors
,selectors
和workers
三个线程池。
acceptors
负责接受新连接,然后交给selectors
处理HTTP消息协议的解包,最后由workers
处理请求。
前两个线程池采用非阻塞模型
,一个线程可以处理很多socket的读写,所以线程池数量较小。
大多数项目,acceptors
线程只需要1-2个,selectors
线程配置2~4个足矣。
workers
是阻塞性的业务逻辑,往往有较多的数据库操作,需要的线程数量较多,具体数量随应用程序的QPS和IO事件占比而定。
QPS越高,需要的线程数量越多,IO占比越高,等待的线程数越多,需要的总线程数也越多。
——————————————————————————————————————————————————————
https://www.cnblogs.com/chenpi/p/9696319.html
阅读目录
前言
默认情况下,Spring Boot会使用内置的tomcat容器去运行应用程序,但偶尔我们也会考虑使用Jetty去替代Tomcat; 对于Tomcat和Jetty,Spring Boot分别提供了对应的starter,以便尽可能的简化我们的开发过程; 当我们想使用Jetty的时候,可以参考以下步骤来使用。
添加spring-boot-starter-jetty依赖
我们需要更新pom.xml
文件,添加spring-boot-starter-jetty
依赖,同时我们需要排除spring-boot-starter-web
默认的spring-boot-starter-tomcat
依赖,如下所示:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
如果我们的工程是使用Gradle构建的话,可以使用以下方式达到同样的效果:
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT")
compile("org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT")
}
配置Jetty参数
我们可以在application.properties配置文件里配置相关参数,去覆盖Jetty默认使用的运行参数: application.properties
server.port=8080
server.servlet.context-path=/home
####Jetty specific properties########
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.
同样,我们可以通过JettyEmbeddedServletContainerFactory
bean以编程的方式去配置这些参数
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer =
new JettyEmbeddedServletContainerFactory();
jettyContainer.setPort(9000);
jettyContainer.setContextPath("/home");
return jettyContainer;
}
Spring boot 2.0.0.RELEASE版本之后的更新
以上代码针对的是Spring Boot Snapshot版本,在Spring boot 2.0.0.RELEASE版本之后,我们应该使用 ConfigurableServletWebServerFactory
和 JettyServletWebServerFactory
类去配置Jetty参数: 创建ConfigurableServletWebServerFactory
Bean
@Bean
public ConfigurableServletWebServerFactory webServerFactory()
{
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.setPort(9000);
factory.setContextPath("/myapp");
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
return factory;
}