2019.9.21 Tomcat多实例及负载均衡
如上图所示实例的多分配,每个实例(假设可以连接400个用户)对应一个java,那4个java和4给实例就是1600的用户,相比一台服务器就一个实例的话,这样可以尽可能的充分运用硬件资源并合理应用。如果并发2万的用户的话至少需要9台左右的服务器,才可以容纳,5台tomcat服务器,2台nginx调度器服务器,2台数据库服务器。
一,Tomcat多实列的配置
1,解压并部署tomcat程序
[root@localhost ~]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.40 /usr/local/
2,创建2个实例的工作目录
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat1
[root@localhost ~]# mkdir -p /usr/local/tomcat_instance/tomcat2
3,拷贝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/
4,在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/tomcat2//bin/startup.sh
[root@localhost ~]# touch /usr/local/tomcat_instance/tomcat1//bin/startup.sh
5,给开启脚本权限
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/startup.sh
6,编辑第一个实例开启脚本
[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/startup.sh //cp脚本到第二个实例下面去
cp:是否覆盖"/usr/local/tomcat_instance/tomcat2/bin/startup.sh"? y
7,编辑第二个实例开启脚本
[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"
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"
#创建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 "$@"
8,编辑第一个关闭实例脚本
[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"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1"
#调用tomcat启动脚本
bash $CATALINA_HOME/bin/shutdown.sh "$@"
9,编辑第二个关闭脚本
[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"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2"
#调用tomcat启动脚本
bash $CATALINA_HOME/bin/shutdown.sh "$@"
10,给关闭脚本权限
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh //给关闭脚本权限
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat2/bin/shutdown.sh //给关闭脚本权限
11,修改主配置文件
注;修改每个tomcat实例中server.xml 中的端口(分别修改以上三个端口(Server port,Connector port,AJP)),不要和其他实例的端口或系统已经占用的端口发送冲突)。
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat2/conf/server.xml //修改以上红色字体所说的3个端口 注意;不要和其他实例的端口或系统已经占用的端口发送冲突 这个是我做实验时修改的8006 8090 8010
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/conf/server.xml //修改以上红色字体所说的3个端口 注意:不要和其他实例的端口或系统已经占用的端口发送冲突 这个是我做实验时修改的8805 8080 8009
12,创建测试页
[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>
13,启动实例脚本
[root@localhost ~]# /usr/local/tomcat_instance/tomcat1/bin/startup.sh //启动实例脚本
[root@localhost ~]# /usr/local/tomcat_instance/tomcat2/bin/startup.sh //启动实例脚本
14,监听两个实例是否都开启端口
[root@localhost ~]# netstat -lnpt |grep java
tcp6 0 0 :::8080 :::* LISTEN 10451/java
tcp6 0 0 :::8090 :::* LISTEN 10505/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 10451/java
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 10505/java
tcp6 0 0 :::8009 :::* LISTEN 10451/java
tcp6 0 0 :::8010 :::* LISTEN 10505/java
15,接下来就是测试能否访问到测试页的内容了
16,红色的字体这个是nginx服务器配置服务均衡的命令
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream tomcat_server {
server 192.168.200.114:8080 weight=1 max_fails=3 fail_timeout=10s; //定义负载均衡的池组
server 192.168.200.114:8090 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; //应用负载均衡的池组
proxy_set_header Host $http_host;
}
# location ~* \.jsp$ {
# proxy_pass http://tomcat_server;
# proxy_set_header Host $http_host;
# }
测试结果如下;
——————————————————————————————————————————————————————————————————————————————————————————————————————————
如果我们要创建第3个实例的话步骤如下;
[root@localhost ~]# cd /usr/local/tomcat_instance/ //进入到实例存放位置
[root@localhost tomcat_instance]# ls
tomcat1 tomcat2
[root@localhost tomcat_instance]# cp -r tomcat2 tomcat3 //通过cp添加第3个实例
[root@localhost tomcat_instance]# ls
tomcat1 tomcat2 tomcat3
[root@localhost tomcat_instance]# cd tomcat3 //查看第3个实例的所有配置文件
[root@localhost tomcat3]# ls
bin conf logs startup.sh temp webapps work
[root@localhost tomcat3]# cd bin/
[root@localhost bin]# ls
shutdown.sh startup.sh tomcat.pid //红色这个是存进程号的,现在没有进程号,所有必须要删除它
[root@localhost bin]# rm -rf tomcat.pid //删除存放进程号的文件
[root@localhost bin]# vim shutdown.sh //编辑关闭脚本
#!/bin/bash
export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat3"
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=tomcat3"
#调用tomcat启动脚本
bash $CATALINA_HOME/bin/shutdown.sh "$@"
[root@localhost bin]# vim startup.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"
export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat2"
#创建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 tomcat3]# ls
bin conf logs startup.sh temp webapps work
[root@localhost tomcat3]# vim conf/server.xml //编辑第3个实例的主配置文件
注;修改每个tomcat实例中server.xml 中的端口(分别修改以上三个端口(Server port,Connector port,AJP)),不要和其他实例的端口或系统已经占用的端口发送冲突)。
我修改的是8004 8070 8008
[root@localhost tomcat3]# vim webapps/ROOT/index.jsp //修改第3个测试网页
<html>
<title>Tomcat-3</title>
<body>
Hello This is Tomcat-3.
</body>
</html>
[root@localhost tomcat3]# /usr/local/tomcat_instance/tomcat3/bin/shutdown.sh //关闭第3给实例脚本
[root@localhost tomcat3]# /usr/local/tomcat_instance/tomcat3/bin/startup.sh //开启第3个实例脚本
之后就是测试第3个实例的网页了
最后也需要在nginx上面加入负载均衡这个环境,修改nginx的主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //修改主配置文件
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream tomcat_server {
server 192.168.200.114:8070 weight=1 max_fails=3 fail_timeout=10s; //定义负载均衡池组
server 192.168.200.114:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.200.114:8090 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; //引用负载均衡池组
proxy_set_header Host $http_host;
}
# location ~* \.jsp$ {
# proxy_pass http://tomcat_server;
# proxy_set_header Host $http_host;
# }
[root@localhost ~]# killall -HUP nginx //最后重新加载nginx
最后测试nginx上面的负载均衡