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
 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

    </Engine>
    <Engine name="Catalina" defaultHost="web1.lyj.com">  #添加web1 引擎 ,必须添加引擎,才能实现负载均衡调度,否则默认访问localhost引擎,访问tomcat/webapps/ROOT/index.jsp,默认页面
<Host name="web1.lyj.com" appBase="/data/webapps" #添加web1 虚拟主机 unpackWARs="true" autoDeploy="true"> </Host> </Engine>

web2虚拟主机配置

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

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>

    <Engine name="Catalina" defaultHost="web2.lyj.com">
      <Host name="web2.lyj.com"  appBase="/data/webapps"
            unpackWARs="true" autoDeploy="true">
      </Host>
    </Engine>

 

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

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

mkdir -pv /data/webapps/ROOT

jsp文件内容

[root@web1 ~]#vim /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

启动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

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web2 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.37:8080</div>
<div>SessionID = <span style="color:blue">6E153FEB589D130E91A66B340FCC4EB4</span></div>
Sun Jun 19 19:04:17 CST 2022
</body>
</html>
[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web1 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.17:8080</div>
<div>SessionID = <span style="color:blue">6FFF0F78783A4C0EC6C4D3CD8C09A38C</span></div>
Sun Jun 19 19:04:20 CST 2022
</body>
</html>
[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web2 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.37:8080</div>
<div>SessionID = <span style="color:blue">6EDBE50ACF1E8FA28310148B1A4DA4EA</span></div>
Sun Jun 19 19:04:30 CST 2022
</body>
</html>
[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web1 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.17:8080</div>
<div>SessionID = <span style="color:blue">62C1B0BCAFE584F590DBC38DBD32FAAA</span></div>
Sun Jun 19 19:04:34 CST 2022
</body>
</html>

 

实现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

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web2 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.37:8080</div>
<div>SessionID = <span style="color:blue">FDB1EF9B00A712483A6BFDA7C59CC99D</span></div>
Sun Jun 19 19:22:16 CST 2022
</body>
</html>
[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web2 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.37:8080</div>
<div>SessionID = <span style="color:blue">DD60C243CF148A059CC8EE770A446088</span></div>
Sun Jun 19 19:22:18 CST 2022
</body>
</html>
[root@centos7-liyj ~]#curl http://proxy.lyj.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>web2 tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.37:8080</div>
<div>SessionID = <span style="color:blue">CCCE7493697E7A70D8AD8CF5D38D1822</span></div>
Sun Jun 19 19:22:20 CST 2022
</body>
</html>

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/268409.html

(0)
上一篇 2022年6月20日
下一篇 2022年6月20日

相关推荐

发表回复

登录后才能评论