Tomcat 7 性能优化

  1. 目的

通过优化tomcat提高网站的并发能力。

  1. 服务器资源

服务器所能提供CPU、内存、硬盘的性能对处理能力有决定性影响。

  1. 优化配置

  1. 配置tomcat管理员账户

在conf/ tomcat-users.xml下添加用户:

<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="admin-gui,admin,manager-gui,manager"/>

启动tomcat,登录查看信息:

http://127.0.0.1:8080/

  1. tomcat的3种运行模式

tomcat的运行模式有3种:

  1. bio
    默认的模式,性能非常低下,没有经过任何优化处理和支持.

  1. nio
    nio(new I/O),是Java SE 1.4及后续版本提供的一种新的I/O操作方式(即java.nio包及其子包)。Java nio是一个基于缓冲区、并能提供非阻塞I/O操作的Java API,因此nio也被看成是non-blocking I/O的缩写。它拥有比传统I/O操作(bio)更好的并发运行性能。

  1. apr
    安装起来最困难,但是从操作系统级别来解决异步的IO问题,大幅度的提高性能.

  1. 启动NIO模式

修改server.xml里的Connector节点,修改protocol为org.apache.coyote.http11.Http11NioProtocol

  1. 执行器(线程池)

在tomcat中每一个用户请求都是一个线程,所以可以使用线程池提高性能。

  1. 开启并且使用

配置:

  1. 参数说明

Attribute

Description

threadPriority (优先级)

(int) The thread priority for threads in the executor, the default is 5 (the value of theThread.NORM_PRIORITY constant)

daemon(守护进程)

(boolean) Whether the threads should be daemon threads or not, the default is true

namePrefix(名称前缀)

(String) The name prefix for each thread created by the executor. The thread name for an individual thread will be namePrefix+threadNumber

maxThreads(最大线程数)

(int) The max number of active threads in this pool, default is 200

minSpareThreads(最小活跃线程数)

(int) The minimum number of threads always kept alive, default is 25

maxIdleTime(空闲线程等待时间)

(int) The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)

maxQueueSize(最大的等待队里数,超过则请求拒绝)

(int) The maximum number of runnable tasks that can queue up awaiting execution before we reject them. Default value is Integer.MAX_VALUE

prestartminSpareThreads

(是否在启动时就生成minSpareThreads个线程)

(boolean) Whether minSpareThreads should be started when starting the Executor or not, the default is false

threadRenewalDelay

(重建线程的时间间隔)

(long) If a ThreadLocalLeakPreventionListener is configured, it will notify this executor about stopped contexts. After a context is stopped, threads in the pool are renewed. To avoid renewing all threads at the same time, this option sets a delay between renewal of any 2 threads. The value is in ms, default value is 1000 ms. If value is negative, threads are not renewed.

。重建线程池内的线程时,为了避免线程同时重建,每隔threadRenewalDelay(单位: ms )重建一个线程。默认值为1000 ,设置为负则不重建

  1. 最佳实践

  1. 连接器(Connector)

Connector是Tomcat接收请求的入口,每个Connector有自己专属的监听端口

Connector有两种:HTTP Connector和AJP Connector

  1. 通用属性(高亮的是重点)

Attribute

Description

allowTrace

A boolean value which can be used to enable or disable the TRACE HTTP method. If not specified, this attribute is set to false.

如果需要服务器能够处理用户的HAED/TRACE请求,这个值应该设置为true,默认值是false

asyncTimeout

The default timeout for asynchronous requests in milliseconds. If not specified, this attribute is set to 10000 (10 seconds).

默认超不时候以毫秒为单位的异步恳求。若是没有指定,该属性被设置为10000(10秒)。

enableLookups

Set to true if you want calls to request.getRemoteHost() to perform DNS lookups in order to return the actual host name of the remote client. Set to false to skip the DNS lookup and return the IP address in String form instead (thereby improving performance). By default, DNS lookups are disabled.

若是你想request.getRemoteHost()的调用 履行,以便返回的长途客户端的实际主机名的DNS查询,则设置为true。设置为false时跳过DNS查找,并返回字符串情势的IP地址(从而提高性能)。默认景象下,禁用DNS查找。

maxHeaderCount

The maximum number of headers in a request that are allowed by the container. A request that contains more headers than the specified limit will be rejected. A value of less than 0 means no limit. If not specified, a default of 100 is used.

容器允许的请求头字段的最大数目。请求中包含比指定的限制更多的头字段将被拒绝。值小于0表示没有限制。如果没有指定,默认设置为100。

maxParameterCount

The maximum number of parameter and value pairs (GET plus POST) which will be automatically parsed by the container. Parameter and value pairs beyond this limit will be ignored. A value of less than 0 means no limit. If not specified, a default of 10000 is used. Note that FailedRequestFilter

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

minmax329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值