tomcat common functions

Modify the port number

Take a port number between 1024-655365
Tomcat has three important ports:
the default access port: 8080
Default listening port is closed tomcat: 8005
Default AJP Access Port: 8009

vim tomcat/conf/server.xml
···
<Server port="8005" shutdown="SHUTDOWN"> #关闭时服务监听的端口
···
    <Connector port="8080" protocol="HTTP/1.1"  #客户端访问监听的端口
               connectionTimeout="20000"
               redirectPort="8443" />
···
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  #处理AJP协议监听的端口

Modify memory

Modify the tomcat memory in two ways:

  1. Modify catalina.bat bin directory (Windows under) | (Linux under) catalina.sh;
  2. Modify startup.bat bin directory (Windows under) | (Linux under) catalina.sh;
JAVA_OPTS="-server -Xms256m -Xms512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m"
#将上面的代码添加到catalina.sh的第二行,或设置catalina.sh中第250行左右的JAVA_OPTS后面的参数。

#注:server:虚拟机的运行模式,多个CPU的时候更好的发挥性能。默认是client。
    Xms:堆内存。java heap初始大小,默认为物理内存的64分之1。最大不要超过物理内存的80%。
    Xmx:java heap的最大值,建议设置为物理内存的一半。
    MetaspaceSize:初始原空间的大小,默认21M。
    MaxMetaspaceSize:最大原空间大小,默认无上限
#另jvm默认的最小内存为机器物理内存的1/64,最大内存为机器物理内存的1/4

Why should modify Tomcat memory?

Daily development, when large development projects are more dependent jar package, and when the application server starts, the project will be a reference for all the classes in turn loaded into memory all of them, java mode is divided into logical memory heap ( examples of storage class, an array, a reference object with the new type is generated), the stack memory (such as local variable storage method parameter), a static memory area (persistent zone, the area memory is not recovered GC)

Common memory exception:
OutOfMemoryError: Java head space abnormal
reasons: heap memory is full, dependent jar too much.
OutOfMemoryError: PermGen abnormal space
reasons: the static memory area is full, much like loading
abnormal StackOverflowError
reasons: stack memory overflow, resulting in an infinite loop or infinite recursion

Tomcat hot deployment

Hot deployment of three ways:

1、将web应用文件war包直接放在webapps里,tomcat运行时会自动解压;

2、修改sever.xml文件,在Host段内添加Context标签。如下:
<Context debug="0" path="/xxxxdemo" docBase="项目路径" reloadable="true"></Context>

#path为访问时端口号后加的路径,项目名。
#debug为项目异常时,输出的日志文件的详细程度,值越大越详细。

3、在conf/Catalina/localhost文件夹下新建xml文件,内容同上,这种情况不能设置path属性,tomcat不认,只能用该xml文件的名字作为部署的应用名。如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context  docBase="项目路径" reloadable="true"></Context>

Hide the version number

  • lib/catalina.jar/org/apache/catalina/util/ServerInfo.properties中server.info=Apache Tomcat/8.5.31改为NO VERSION

AJP protocol

If may be ignored by the front tomcat nginx reverse proxy ajp connector (Connector), should the use of the apache httpd server can not close ajp connector, the connection is closed only needs to server.xml comment out the corresponding node. as follows:

Cookies modify security

In tomcat context.xml configuration file context node is set to open useHttpOnly = true cookie encryption, improve security, the cookie is not so easy to be stolen, prevent xss attacks

Performance Optimization

tomcat three motions modes:

  • BIO: inefficient, a thread can only handle a request, the request too much time, the corresponding increase in threads, it is a waste of resources, tomcat7 following versions are BIO,

  • NIO: based buffer, non-blocking I / O. BIO higher efficiency with respect to (a small amount of multi-threaded processing requests),

  • APR: the need to support the operating system (tomcat7 and above versions of the operating system installed when the APR, tomcat default mode is apr)

Guess you like

Origin www.cnblogs.com/Smbands/p/11468624.html