tomcat 配置文件设置实践总结

此文章纯粹为个人最近在工作中遇到的tomcat的一些实践问题,非原理解析,各位小伙伴可按需读取。
此文章将不定期更新,作为本人工作中遇到的tomcat相关问题的总结记录,不足之处欢迎各位大佬批评指正!

1、绿色解压版Tomcat的单机多部署问题

这个问题很简单,我们都知道,tomcat的启动项脚本文件都放置于/bin/路径下,若需在单机环境部署多个tomcat并需要同时启动,然而,又不想配置多个环境变量CATALINA_HOME的话,我们可以:

  • 打开/bin/startup.bat
  • 在文件开头添加:set CATALINA_HOME = YOUR_CATALINA_HOME其中YOUR_CATALINA_HOME为你的tomcat安装的路径

若需要同时开启多个tomcat,我们还需要将他们的端口号进行区分,防止端口占用的问题:

  • 打开/conf/server.xml
  • 修改 <Connector port="8081" protocol="HTTP/1.1"/><Server port="8005" shutdown="SHUTDOWN"><Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />使各个tomcat中的这三个端口号的port值都不同

通过上述操作,就能在同一台机器上同时打开多个tomcat了。

2、tomcat http请求并发数限制

这个问题的来源为,项目需要做等保审核,需要限制服务器的并发会话数不超过200,查看资料发现可以通过tomcat的配置来达到这个目的。首先,我们来了解下列几个参数:

  • maxThreads :此连接器将建立的用于处理请求的最大线程数量,决定了可以处理的并发请求的最大值。默认值为200,

官网原文:The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200.

  • acceptCount:当所有请求处理线程都被占用时,为接收到的并发请求设置的最大等待队列长度。任何在此队列达到上限之后发起的并发请求都将被拒绝,默认值为100。

官网原文:The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

  • maxConnections:服务器在任何给定时间内接收和处理的最大连接数,当达到此阈值时,服务器将接收请求直至达到acceptCount设置的阈值,但超出部分将不进行处理,直到正在处理的连接数量低于maxConnections,此时,服务器将开始接受并再次处理连接。对BIO连接器,默认值为maxThreads的值;对NIO默认值为10000;对APR/native,默认值为8192

官网原文:The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting. The default value varies by connector type. For BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO the default is 10000. For APR/native, the default is 8192.

在达到指定的acceptCount值后,任何进一步的并发请求都会收到connection refused响应,直到资源可用于处理它们。

看完上面的官网介绍,我们简单总结一下这几个参数的效果:

  • 情况1:接受一个请求,此时tomcat起动的线程数没有到达maxThreads,tomcat会起动一个线程来处理此请求。
  • 情况2:接受一个请求,此时tomcat起动的线程数已经到达maxThreads,tomcat会把此请求放入等待队列,等待空闲线程。
  • 情况3:接受一个请求,此时tomcat起动的线程数已经到达maxThreads,等待队列中的请求个数也达到了acceptCount,此时tomcat会直接拒绝此次请求,返回connection refused

并发测试实践:

本次实验我们使用jmeter 3.1工具模拟并发场景。

场景1

tomcat配置:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxThreads="10"
acceptCount="10"/>

此时,未设置maxConnections参数,预期并发请求数超过maxThreads+acceptCount=20将会出现返回connect refused的请求。
设置并发请求数为30:
并发数设置
开始测试,结果如下:
并发测试结果--聚合报告
并发测试结果--结果树

由上述结果可看出我们的猜想是正确的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值