Docker 搭 JDK21 Tomcat 10.1.x 性能调优 home 编辑时间 2024/06/14 ![](/api/file/getImage?fileId=666bffd9c0a657000b00051d) ## 前言 听说 JDK21 + Tomcat 10.1.x 性能显著提升 尤其是开启虚拟线程后,可以有20%以上性能提升 正好在部署一台新的服务器,且配置不高 研究本篇先记录下如何部署 Docker + JDK21 + Tomcat 10 并做简单的性能调优 补充一下 目前个人感觉Tomcat跑在Docker下性能损失也很大 直接跑在物理机下明显性能会更强,还没有具体测过 后续可能还想研究一下,直接在物理机部署Tomcat 如何能像Docker一样稳定运行、宕机自动重启 以及性能对比 ## 折腾 先随便启动一次,把Tomcat文件复制出来 ```shell # 随便启动一下 docker run -d -p 8080:8080 --name tomcat tomcat:10.1.24-jdk21 # 复制tomcat目录到宿主机 docker cp tomcat:/usr/local/tomcat /root/docker/ # 停止并删除容器 docker stop tomcat docker rm tomcat ``` 接下来正式启动,把上一步得到的宿主机目录映射到虚拟机中 以下参数基于一台轻量云2C4G配置,请根据你的实际情况调整 ```shell # 参数解释 # -Xms500m:设置JVM的初始堆内存为500MB。 # -Xmx3000m:设置JVM的最大堆内存为3000MB。 # -XX:+UseG1GC:启用G1垃圾收集器,这是一种适合大堆内存的收集器,可以提供更可预测的停顿时间。 # -XX:MaxGCPauseMillis=200:设置G1收集器的最大垃圾收集停顿时间为200毫秒,以减少请求处理的延迟。 docker run -d -p 8080:8080 \ -e "TZ=Asia/Shanghai" \ -e CATALINA_OPTS="-Xms500m -Xmx3000m -XX:+UseG1GC -XX:MaxGCPauseMillis=200" \ --restart=always \ -v /root/docker/tomcat:/usr/local/tomcat \ -m 3000m \ --oom-kill-disable \ --memory-swap=3000m \ --name tomcat \ tomcat:10.1.24-jdk21 ``` 顺便看一眼日志 ```shell docker logs -f tomcat ``` ```shell 14-Jun-2024 15:47:45.557 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.1.24 14-Jun-2024 15:47:45.565 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: May 9 2024 17:41:23 UTC 14-Jun-2024 15:47:45.566 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.1.24.0 14-Jun-2024 15:47:45.566 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.4.119-20.0009.29 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /opt/java/openjdk 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 21.0.3+9-LTS 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Eclipse Adoptium 14-Jun-2024 15:47:45.569 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 14-Jun-2024 15:47:45.570 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 14-Jun-2024 15:47:45.609 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 14-Jun-2024 15:47:45.609 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED 14-Jun-2024 15:47:45.610 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xms500m 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmx3000m 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:+UseG1GC 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:MaxGCPauseMillis=200 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 14-Jun-2024 15:47:45.611 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 14-Jun-2024 15:47:45.615 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [2.0.7] using APR version [1.7.0]. 14-Jun-2024 15:47:45.620 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 3.0.2 15 Mar 2022] 14-Jun-2024 15:47:46.288 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 14-Jun-2024 15:47:46.346 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [1094] milliseconds 14-Jun-2024 15:47:46.464 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 14-Jun-2024 15:47:46.465 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.1.24] 14-Jun-2024 15:47:46.510 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 14-Jun-2024 15:47:46.558 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [207] milliseconds ``` <br><br> 先申明一下,从这里往下的内容我也是第一次接触 本意是想开启虚拟线程,网传可以有20%性能提升 但我短时间内也无法确定我的配置是否正确 仅供参考! 后期有时间我会单独写一篇虚拟线程相关文章 官方文档地址: https://tomcat.apache.org/tomcat-10.1-doc/config/executor.html https://tomcat.apache.org/tomcat-10.1-doc/config/http.html 调优 `server.xml` ```xml <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" className="org.apache.catalina.core.StandardVirtualThreadExecutor" maxThreads="600" minSpareThreads="16" maxIdleTime="60000"/> <Connector port="8080" executor="tomcatThreadPool" protocol="org.apache.coyote.http11.Http11Nio2Protocol" connectionTimeout="30000" redirectPort="8443" maxParameterCount="2000" maxConnections="1000" acceptCount="10000" URIEncoding="UTF-8" enableLookups="false" disableUploadTimeout="true" /> ``` 送人玫瑰,手留余香 赞赏 Wechat Pay Alipay 解决腾讯云轻量云服务器内网互联云数据库MySQL内网地址连不上问题 解决 SpringBoot 升级 3.3.0 MybatisPlus报错 Invalid value type for attribute 'factoryBeanObjectType': java.lang.String