目录
- 前言
- 1、Tomcat 的缺省端口是多少,怎么修改?
- 2、tomcat 有哪几种 Connector 运行模式(优化)?
- 3、Tomcat 有几种部署方式?
- 4、tomcat 容器是如何创建 servlet 类实例?用到了什么原理?
- 5.tomcat 如何优化?
- 6.内存调优
- 7.垃圾回收策略调优
- 8.添加 JMS 远程监控
- 9.专业点的分析工具有哪些?
- 10.关于 Tomcat 的 session 数目
- 11.监视 Tomcat 的内存使用情况
- 12.打印类的加载情况及对象的回收情况
- 13.Tomcat 一个请求的完整过程
- 14.Tomcat 工作模式?
- 15.你怎样给 tomcat 去调优?
- 16.如何加大 tomcat 连接数
- 17.怎样加大 tomcat 的内存。
- 18.Tomcat 有几种部署方式
- 19.Tomcat 的优化经验。
- 总结
前言
架构师面试题 - Tomcat 面试专题
1、Tomcat 的缺省端口是多少,怎么修改?
1)找到 Tomcat 目录下的 conf 文件夹
2)进入 conf 文件夹里面找到 server.xml 文件
3)打开 server.xml 文件
4)在 server.xml 文件里面找到下列信息
<Connector connectionTimeout="20000" port="8000" protocol="HTTP/1.1" redirectPort="8443" uriEncoding="utf-8"/>
2、tomcat 有哪几种 Connector 运行模式(优化)?
bio:传统的 Java I/O 操作,同步且阻塞 IO。maxThreads=”150”//Tomcat 使用线程来处理接收的每个请求。这个值表示 Tomcat 可创建的最大的线程数。默认值 200。可以根据机器的时期性能和内存大小调整,一般可以在 400-500。最大可以在 800 左右。minSpareThreads=”25”—Tomcat 初始化时创建的线程数。默认值 4。如果当前没有空闲线程,且没有超过 maxThreads,一次性创建的空闲线程数量。Tomcat 初始化时创建的线程数量也由此值设置。maxSpareThreads=”75”–一旦创建的线程超过这个值,Tomcat 就会关闭不再需要的 socket 线程。默认值 50。一旦创建的线程超过此数值,Tomcat 会关闭不再需要的线程。线程数可以大致上用 “同时在线人数每秒用户操作次数系统平均操作时间” 来计算。acceptCount=”100”—-指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理。默认值 10。如果当前可用线程数为 0,则将请求放入处理队列中。这个值限定了请求队列的大小,超过这个数值的请求将不予处理。connectionTimeout=”20000” –网络连接超时,默认值20000,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。nio:JDK1.4 开始支持,同步阻塞或同步非阻塞 IO。指定使用 NIO 模型来接受 HTTP 请求protocol=”org.apache.coyote.http11.Http11NioProtocol” 指定使用 NIO 模型来接受 HTTP 请求。默认是 BlockingIO,配置为 protocol=”HTTP/1.1” acceptorThreadCount=”2” 使用 NIO 模型时接收线程的数目 aio(nio.2):
JDK7 开始支持,异步非阻塞 IO。apr:Tomcat 将以 JNI 的形式调用 Apache HTTP 服务器的核心动态链接库来处理文件读取或网络传输操作,从而大大地提高 Tomcat 对静态文件的处理性能。
<!-- protocol 启用 nio 模式,(tomcat8 默认使用的是 nio)(apr 模式利用系统级
异步 io) -->
<!-- minProcessors 最小空闲连接线程数-->
<!-- maxProcessors 最大连接线程数-->
<!-- acceptCount 允许的最大连接数,应大于等于 maxProcessors-->
<!-- enableLookups 如果为 true,requst.getRemoteHost 会执行 DNS 查找,反向
解析 ip 对应域名或主机名-->
<Connector port="8080"
protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000" redirectPort="8443 maxThreads=“500”
minSpareThreads=“100” maxSpareThreads=“200” acceptCount="200"
enableLookups="false"/>
其他配置 maxHttpHeaderSize=“8192” http 请求头信息的最大程度,超过此长度的部分不予处理。一般 8K。URIEncoding=“UTF-8” 指定 Tomcat 容器的 URL 编码格式。
disableUploadTimeout=“true” 上传时是否使用超时机制 enableLookups=“false”–是否反查域名,默认值为 true。为了提高处理能力,应设置为 false compression=“on” 打开压缩功能 compressionMinSize=“10240” 启用压缩的输出内容大小,默认为 2KB noCompressionUserAgents=“gozilla, traviata” 对于以下的浏览器,不启用压缩
compressableMimeType=“text/html,text/xml,text/javascript,text/css,text/plain”
3、Tomcat 有几种部署方式?
1)直接把 Web 项目放在 webapps 下,Tomcat 会自动将其部署
2)在 server.xml 文件上配置 节点,设置相关的属性即可
3)通过 Catalina 来进行配置:进入到 conf\Catalina\localhost 文件下,创建一个xml 文件,该文件的名字就是站点的名字。编写 XML 的方式来进行设置。
4、tomcat 容器是如何创建 servlet 类实例?用到了什么原理?
当容器启动时,会读取在 webapps 目录下所有的 web 应用中的 web.xml 文件,然后对xml 文件进行解析,并读取 servlet 注册信息。然后,将每个应用中注册的 servlet 类都进行加载,并通过反射的方式实例化。(有时候也是在第一次请求时实例化)在 servlet 注册时加上如果为正数,则在一开始就实例化,如果不写或为负数,则第一次请求实例化。
5.tomcat 如何优化?
1、优化连接配置.这里以 tomcat7 的参数配置为例,需要修改 conf/server.xml 文件,修改连接数,关闭客户端 dns 查询。参数解释:URIEncoding=”UTF-8ʺ :使得 tomcat 可以解析含有中文名的文件的 url,真方便,不像 apache 里还有搞个mod_encoding,还要手工编译 maxSpareThreads : 如果空闲状态的线程数多于设置的数目,则将这些线程中止,减少这个池中的线程总数。minSpareThreads : 最小备用线程数,tomcat 启动时的初始化的线程数。
enableLookups : 这个功效和 Apache 中的 HostnameLookups 一样,设为关闭。
connectionTimeout : connectionTimeout 为网络连接超时时间毫秒数。
maxThreads : maxThreads Tomcat 使用线程来处理接收的每个请求。这个值表示 Tomcat 可创建的最大的线程数,即最大并发数。
acceptCount : acceptCount 是当线程数达到 maxThreads 后,后续请求会被放入一个等待队列,这个 acceptCount 是这个队列的大小,如果这个队列也满了,就直接 refuse connection maxProcessors 与 minProcessors : 在 Java 中线程是程序运行时的路径,是在一个程序中与其它控制线程无关的、能够独立运行的代码段。它们共享相同的地址空间。多线程帮助程序员写出 CPU 最 大利用率的高效程序,使空闲时间保持最低,从而接受更多的请求。通常 Windows 是 1000 个左右,Linux 是 2000 个左右。
useURIValidationHack:我们来看一下 tomcat 中的一段源码:
<!–enable tomcat ssl–>
<Connector port