使用Nginx实现多个Tomcat负载均衡

2023年6月2日10:09:11

第一步. 安装多个Tomcat,用来集群:

安装多个Tomcat的步骤,在这篇博客里面:https://blog.csdn.net/a1422655169/article/details/115360274?spm=1001.2014.3001.5501

第二步. 安装Nginx:

安装Nginx的步骤我放在这篇博客里面了:https://blog.csdn.net/a1422655169/article/details/115361095?spm=1001.2014.3001.5501

第三步. 编写shell脚本:

这里使用的是编写shell脚本的方式来处理。
打开编译:vi /etc/init.d/nginx (输入下面的代码)

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/myfile/sbin/nginx
nginx_config=/usr/myfile/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

编译好之后先按esc键,然后 :wq 保存并退出。

第四步. 设置文件访问权限:

命令:chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
  这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
使用Nginx实现多个Tomcat负载均衡
  如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

  上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。

先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx

  加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start
service nginx stop

第五步. 加入到rc.local文件中

1. 使用命令:vi /etc/rc.local 进入此文件,并在此文件中加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
2.ESC:wq

第六步. 实现负载均衡:

==在/etc/profile 下进行三个Tomcat的配置,==详情见博客:Linux虚拟机安装多个Tomcat
修改三个Tomcat的端口 8080 8081 8082

第七步. 修改/usr/local/ngin/conf/nginx.conf

配置文件的具体位置可以在nginx启动的情查看进程看:
使用Nginx实现多个Tomcat负载均衡
添加的代码如下:

upstream mynginx {  #集群地址
    server  127.0.0.1:8080 weight=100;#单个服务地址及其权重,这个权重是默认负载均衡算法的值,越大越容易被访问
server  127.0.0.1:8081 weight=50;
server  127.0.0.1:8082 weight=10;
}   
#修改server的配置,在其中添加如下代码:
server {
        listen       8888;#nginx端口
        server_name  localhost;#host name

        location / {
           proxy_pass http://mynginx;#代理指向
           # proxy_redirect default;
        }
}

使用Nginx实现多个Tomcat负载均衡

第八步. 启动测试:

1. 启动三个Tomcat
2. 启动nginx
测试: 网站输入linux的ip:8888就会发现Tomcat1,Tomcat2,Tomcat3之前相互切换,且Tomcat的次数最多


同一个项目分别上传至三个Tomcat/webapps下,启动Tomcat,启动nginx.访问nginx的ip和端口。

  • 作者:小郑要做干饭人
  • 原文链接:https://blog.csdn.net/a1422655169/article/details/115363156
    更新时间:2023年6月2日10:09:11 ,共 2701 字。