Tomcat ( 单机多 Tomcat 并存 )

Posted 小怪兽的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat ( 单机多 Tomcat 并存 )相关的知识,希望对你有一定的参考价值。


简介:

Tomcat 扩展( 一台服务器运行多个 tomcat )

一、安装 JDK 、Tomcat

shell > rpm -ivh jdk-8u25-linux-x64.rpm # 安装 jdk ,jdk 版本最好与 tomcat 一致

shell > tar zxf apache-tomcat-8.0.14.tar.gz -C /usr/local/
shell > cd ..
shell > cp -r apache-tomcat-8.0.14 tomcat1 # 复制三份 tomcat 其中 tomcat1 保持不变
shell > cp -r apache-tomcat-8.0.14 tomcat2
shell > cp -r apache-tomcat-8.0.14 tomcat3

二、配置多 Tomcat

shell > vim tomcat2/conf/server.xml # 修改 tomcat2
22 <Server port="8006" shutdown="SHUTDOWN"> # 原端口为 8005 修改为 8006
69 <Connector port="8081" protocol="HTTP/1.1" # 原端口为 8080 修改为 8081
91 <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> # 原端口为 8009 修改为 8019

shell > vim tomcat3/conf/server.xml # 修改 tomcat3
22 <Server port="8007" shutdown="SHUTDOWN"> # 原端口为 8005 修改为 8007
69 <Connector port="8082" protocol="HTTP/1.1" # 原端口为 8080 修改为 8082
91 <Connector port="8029" protocol="AJP/1.3" redirectPort="8443" /> # 原端口为 8009 修改为 8029

三、测试

shell > echo "welcome to tomcat1" > /usr/local/tomcat1/webapps/ROOT/index.html # 创建首面文件
shell > echo "welcome to tomcat2" > /usr/local/tomcat2/webapps/ROOT/index.html
shell > echo "welcome to tomcat3" > /usr/local/tomcat3/webapps/ROOT/index.html

shell > /usr/local/tomcat1/bin/startup.sh # 启动 tomcat1
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

shell > tail /usr/local/tomcat1/logs/catalina.out # 查看日志,这里有 tomcat 的开启、关闭信息
31-Oct-2014 16:46:29.296 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat1/webapps/manager has finished in 104 ms
31-Oct-2014 16:46:29.301 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat1/webapps/examples
31-Oct-2014 16:46:30.488 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat1/webapps/examples has finished in 1,187 ms
31-Oct-2014 16:46:30.492 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat1/webapps/docs
31-Oct-2014 16:46:30.557 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat1/webapps/docs has finished in 65 ms
31-Oct-2014 16:46:30.564 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat1/webapps/ROOT
31-Oct-2014 16:46:30.614 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat1/webapps/ROOT has finished in 50 ms
31-Oct-2014 16:46:30.624 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
31-Oct-2014 16:46:30.643 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
31-Oct-2014 16:46:30.649 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 2772 ms

# 可以看到服务启动花费的时间

shell > netstat -anpt | grep java # 监听的端口( 缺一不可 )
tcp 0 0 :::8080 :::* LISTEN 1304/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1304/java
tcp 0 0 :::8009 :::* LISTEN 1304/java

shell > curl "http://192.168.1.80:8080" # 访问一下首页文件
welcome to tomcat1

shell > ps aux | grep java # 看看进程路径也没有问题
root 1304 2.2 8.6 2270808 88064 pts/0 Sl 16:46 0:07 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat1/endorsed -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start

shell > /usr/local/tomcat1/bin/shutdown.sh # 关闭 tomcat1
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
shell > /usr/local/tomcat1/bin/startup.sh # 启动 tomcat1
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

shell > /usr/local/tomcat2/bin/startup.sh # 启动 tomcat2
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

shell > /usr/local/tomcat3/bin/startup.sh # 启动 tomcat3
Using CATALINA_BASE: /usr/local/tomcat3
Using CATALINA_HOME: /usr/local/tomcat3
Using CATALINA_TMPDIR: /usr/local/tomcat3/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat3/bin/bootstrap.jar:/usr/local/tomcat3/bin/tomcat-juli.jar
Tomcat started.
shell > netstat -anpt | grep java # 这种情况是 tomcat 还没有完全启动,稍等即可
tcp 0 0 :::8080 :::* LISTEN 1381/java
tcp 0 0 :::8081 :::* LISTEN 1411/java
tcp 0 0 :::8082 :::* LISTEN 1441/java
tcp 0 0 :::8019 :::* LISTEN 1411/java
tcp 0 0 :::8029 :::* LISTEN 1441/java
tcp 0 0 :::8009 :::* LISTEN 1381/java

shell > tail /usr/local/tomcat3/logs/catalina.out # 日志中也没有出来服务启动成功的标志
31-Oct-2014 16:54:02.840 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
31-Oct-2014 16:54:03.619 INFO [main] org.apache.catalina.core.AprLifecycleListener.init The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
31-Oct-2014 16:54:04.374 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8082"]
31-Oct-2014 16:54:04.431 INFO [main] org.apache.tomcat.util.net.NioselectorPool.getSharedSelector Using a shared selector for servlet write/read
31-Oct-2014 16:54:04.442 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8029"]
31-Oct-2014 16:54:04.457 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
31-Oct-2014 16:54:04.461 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1811 ms
31-Oct-2014 16:54:04.597 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
31-Oct-2014 16:54:04.601 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.14
31-Oct-2014 16:54:04.628 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat3/webapps/host-manager
shell > netstat -anpt | grep java # 现在 tomcat1/2/3 都已经全部启动
tcp 0 0 :::8080 :::* LISTEN 1381/java
tcp 0 0 :::8081 :::* LISTEN 1411/java
tcp 0 0 :::8082 :::* LISTEN 1441/java
tcp 0 0 :::8019 :::* LISTEN 1411/java
tcp 0 0 :::8029 :::* LISTEN 1441/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1381/java
tcp 0 0 ::ffff:127.0.0.1:8006 :::* LISTEN 1411/java
tcp 0 0 ::ffff:127.0.0.1:8007 :::* LISTEN 1441/java
tcp 0 0 :::8009 :::* LISTEN 1381/java

shell > curl "http://192.168.1.80:8080" # 全部可以访问
welcome to tomcat1
shell > curl "http://192.168.1.80:8081"
welcome to tomcat2
shell > curl "http://192.168.1.80:8082"
welcome to tomcat3
shell > tail -1 /usr/local/tomcat1/logs/catalina.out # 可以看到启动时间有长有短
31-Oct-2014 16:55:11.536 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 74454 ms

shell > tail -1 /usr/local/tomcat2/logs/catalina.out 
31-Oct-2014 16:58:30.971 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 269855 ms

shell > tail -1 /usr/local/tomcat3/logs/catalina.out
31-Oct-2014 16:56:10.965 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 126503 ms

shell > ps aux | grep java # 进程路径也都没有错
root 1381 1.3 8.4 2270676 86560 pts/0 Sl 16:53 0:06 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat1/endorsed -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root 1411 1.3 8.5 2270808 87144 pts/0 Sl 16:53 0:06 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat2/endorsed -classpath /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat2 -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
root 1441 1.4 8.6 2270836 87784 pts/0 Sl 16:54 0:06 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat3/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat3/endorsed -classpath /usr/local/tomcat3/bin/bootstrap.jar:/usr/local/tomcat3/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat3 -Dcatalina.home=/usr/local/tomcat3 -Djava.io.tmpdir=/usr/local/tomcat3/temp org.apache.catalina.startup.Bootstrap start
shell > /usr/local/tomcat2/bin/shutdown.sh # 现在单独关闭任何一个 别的都不受影响
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar

shell > ps aux | grep java
root 1381 1.2 8.4 2270676 86572 pts/0 Sl 16:53 0:06 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat1/endorsed -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root 1441 1.2 8.6 2270836 87804 pts/0 Sl 16:54 0:06 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat3/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat3/endorsed -classpath /usr/local/tomcat3/bin/bootstrap.jar:/usr/local/tomcat3/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat3 -Dcatalina.home=/usr/local/tomcat3 -Djava.io.tmpdir=/usr/local/tomcat3/temp org.apache.catalina.startup.Bootstrap start
root 1546 0.0 0.0 103248 864 pts/0 S+ 17:03 0:00 grep java

shell > netstat -anpt | grep java
tcp 0 0 :::8080 :::* LISTEN 1381/java
tcp 0 0 :::8082 :::* LISTEN 1441/java
tcp 0 0 :::8029 :::* LISTEN 1441/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1381/java
tcp 0 0 ::ffff:127.0.0.1:8007 :::* LISTEN 1441/java
tcp 0 0 :::8009 :::* LISTEN 1381/java

## 可以启动、关闭任意 tomcat ,没有顺序之分。唯一注意的一点,就是启动有一个过程,需要稍微等等,不要急。
只需修改端口即可,无需按照网上所说,要设置变量、更改 catalina.sh 等文件(如有不对的地方,还请各位指正)

以上是关于Tomcat ( 单机多 Tomcat 并存 )的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat 的单机多实例配置

Linux 下Tomcat单机多应用

tomcat 单机多实例

tomcat 单机多实例

Tomcat单机多实例配置

Web服务器群集——TOMCAT单机多实例配置