Tomcat多实例及负载均衡

Posted elin菻

tags:

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

Tomcat多实例配置
==================面试==========
一般在在公司中一个实例的内存为4-8个G ,一个实例最高有400个并发量
一般来说一台机器最多10个左右实例,普遍设置8个实例
并发量:同一个时间,用户最多有多少访问量
例子:要处理20000的并发量,需要几台机器
20000/400=50(个实例) 50/10=5(台)nginx调度器需要2台(一主一备) 数据库需要2台 (一主一备)   并且要保证机器的内存要有64G才能够搞定2万的并发量
所以要处理2万的并发量至少要10台服务器
=================================

 

Tomcat多实例配置
应用程序需要部署在服务器上时Tomcat软件的部署方式可以分为以下几种:
单实例单应用(webapps/a)  ///只有一个Java进程,只提供一个页面(一个实例是一个进程)
单实例多应用(webapps/{a,b}) ///一个进程,多个页面
多实例单应用     ///多个进程跑一个服务页面
多实例多应用     ///
实例的概念可以先理解为一个Tomcat目录中的一个工作子目录
【1】单实例单应用:比较常用的一种方式,只需要把做好的war包丢在webapps 目录下,执行启动Tomcat的脚本就行了。
【2】单实例多应用有两个不同的Web项目的war包,还是只需要丢在webapps目录下,执行启动Tomcat 的脚本,访问不同项目会加载不同的WEB虚拟目录。这种方式在生产环境中要慎用,因为重启或挂掉Tomcat程序后会影响另外一个应用的访问。。
【3】多实例单应用:多 个Tomcat实例部署同一个项目,端口号不同,可以利用Nginx做负载均衡。
【4】多实例多应用, 多个Tomcat实例部署多个不同的项目。这种模式在服务器资源有限,或者对服务器资源要求并不是很高的情况下,可以实现多个不同项目部署在同一台服务器上的需求,来实现资源使用的最大化。。
=================================
a、解压并部署tomcat程序。
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# rm -rf /usr/local/tomcat8/ /web/    //把之前装的给卸掉重新安装tomcat
[root@localhost ~]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.40 /usr/local/
[root@localhost ~]# ls /usr/local/
apache-tomcat-8.5.40  games    lib      nginx  src
bin                   include  lib64    sbin
etc                   java     libexec  share
b、创建两个实例的工作目录
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat1
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat2
c、拷贝tomcat程序目录下的conf 目录分别放入2个实例目录中
[root@localhost ~]# cp -R /usr/local/apache-tomcat-8.5.40/conf/ /usr/local/tomcat_instance/tomcat1
[root@localhost ~]# cp -R /usr/local/apache-tomcat-8.5.40/conf/ /usr/local/tomcat_instance/tomcat2
d、在tomcat实例目录下的bin目录中创建实例启动和停止脚本
启动脚本
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat1/bin
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat2/bin
[root@localhost ~]# touch /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# touch /usr/local/tomcat_instance/tomcat2/bin/startup.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/startup.sh
[root@localhost ~]# ls /usr/local/tomcat_instance/
tomcat1  tomcat2
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat1
bin  conf
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat1/conf/
catalina.policy       logging.properties
catalina.properties   server.xml
context.xml           tomcat-users.xml
jaspic-providers.xml  tomcat-users.xsd
jaspic-providers.xsd  web.xml
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat1/bin/
startup.sh
[root@localhost ~]# ls /usr/local/tomcat_instance/tomcat2/bin/
startup.sh
··································
修改startup.sh 文件
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/startup.sh
#!/bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1"
#创建logs目录
if [ ! -d "$CATALINA_BASE/logs" ]
then
        mkdir $CATALINA_BASE/logs
fi
#创建temp目录
if [ ! -d "$CATALINA_BASE/temp" ]
then
        mkdir $CATALINA_BASE/temp
fi
#调用tomcat启动脚本
bash $CATALINA_HOME/bin/startup.sh "$@"
保存退出
[root@localhost ~]# cp /usr/local/tomcat_instance/tomcat1/bin/startup.sh /usr/local/tomcat_instance/tomcat2/bin/
cp:是否覆盖"/usr/local/tomcat_instance/tomcat2/bin/startup.sh"? y
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/bin/startup.sh
#!/bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat2"   //修改为2
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2"  //修改为2
#创建logs目录
if [ ! -d "$CATALINA_BASE/logs" ]
then
        mkdir $CATALINA_BASE/logs
fi
#创建temp目录
if [ ! -d "$CATALINA_BASE/temp" ]
then
        mkdir $CATALINA_BASE/temp
fi
#调用tomcat启动脚本
bash $CATALINA_HOME/bin/startup.sh "$@"
保存退出
·······························
修改shutdown.sh 文件
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh
#!/bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat2"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
bash $CATALINA_HOME/bin/shutdown.sh "$@"
保存退出
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh
#!/bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
bash $CATALINA_HOME/bin/shutdown.sh "$@"
保存退出
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh
······································
修改server.xml 文件
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/conf/server.xml
这个端口设定为8005 不做改变
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/conf/server.xml
//修改以下3个的端口号
 22 <Server port="8006" shutdown="SHUTDOWN">
 69     <Connector port="8090" protocol="HTTP/1.1"
116     <Connector port="8010" protocol="AJP/1.3" redi    rectPort="8443" />
保存退出
··········································
因为配置文件中指定的路径是 webapps/ROOT
[root@localhost ~]# mkdir -p  /usr/local/tomcat_instance/tomcat1/webapps/ROOT
[root@localhost ~]# mkdir -p  /usr/local/tomcat_instance/tomcat2/webapps/ROOT
···········································
写测试页面
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/webapps/ROOT/index.jsp
<html>
<title>Tomcat-1</title>
<body>
        hello ,This is Tomcat-1.
</body>
</html>
保存退出
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/webapps/ROOT/index.jsp
<html>
<title>Tomcat-2</title>
<body>
        hello ,This is Tomcat-2.
</body>
</html>
保存退出
~~~~~~~~~~~~~~~~~~~~~·2个实例制作完成~~~~~~~~~~~~~~~
启动实例
[root@localhost ~]# /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# /usr/local/tomcat_instance/tomcat2/bin/startup.sh
[root@localhost ~]# netstat -lnpt |grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      16547/java         
tcp6       0      0 :::8090                 :::*                    LISTEN      16601/java         
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      16547/java         
tcp6       0      0 127.0.0.1:8006          :::*                    LISTEN      16601/java         
tcp6       0      0 :::8009                 :::*                    LISTEN      16547/java         
tcp6       0      0 :::8010                 :::*                    LISTEN      16601/java
···················
在浏览器中进行测试 192.168.200.66:8080   192.168.200.66:8090

 

 ======做第3个实例======
//复制tomcat2 的内容,并把相关文件做修改
[root@localhost ~]# cd /usr/local/tomcat_instance/
[root@localhost tomcat_instance]# ls
tomcat1  tomcat2
[root@localhost tomcat_instance]# cp -R tomcat2 tomcat3
[root@localhost tomcat_instance]# ls
tomcat1  tomcat2  tomcat3
[root@localhost tomcat_instance]# cd tomcat3
[root@localhost tomcat3]# ls
bin  conf  logs  temp  webapps  work
[root@localhost tomcat3]# vim bin/
shutdown.sh  startup.sh   tomcat.pid
~···~···修改  shudown.sh~···~
[root@localhost tomcat3]# vim bin/shutdown.sh
3 export CATALINA_BASE="/usr/local/tomcat_instance/tomcat3"
保存退出
~~····修改startup.sh~~····~·~
[root@localhost tomcat3]# vim bin/startup.sh
 3 export CATALINA_BASE="/usr/local/tomcat_instance/tomcat3"
  6 export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat3"
保存退出
[root@localhost tomcat3]# rm -rf bin/tomcat.pid
·····修改conf文件
[root@localhost tomcat3]# vim conf/server.xml
 22 <Server port="8004" shutdown="SHUTDOWN">
 69     <Connector port="8070" protocol="HTTP/1.1"
116     <Connector port="8008" protocol="AJP/1.3" redirectPort="8443" />
保存退出
~````修改webapps ..`````
[root@localhost tomcat3]# vim webapps/ROOT/index.jsp
<html>
<title>Tomcat-3</title>
<body>
        hello ,This is Tomcat-3.
</body>
</html>
保存退出
~~~~~启动~~~~~
[root@localhost tomcat3]# /usr/local/tomcat_instance/tomcat3/bin/startup.sh
[root@localhost tomcat3]# netstat -lnpt |grep java
tcp6       0      0 127.0.0.1:8004          :::*                    LISTEN      17358/java
tcp6       0      0 :::8070                 :::*                    LISTEN      17358/java
tcp6       0      0 :::8008                 :::*                    LISTEN      17358/java  
在浏览器中进行测试:192.168.200.66:8070

 
 
 
 ~~~~~让nginx调度多台机器~~~~~~
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 31     keepalive_timeout  65;
 32         upstream tomcat_pool{
 33                 server 192.168.200.66:8070 weight=    1;          
 34                 server 192.168.200.66:8080 weight=    1; 
 35                 server 192.168.200.66:8090 weight=    1;
 36         }
保存退出
[root@localhost ~]# nginx -t
[root@localhost ~]# killall -HUP nginx
 
 

 

 

 

 

注意CATALINA_BASE所指向的tomcat实例目录路径
~~

~~~~~~~

~~~~~~~~·完成实验!!!!! !!!!!!!!!!!!!!!!!!!

以上是关于Tomcat多实例及负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat多实例和负载均衡

Tomcat多实例和负载均衡

必须要掌握的开源Web应用服务器—Tomcat多实例和负载均衡

tomcat 多实例部署以及负载均衡配置

tomcat的多实例部署和负载均衡

Nginx 配置实例-配置负载均衡