前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tomcat双端口基于nginx实现负载均衡

tomcat双端口基于nginx实现负载均衡

作者头像
冷影玺
发布2023-10-12 14:00:23
1750
发布2023-10-12 14:00:23
举报
文章被收录于专栏:冷影玺冷影玺

上述配置完成之后进行对tomcat配置不同端口

tomcat设置端口

代码语言:javascript
复制
# 移动tomcat设置两个主目录
[root@lyx local]# mv apache-tomcat-8.5.79 tomcat-home
[root@lyx local]# cp -R tomcat-home tomcat-8080
[root@lyx local]# cp -R tomcat-home tomcat-8090

进行修改对应端口8080默认不变修改8090:

代码语言:javascript
复制
[root@lyx local]# vi /usr/local/tomcat-8090/conf/server.xml

...
<Server port="8015" shutdown="SHUTDOWN">
...
<Connector port="8090" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
...
tomcat双端口基于nginx实现负载均衡_nginx
tomcat双端口基于nginx实现负载均衡_nginx
tomcat双端口基于nginx实现负载均衡_jar_02
tomcat双端口基于nginx实现负载均衡_jar_02

进入到tomcat-8080目录下创建一个启动的shell脚本和一个停止的shell脚本:

代码语言:javascript
复制
[root@lyx tomcat-8080]# cat start.sh
#!/bin/sh
CUR_DIR=`dirname $BASH_SOURCE`
export CATALINA_BASE=`readlink -f $CUR_DIR`
export CATALINA_HOME="/usr/local/tomcat-home"

if [ -f $CATALINA_HOME/bin/startup.sh ]; then
        $CATALINA_HOME/bin/startup.sh
else
        echo "$CATALINA_HOME/bin/startup.sh not exist"
fi
[root@lyx tomcat-8080]#
# 然后添加权限
[root@lyx tomcat-8080]# chmod +x start.sh
代码语言:javascript
复制
[root@lyx tomcat-8080]# cat stop.sh
#!/bin/sh
CUR_DIR=`dirname $BASH_SOURCE`
export CATALINA_BASE=`readlink -f $CUR_DIR`
export CATALINA_HOME="/usr/local/tomcat-home"

if [ -f $CATALINA_HOME/bin/shutdown.sh ]; then
       $CATALINA_HOME/bin/shutdown.sh
else
       echo "$CATALINA_HOME/bin/shutdown.sh not exist"
fi
[root@lyx tomcat-8080]#
# 然后添加权限
[root@lyx tomcat-8080]# chmod +x stop.sh

同样进入到tomcat-8090目录下创建一个启动的shell脚本和一个停止的shell脚本:

代码语言:javascript
复制
[root@lyx tomcat-8090]# cat start.sh
#!/bin/sh
CUR_DIR=`dirname $BASH_SOURCE`
export CATALINA_BASE=`readlink -f $CUR_DIR`
export CATALINA_HOME="/usr/local/tomcat-home"

if [ -f $CATALINA_HOME/bin/startup.sh ]; then
        $CATALINA_HOME/bin/startup.sh
else
        echo "$CATALINA_HOME/bin/startup.sh not exist"
fi
[root@lyx tomcat-8090]#
# 然后添加权限
[root@lyx tomcat-8090]# chmod +x start.sh
代码语言:javascript
复制
[root@lyx tomcat-8090]# cat stop.sh
#!/bin/sh
CUR_DIR=`dirname $BASH_SOURCE`
export CATALINA_BASE=`readlink -f $CUR_DIR`
export CATALINA_HOME="/usr/local/tomcat-home"

if [ -f $CATALINA_HOME/bin/shutdown.sh ]; then
       $CATALINA_HOME/bin/shutdown.sh
else
       echo "$CATALINA_HOME/bin/shutdown.sh not exist"
fi
[root@lyx tomcat-8090]#
# 然后添加权限
[root@lyx tomcat-8090]# chmod +x stop.sh

对tomcat-8080进行启动:

代码语言:javascript
复制
[root@lyx tomcat-8080]# ./start.sh
Using CATALINA_BASE:   /usr/local/tomcat-8080
Using CATALINA_HOME:   /usr/local/tomcat-home
Using CATALINA_TMPDIR: /usr/local/tomcat-8080/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat-home/bin/bootstrap.jar:/usr/local/tomcat-8080/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

对tomcat-8090进行启动:

代码语言:javascript
复制
[root@lyx tomcat-8090]# ./start.sh
Using CATALINA_BASE:   /usr/local/tomcat-8090
Using CATALINA_HOME:   /usr/local/tomcat-home
Using CATALINA_TMPDIR: /usr/local/tomcat-8090/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat-home/bin/bootstrap.jar:/usr/local/tomcat-9090/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

对应访问:

tomcat双端口基于nginx实现负载均衡_jar_03
tomcat双端口基于nginx实现负载均衡_jar_03
tomcat双端口基于nginx实现负载均衡_nginx_04
tomcat双端口基于nginx实现负载均衡_nginx_04

修改8090端口网页内容为tomcat-8090而8080端口为hello依然为猫作为对比测试:

代码语言:javascript
复制
[root@lyx local]# cd tomcat-8090/
[root@lyx tomcat-8090]# cd webapps/ROOT/
[root@lyx ROOT]# cp -a index.jsp index1.jsp
[root@lyx ROOT]# rm -rf index.jsp
[root@lyx ROOT]# cat index.jsp
tomcat-9090
[root@lyx ROOT]#



[root@lyx ~]# cd /usr/local/tomcat-8080/
[root@lyx tomcat-8080]# cat webapps/ROOT/index.jsp
hello
[root@lyx tomcat-8080]#

进行刷新网页查看:

tomcat双端口基于nginx实现负载均衡_jar_05
tomcat双端口基于nginx实现负载均衡_jar_05
tomcat双端口基于nginx实现负载均衡_tomcat_06
tomcat双端口基于nginx实现负载均衡_tomcat_06

注意:如果操作完之后会出现显示一个端口那么就是端口进行冲突需要进行修改环境变量,正常访问则无需修改:

代码语言:javascript
复制
# 编辑
vim /etc/profile

# 添加到最后

TOMCAT_HOME=/usr/local/tomcat-8080/
CATALINA_HOME=/usr/local/tomcat-8080/
CATLINA_BASE=/usr/local/tomcat-8080
export TOMCAT_HOME  CATALINA_HOME  CATALINA_BASH


TOMCAT_HOME_1=/usr/local/tomcat-8090
CATALINA_HOME_1=/usr/local/tomcat-8090
CATLINA_BASE_1=/usr/local/tomcat-8090
export TOMCAT_HOME_1  CATALINA_HOME_1  CATALINA_BASH_1

# 进行加载环境变量
source /etc/profile   #这个是一定要做的

# 然后进行修改tomcat的bin目录下catalina.sh

[root@lyx tomcat-8090]# vi bin/catalina.sh
# 添加
export CATALINA_HOME=$CATALINA_HOME_1
export CATALINA_HOME=$CATALINA_HOME_1

负载均衡——代理

进行配置nginx:

代码语言:javascript
复制
[root@lyx nginx-1.21.6]# vi /usr/local/nginx/conf/nginx.conf

# 添加:
     upstream tomcatserver{
        server 192.168.88.88:8080 weight=20;
        server 192.168.88.88:8090 weight=10;
}
tomcat双端口基于nginx实现负载均衡_tomcat_07
tomcat双端口基于nginx实现负载均衡_tomcat_07
tomcat双端口基于nginx实现负载均衡_nginx_08
tomcat双端口基于nginx实现负载均衡_nginx_08
代码语言:javascript
复制
# 停止nginx
[root@lyx ~]# /usr/local/nginx/sbin/nginx -s stop

# 启动nginx
[root@lyx ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

访问查看:

tomcat双端口基于nginx实现负载均衡_tomcat_09
tomcat双端口基于nginx实现负载均衡_tomcat_09

再次刷新:

tomcat双端口基于nginx实现负载均衡_nginx_10
tomcat双端口基于nginx实现负载均衡_nginx_10

负载均衡——基于四层tcp/udp

代码语言:javascript
复制
[root@lyx ~]# vi /usr/local/nginx/conf/nginx.conf
tomcat双端口基于nginx实现负载均衡_nginx_11
tomcat双端口基于nginx实现负载均衡_nginx_11
代码语言:javascript
复制
# 停止nginx
[root@lyx ~]# /usr/local/nginx/sbin/nginx -s stop

# 启动nginx
[root@lyx ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

进行访问:一直不变

tomcat双端口基于nginx实现负载均衡_nginx_12
tomcat双端口基于nginx实现负载均衡_nginx_12

负载均衡——Session:根据cookie

代码语言:javascript
复制
[root@lyx ~]# vi /usr/local/nginx/conf/nginx.conf
tomcat双端口基于nginx实现负载均衡_jar_13
tomcat双端口基于nginx实现负载均衡_jar_13

编译环境:

nginx1.8.1---密码lyx

代码语言:javascript
复制
# 上传并且解压1.8.1 并且sticky也在此执行
[root@lyx ~]# cd /server/tools/
[root@lyx tools]# ll
总用量 940
drwxr-xr-x 9 1001 1001    230 4月  12 09:27 nginx-1.8.1
-rw-r--r-- 1 root root 833473 4月  12 09:20 nginx-1.8.1.tar.gz
-rw-r--r-- 1 root root 124138 4月  12 09:14 nginx-sticky-module-ng
# 下载Sticky
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz

# 进行解压
tar -xvf master.tar.gz

#重命名为nginx-goodies-nginx-sticky-module-ng

mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 /opt/nginx-goodies-nginx-sticky-module-ng

#进入nginx目录进行安装编译
[root@localhost nginx-1.8.1]# ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/server/tools/nginx-goodies-nginx-sticky-module-ng/


# 源码编译
make && make install ,一定要编译安装,否则不生效

# 查看模块
/usr/local/nginx/sbin/nginx -V


useradd -M -s /sbin/nologin nginx

#启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 负载均衡——代理
  • 负载均衡——基于四层tcp/udp
  • 负载均衡——Session:根据cookie
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档