How to tune the performance of Tomcat7 for web server acceleration

How to tune the performance of Tomcat7 for web server acceleration

1. Tomcat is an application that runs in the Java JVM (Virtual Machine) environment and is developed using Java.

2. Tomcat also has its own web management function, which corresponds to the projects under webapps under Tomcat as follows:

They all correspond to different roles and can access specific projects. Currently, only Tomcat is as follows:

<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<role rolename="admin "/>
<role rolename="admin "/>

The configuration here is to configure the administrator account, the purpose is to enter the project deployed by Tomcat by default, and carry out the relevant Tomcat startup parameters and startup information

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

3. Doubt: Because I have not read and Baidu to the detailed description of the specific role permissions to access the project, I do not know admin, manager and manager-gui, admin-gui for the time being (these two are mainly to configure server status and host manager management page access) What kind of permissions does the difference in roles correspond to?

4. When the client dynamically requests to access the project dynamic resources (http1.0 or http1.1) under webapps under Tomcat, the resources will be directly returned according to http1.0 or http1.1, but when accessing static resources, in tomcat ( Server) will not directly return static resources, and will process an additional layer of static resources on the client and server side, server-->ajp (protocol)-->Static resource processing-->http protocol-->client , but usually the ajp protocol is generally not used.

Intermediate static resource processing is not directly processed locally, but forwarded to the server under the Apache foundation for static resource processing, which consumes resources and is not commonly used, and Nginx+ is generally used for static resource processing. Tomcat is used for processing, and Nginx processing performance is much faster than ajp. All can be optimized as follows: Disable the ajp protocol

5. Tomcat optimization methods

a) Virtual machine (JVM) optimization

b) Communication protocol optimization (using Bio, Nio, Aio and disabling ajp protocol in the end)

c) External connection pool optimization (the number of connection pools can be reasonably allocated according to the business volume and resources), and the optimal number of thread pool connections can be determined according to third-party tools to open concurrent tests.

d) Go deeper into the internal configuration (server.xml)

6. Trigger optimization of communication protocol according to Tomcat's communication protocol (Bio, Nio, Aio)

How to check what communication mode Tomcat uses when it starts, you can see the startup log:

Reference blog address: Tomcat Bio, Nio, Aio mode performance test and personal opinion, https://yq.aliyun.com/articles/14768

7. server.xml is the core configuration file of Tomcat, which includes the overall architecture information of Tomcat and the connection between components. For the configuration introduction of service.xml file, please refer to the blog address: https://www.cnblogs.com/kismetv/p/ 7228274.html

8. Compared with Bio, Nio's efficient processing is: synchronous non-blocking, more (channels, buffers and selectors, and the concept of connection pools, all can be optimized by setting the specific max number of connections according to the business volume performance, the default number of connection pools is 150).

9. Jetty has higher performance than Tomcat, but why so many companies are using Tomcat is that although Tomcat itself does not have the high performance of Jetty, the optimization of Tomcat and the scalability of Tomcat determine that Tomcat cannot be directly used by Jetty. Replaced, and based on Tomcat or developed by J2ee specifiers, it has also brought many people to use it. Tomcat can perform fine-grained control and optimization with nginx, which is even more powerful, and there are many internal optimizations in Tomcat.

10. Virtual machine (JVM) optimization parameters in Tomcat can be directly configured in catalina.bat

11. Connector parameter optimization (mainly used to accept client request processing, all its parameter configuration is also particularly important)

12. Enable (external configuration connection pool), the thread connection pool connection pool can be appropriately increased according to the business scenario and one of our server hardware resource conditions.

13. Question: The downloaded Tomcat uses the communication protocol of the Apr mode instead of Bio by default. How can I configure the communication protocol of Bio?

14. Question: When using Nio communication mode, it is not easy to configure the number of external thread pools. The management console prompts that the number of thread pools is -1?

15. When Tomcat is clustered, according to our project to determine whether to use the Nio mode, if the class library of our project is relatively new, after the 1.5 class library (jdk version), then we might as well put each Tomcat in our cluster. The startup mode of the server is set to high concurrency and high performance response mode --> NIO mode.

16. The server.xml configuration file is as follows:

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">

	<!-- 优化手段之三:启动外部连接池,来满足高并发已经复用的请求,根据业务场景(如每秒并发数)在硬件资源条件下可以加大线程连接池 -->
	<!-- 模拟并发来测试线程连接池在多少比较合适:使用第三方并非测试工具来模拟(telnet) -->
	<!-- 
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->

	<!--
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
	-->
	<!-- 优化手段之二:关闭bio,启动nio(高性能的应答模式), -->		
	<!-- 优化手段之四:连接器的优化,可以根据经验设置一个最佳实践配置参数 -->
	<!-- 细粒度控制问题决定一个企业要选择tomcat,而不选择jetty,要选择mybatis而不选择hibernate,架构师都是可控灵活的架构 -->
	<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
		   connectionTimeout="20000"
		   redirectPort="8443" />
	
	<!-- 优化手段之五:JVM参数的优化 -->
	<!-- 优化手段之六:虚拟机的性能优化 -->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
	
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 优化手段之一,禁用ajp 减少集群处理时间 -->
    <!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
		
      </Host>
    </Engine>
  </Service>
</Server>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325204255&siteId=291194637