linux-nginx实现tomcat负载均衡给


设备

系统            IP                       主机名             软件               服务

centos7     10.0.0.17      web1.lyj.com    JDK8、tomcat      tomcat1

centos7     10.0.0.37      web2.lyj.com    JDK8、tomcat     tomcat2

centos7     10.0.0.27      proxy.lyj.com     nginx                    调度器

centos7      10.0.0.7        客户端

负载均衡tomcat主机准备

web1虚拟主机配置

[root@web1 ~]#vim /usr/local/tomcat/conf/server.xml
 "localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        
        

        
        "org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      

    
      #添加web1 引擎 ,必须添加引擎,才能实现负载均衡调度,否则默认访问localhost引擎,访问tomcat/webapps/ROOT/index.jsp,默认页面

web2虚拟主机配置

[root@web2 ~]#vim /usr/local/tomcat/conf/server.xml
 "localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        
        

        
        "org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      
    

    
      
      
    

准备负载均衡测试用jsp文件

web1和web2节点创建相同的文件

mkdir -pv /data/webapps/ROOT

jsp文件内容

[root@web1 ~]#vim /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>

"en">

   "UTF-8">
   tomcat test


On <%=request.getServerName() %>
<%=request.getLocalAddr() + ":" + request.getLocalPort() %>
SessionID = "color:blue"><%=session.getId() %>
<%=new Date()%>

启动tomcat服务或重启

[root@web2 ~]#systemctl start tomcat   #启动
或 [root@web2
~]#systemctl restart tomcat #重启

nginx实现后端tomcat的负载均衡调度

子配置文件 tomcat 专用  tomcat.conf

[root@proxy ~]#vim /usr/local/nginx/conf/conf.d/tomcat.conf 
upstream tomcat-server {
        server web1.lyj.com:8080;
        server web2.lyj.com:8080;
}
server {
        listen       80;
        server_name proxy.lyj.com;
        root    /usr/local/nginx/html;

        location ~* \.(jsp|do)$ {
           proxy_pass http://tomcat-server;
       }

}

域名解析,用hosts文件做域名解析

nginx服务器

[root@proxy ~]#vim /etc/hosts   #添加以下内容

10.0.0.17   web1.lyj.com
10.0.0.37   web2.lyj.com

客户端

[root@centos7-liyj ~]#vim /etc/hosts

10.0.0.27  prxoy.lyj.com

页面测试

[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp


"en">

   "UTF-8">
   <span style="color: rgba(255, 0, 0, 1)">web2 tomcat test</span>


On tomcat-server
10.0.0.37:8080
SessionID = "color:blue">6E153FEB589D130E91A66B340FCC4EB4
Sun Jun 19 19:04:17 CST 2022 [root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp "en"> "UTF-8"> <span style="color: rgba(255, 0, 0, 1)">web1 tomcat test</span>
On tomcat-server
10.0.0.17:8080
SessionID = "color:blue">6FFF0F78783A4C0EC6C4D3CD8C09A38C
Sun Jun 19 19:04:20 CST 2022 [root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp "en"> "UTF-8"> <span style="color: rgba(255, 0, 0, 1)">web2 tomcat test</span>
On tomcat-server
10.0.0.37:8080
SessionID = "color:blue">6EDBE50ACF1E8FA28310148B1A4DA4EA
Sun Jun 19 19:04:30 CST 2022 [root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp "en"> "UTF-8"> <span style="color: rgba(255, 0, 0, 1)">web1 tomcat test</span>
On tomcat-server
10.0.0.17:8080
SessionID = "color:blue">62C1B0BCAFE584F590DBC38DBD32FAAA
Sun Jun 19 19:04:34 CST 2022

实现session 黏性

在upstream中使用ip_hash指令,使用客户端i地址hash。

upstream tomcat-server {
        ip_hash;                         #添加IP地址hash算法
       
        server web1.lyj.com:8080;
        server web2.lyj.com:8080;
}
server {
        listen       80;
        server_name proxy.lyj.com;
        root    /usr/local/nginx/html;

        location ~* \.(jsp|do)$ {
           proxy_pass http://tomcat-server;
       }

}

10.0.0.7 客户端  测试

[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp


"en">

   "UTF-8">
   web2 tomcat test


On tomcat-server
10.0.0.37:8080
SessionID = "color:blue">FDB1EF9B00A712483A6BFDA7C59CC99D
Sun Jun 19 19:22:16 CST 2022 [root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp "en"> "UTF-8"> web2 tomcat test
On tomcat-server
10.0.0.37:8080
SessionID = "color:blue">DD60C243CF148A059CC8EE770A446088
Sun Jun 19 19:22:18 CST 2022 [root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp "en"> "UTF-8"> web2 tomcat test
On tomcat-server
10.0.0.37:8080
SessionID = "color:blue">CCCE7493697E7A70D8AD8CF5D38D1822
Sun Jun 19 19:22:20 CST 2022