Summary of common problems in operation and maintenance-tomcat

tomcat introduction

Tomcat server is an open source lightweight web application server, which is widely used in small and medium-sized systems and occasions with small concurrency. It is the first choice for developing and debugging Servlet and JSP programs.

#tomcat Introduction to the operating environment

1. Tomcat itself cannot run directly on the computer, it needs to rely on the operating system and Java virtual machine based on the hardware
;
2. When the Java program starts, the JVM will allocate an initial memory and maximum memory to the application; 3. When the application is used At the moment of maximum memory, the JVM will be triggered to do garbage collection (GC) to release the occupied memory;
4. Therefore, if you want to adjust the initial memory and maximum memory when the Java program is started, you need to apply to the JVM;
5. If the initial memory The size setting is too small, and there are too many application objects initialized at this time, the virtual machine must repeatedly load memory to meet the use;
6. Based on the above reasons, it is best to set the initial memory size (Xms) and maximum memory (Xmx) to The same;
7. All objects on the JVM allocate memory on """heap""" (there are also memory allocated on the "stack")
8. The size of the heap can be dynamically expanded, but " The size of ""heap""" is limited by the physical memory used by the system. When the memory required by the application exceeds the maximum value of "heap", the JVM virtual machine will throw a memory overflow exception and cause the application to crash;
9. Based on the above reasons, it is recommended that the size of the "heap" be set to 80% of the physical memory

tomcat optimization-memory optimization

Tomcat memory optimization is mainly to optimize the tomcat startup parameters. We can set the java_OPTS parameter in the tomcat startup script catalina.sh.

JAVA_OPTS parameter description -server enables jdk server version; -minimum memory when Xms java virtual machine is initialized; -maximum memory that can be used by Xmx java virtual machine; -XX: PermSize memory permanent reserved area -XX:MaxPermSize memory maximum permanent reserved area

Recommended configuration:

For server memory 2G, the following configurations can be adopted:

JAVA_OPTS=’-Xms1024m -Xmx2048m -XX: PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m’

After the configuration is complete, you can restart Tomcat, and check whether the configuration takes effect through the following command:
If you see that the Tomcat process number is 15242. 

Check whether the configuration takes effect:

sudo jmap-heap 15242
We can see that the parameters such as MaxHeapSize have taken effect.

tomcat optimization-concurrency optimization

Adjust the concurrent processing capability of the connector; in the Tomcat configuration file server.xml

<Connector port="9027"  protocol="HTTP/1.1"  maxHttpHeaderSize="8192"  minProcessors="100"  maxProcessors="1000"  acceptCount="1000"  redirectPort="8443"  disableUploadTimeout="true"/>

Parameter Description:

maxThreads The maximum number of threads requested by the client minSpareThreads The number of socket threads created when Tomcat is initialized maxSpareThreads The maximum number of idle socket threads for the Tomcat connector enableLookups If set to true, domain name resolution is supported, and the ip address can be resolved to the host name redirectPort If a secure channel is required If the client request is forwarded to the SSL-based redirectPort port acceptAccount The maximum number of listening port queues, the client request will be rejected when it is full (not less than maxSpareThreads) connectionTimeout connection timeout minProcessors The minimum number of processing threads when the server is created maxProcessors The maximum number of processing threads when the server is created Number of threads URIEncoding URL uniform encoding


tomcat optimization-cache compression optimization

Turn on the compression function of tomcat; the compression optimization of tomcat is the process of compressing the returned html page and other content, compressing it into gzip format, and sending it to the browser, and the browser decompresses it locally.

<Connector port="9027"  protocol="HTTP/1.1"  maxHttpHeaderSize="8192"  maxThreads="1000"  minSpareThreads="100"  maxSpareThreads="1000"  minProcessors="100"  maxProcessors="1000"  enableLookups="false"  compression="on"  compressionMinSize="2048"  compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"  connectionTimeout="20000"  URIEncoding="utf-8"  acceptCount="1000"  redirectPort="8443"  disableUploadTimeout="true"/>

其中:

 ●compression="on"   打开压缩功能      ●compressionMinSize="2048"启用压缩的输出内容大小,默认为2KB      ●noCompressionUserAgents="gozilla,traviata" 对于以下的浏览器,不启用压缩  ●compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" 哪些资源类型需要压缩

缓存策略:

开启浏览器的缓存,这样读取存放在webapps文件夹里的静态内容会更快,大大推动整体性能。

1)浏览器第一次请求tomcat服务器某资源2)tomcat查询到该资源,并将该资源最后修改的时间保存在响应头的Last-Modified中 (Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT) 3)浏览器第二次访问tomcat请求该资源,并将之前该资源的最后修改时间放入If-Modified-Since请求头中4)服务器收到该请求,比较该请求中的If-Modified-Since与资源最后修改时间Last-Modified是否一致,如果一致则不向其发送该资源并向其发送响应代码304告诉其去本地取缓存。

tomcat优化-安全优化

#降权启动

以普通用户启动tomcat,降权启动,防止不法分子通过tomcat获得root权限。

#修改端口号

修改tomcat配置文件server.xml中的全球人都知道的http连接器端口号,防止******。

#更改关闭Tomcat的指令

这个端口是有安全隐患的,直接Telnet远程连接主机,输入shutdown即可关闭tomcat。shown端口是写在Server参数里的,直接去掉是不管用,也是会默认启动的,一般在安全设置时候建议把端口修改为其他端口,shutdown修改为其他复杂字串。实际上这个端口是可以直接屏蔽不监听的。设置时候将其port值修改为-1即可:

<Server port="-1" shutdown="SHUTDOWN">

#修改管理员用户名,密码

修改tomcat-user.xml中默认的Manager用户名和密码

  <?xml version=’1.0’ encoding=’utf-8’?>                   <tomcat-users>                       <role rolename=”manager”/>                            <user username=”temobi” password=”temobi8090” roles=”manager”/>                  </tomcat-users>


#清空站点目录下ROOT下管理页面等文件

ROOT下有一些站点的管理程序可以查看tomcat的各种信息及配置,因此我们需要清空这些文件或者将站点目录更改。

生产环境一般不适用Tomcat默认的管理界面,这些页面存放在Tomcat 的webapps安装目录下,把该目录下的所有文件删除即可:rm -rf  /usr/local/tomcat8/webapps/*

tomcat优化-数据库性能调优

tomcat性能在等待数据库查询被执行期间会降低。如今大多数应用程序都是使用可能包含“命名查询”的关系型数据库。如果是那样的话,Tomcat会在启动时默认加载命名查询,这个可能会提升性能。另一件重要事是确保所有数据库连接正确地关闭。给数据库连接池设置正确值也是十分重要的。我所说的值是指Resource要素的最大空闲数(maxIdle),最大连接数(maxActive),最大建立连接等待时间(maxWait)属性的值。因为配置依赖与应用要求,我也不能在本文指定正确的值。你可以通过调用数据库性能测试来找到正确的值。 

tomcat优化-其他优化

#错误页面优雅显示

#隐藏版本号

#禁用DNS查询

当web应用程序向要记录客户端的信息时,它也会记录客户端的IP地址或者通过域名服务器查找机器名转换为IP地址。DNS查询需要占用网络,并且包括可能从很多很远的服务器或者不起作用的服务器上去获取对应的IP的过程,这样会消耗一定的时间。

为了消除DNS查询对性能的影响我们可以关闭DNS查询,方式是修改server.xml文件中的enableLookups参数值改为

false:enableLookups="false"


#设置session过期时间

Tomcat采用数据库连接池技术,当用户在一定时间不对数据库有操作时间后,就自动关闭这个连接,这是为了更好的利用资源,防止浪费宝贵的数据库连接资源。

以下是设置session时间的3个方法:

  1. 在tomcat——>conf——>servler.xml文件中定义:  <Context path="/test" docBase="/test"  defaultSessionTimeOut="3600" isWARExpanded="true"  isWARValidated="false" isInvokerEnabled="true"  isWorkDirPersistent="false"/>   defaultSessionTimeOut="3600"  2. 项目所在目录下的 web.xml中定义:  <session-config>  <session-timeout>20</session-timeout>  </session-config>  3. 在程序中定义:  session.setMaxInactiveInterval(30*60);  设置单位为秒,设置为-1永不过期


Guess you like

Origin blog.51cto.com/15127502/2655087