Tomcat多实例配置与负载均衡

Tmocat多实例配置与负载均衡

多实例(多进程):同一个程序启动多次,分为两种情况:

第一种:一台机器跑多个站点;

第二种:一个机器跑一个站点多个实例,配合负载均衡

  1. 多实例配置
# 复制tomcat程序
[root@localhost local]# cp -r /usr/local/tomcat /usr/local/tomcat_2
# 修改监听端口
[root@localhost local]# sed -i 's#8005#8012#;s#8080#8082#' /usr/localtomcat_2/conf/server.xml
# 方便对比为tomcat_2创建发布页面
[root@localhost local]# vim /usr/local/tomcat_2/webapps/ROOT/index.jsp
tomcat_2
# 创建启动脚本
[root@localhost local]# vim/usr/local/tomcat_2/bin/start.sh
#!/bin/bash
#tomcat_2
export CATALINA_BASE="/usr/local/tomcat_2"

case "$1" in

start)
    $CATALINA_BASE/bin/startup.sh
    ;;
stop)
    $CATALINA_BASE/bin/shutdown.sh
esac
# 赋执行权
[root@localhost local]# chmod +x /usr/local/tomcat_2/bin/start.sh
# 添加环境变量
[root@localhost local]# vim /usr/local/tomcat_2/catalina.sh
CATALINA=/usr/local/tomcat_2
# 启动
[root@localhost local]# /usr/local/tomcat_2/bin/start.sh start
[root@localhost local]# netstat -lntp |grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      20186/java          
tcp6       0      0 :::8082                 :::*                    LISTEN      18845/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      20186/java          
tcp6       0      0 127.0.0.1:8012          :::*                    LISTEN      18845/java 

  1. 负载均衡
# 安装nginx
[root@localhost ~]# vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@localhost ~]# yum -y install nginx
# 负载均衡配置
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 
upstream tomcat{
     
        server 192.168.28.158:8080;
        server 192.168.28.158:8082;
}
server {
     
    listen       80;
    server_name  localhost;
    location / {
     
        proxy_pass http://tomcat;
    }
}
[root@localhost ~]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# nginx

访问代理服务器 192.168.28.155
Tomcat多实例配置与负载均衡_第1张图片
刷新,成功跳转
Tomcat多实例配置与负载均衡_第2张图片
在这里插入图片描述

nlnlznl

你可能感兴趣的:(Tomcat多实例配置与负载均衡)