Tomcat manager 配置


Tomcat manager 配置

概述:

因需要经常进行应用发布,应用服务为tomcat。每次发布时,都需要远程登录到应用服务器,将升级文件拷贝到指定目录,然后重启tomcat,感觉太麻烦了,尤其是服务器版本为windows 2003更是抓狂。

通过研究发现,关于升级文件的更新,可以通过架设ftp服务器方式解决,但是重启tomcat,就有点麻烦了。通过研究官方文档,终于实现通过命令行方式进行远程重启tomcat服务的方式,整理相关知识点如下。

基于tomcat版本:8.5.24

一、Tomcat 角色

关于tomcat 角色的定义,详见的 manager 应用服务目录下的 web.xml 文件。

  • manager-gui — Access to the HTML interface.
  • manager-status — Access to the “Server Status” page only.
  • manager-script — Access to the tools-friendly plain text interface that is described in this document, and to the “Server Status” page.
  • manager-jmx — Access to JMX proxy interface and to the “Server Status” page.

二、配置用户

编辑用户配置文件:$CATALINA_BASE/conf/tomcat-users.xml

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<user username="myName" password="myPwd" roles="manager-gui"/>
<user username="myName_script" password="myPwd" roles="manager-script"/>
<user username="myName_jmx" password="myPwd" roles="manager-jmx"/>

添加完成上述用户后,就可以登录manager进行配置管理:

三、通过html访问manager

http://{host}:{port}/manager/html

此url直接在浏览器里即可查看

四、通过命令行访问manager

http://{host}:{port}/manager/text/{command}?{parameters}

此命令需要使用 curl 命令执行,具体方法请自行百度。

1、列出当前已部署的应用

curl -u myName_script:myPwd http://localhost:8080/manager/text/list
输出:
OK - Listed applications for virtual host [localhost]
/:running:0:ROOT
/examples:running:0:examples
/host-manager:running:0:host-manager
/manager:running:0:manager
/docs:running:0:docs
/test:running:0:test

2、重启应用服务

curl -u myName_script:myPwd http://localhost:8080/manager/text/reload?path=/test
输出:
OK - Reloaded application at context path [/test]

3、查看OS,jvm信息

curl -u myName_script:myPwd http://localhost:8080/manager/text/serverinfo

4、session统计

curl -u myName_script:myPwd http://localhost:8080/manager/text/sessions?path=/test
输出:
OK - Session information for application at context path [/test]
Default maximum session inactive interval [50] minutes
[<1] minutes: [1] sessions

5、session失效

显示session统计信息,将时间 >num 分钟的session失效,如果使全部session失效,设置 & idle=0

curl -u myName_script:myPwd "http://localhost:8080/manager/text/expire?path=/test&idle=num"
输出:(设置&idle=5):
OK - Session information for application at context path [/test]
Default maximum session inactive interval [50] minutes
[8 - <9] minutes: [1] sessions
[>5] minutes: [1] sessions were expired

注意: 在windows中,上述url包含多个参数,需要添加双引号,否则会报错:

“‘idle’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。”

6、启动服务

curl -u myName_script:myPwd "http://localhost:8080/manager/text/start?path=/test"
输出:
OK - Started application at context path [/test]

7、停止服务

curl -u myName_script:myPwd "http://localhost:8080/manager/text/stop?path=/test"
输出,可以看出 test 服务已停止:
OK - Listed applications for virtual host [localhost]
/:running:0:ROOT
/examples:running:0:examples
/host-manager:running:0:host-manager
/test:stopped:0:test
/manager:running:0:manager
/docs:running:0:docs

8、卸载服务

curl -u myName_script:myPwd "http://localhost:8080/manager/text/undeploy?path=/test"
输出:
FAIL - Unable to delete [D:\apache-tomcat-8.5.24\webapps\test]. The continued presence of this file may cause problems.

打开 test 应用服务目录,发现除了 WEB-INF 文件夹以外,其他文件都被删除掉了。感觉tomcat的卸载功能可能有问题,有时间再看。

9、查找内存泄漏

curl -u myName_script:myPwd "http://localhost:8080/manager/text/findleaks[?statusLine=[true|false]]"
输出:
OK - No memory leaks found

由于当前没有内存溢出的服务,所以没有输出异常。

如果存在内存溢出的服务,将输出如下:

/leaking-webapp

10、Connector SSL/TLS 诊断

curl -u myName_script:myPwd "http://localhost:8080/manager/text/sslConnectorCiphers"
输出:
OK - Connector / SSL Cipher information
Connector[HTTP/1.1-8080]
  SSL is not enabled for this connector
Connector[AJP/1.3-8009]
  SSL is not enabled for this connector

11、线程转储(Thread Dump)

curl -u myName_script:myPwd "http://localhost:8080/manager/text/threaddump"
输出:
OK - JVM thread dump
2018-01-23 16:25:36.123
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.144-b01 mixed mode):

"ajp-nio-8009-AsyncTimeout" Id=69 cpu=0 ns usr=0 ns blocked 0 for -1 ms waited 1485 for -1 ms
   java.lang.Thread.State: TIMED_WAITING
        at java.lang.Thread.sleep(Native Method)
        at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1133)

12、VM Info

curl -u myName_script:myPwd "http://localhost:8080/manager/text/vminfo"
输出:
OK - VM info
2018-01-23 16:27:29.762
Runtime information:
  vmName: Java HotSpot(TM) 64-Bit Server VM
  vmVersion: 25.144-b01
  vmVendor: Oracle Corporation
  specName: Java Virtual Machine Specification
  specVersion: 1.8

13、保存配置

curl -u myName_script:myPwd "http://localhost:8080/manager/text/save"
输出:
FAIL - No StoreConfig MBean registered at [Catalina:type=StoreConfig]. Registration is typically performed by the StoreConfigLifecycleListener.

感觉这个命令暂时用不到,以后用到再研究。

五、服务器状态(Server Status)

html格式输出:

http://localhost:8080/manager/status
http://localhost:8080/manager/status/all

xml格式输出:

http://localhost:8080/manager/status?XML=true
http://localhost:8080/manager/status/all?XML=true

直接在浏览器里执行即可。

六、远程访问权限

默认情况,manger仅支持本地访问,如果需要远程访问,需要进行如下设置,打开 manager.xml ,文件地址如下:

$CATALINA_BASE/conf/Catalina/localhost/manager.xml

编辑文件内容如下,允许127.0.0.1访问,和192.168.0.1两个IP地址访问:

<?xml version="1.0" encoding="UTF-8"?>
<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1|192\.168\.0\.1" />
</Context>

参考

https://tomcat.apache.org/tomcat-8.5-doc/manager-howto.html


原文链接:https://blog.csdn.net/huryer/article/details/79146926?utm_medium=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase