tomcat3----进阶配置

tomcat进阶配置

  • session保持
    • 调度器
      • session sticky 会话粘性
        • source ip 源地址哈希
        • Cookice LVS不支持,nginx商业版支持,httpd和haproxy支持
    • UpStream server
      • session replication cluster 会话复制集群,多个后端服务器定义一个集群,每个节点获取的session都通过集群的专用信道送给同一集群的其他主机,其他主机可以接收到其他任意发送过来的session并整合到本地session中,因此,对于非常繁忙的后端服务组来讲,每个节点每时每刻都在更新大量的session,这样主机会变的很繁忙,所以不适用特别大规模或任务繁重的场景,但这是一个很好的解决方案(每一个后端服务器都做成单点,若服务器单点挂了,那么则session都会丢失,这样,使用会话复制集群,重新调度就可以了)
         - session server需要选择一台服务器以进行 session会话保留,共享 session并允许其他后端服务主机可访问,还需要考虑一些问题,冗余和扩展性,避免单一故障

示例 nginx反向代理 --> tomcata,tomcatb

启动两个tomcat容器

docker run -d --name tomcata -v /data/tomcata:/usr/local/tomcat/webapps/ROOT/ tomcat:8.5.70-jdk8
docker run -d --name tomcatb -v /data/tomcatb:/usr/local/tomcat/webapps/ROOT/ tomcat:8.5.70-jdk8

创建两个tomcat首页,显示session信息

[root@final ~]# cat /data/tomcata/index.jsp 
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
        <head><title>JSP Test Page</title></head>
        <body>
        <h1><font color="red">TomcatA.test.com</font></h1>
        <table align="centre"border="1">
        <tr>
        <td>Session ID</td>
        <% session.setAttribute("test.com","test.com"); %>
        <td><%=session.getId() %></td></tr>
        <tr>
        <td>Created on</td>
        <td><%=session.getCreationTime() %></td>
        </tr>
</table>
</body>
</html>

[root@final ~]# cat /data/tomcatb/index.jsp  
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
        <head><title>JSP Test Page</title></head>
        <body>
        <h1><font color="blue">TomcatB.test.com</font></h1>
        <table align="centre"border="1">
        <tr>
        <td>Session ID</td>
        <% session.setAttribute("test.com","test.com"); %>
        <td><%=session.getId() %></td></tr>
        <tr>
        <td>Created on</td>
        <td><%=session.getCreationTime() %></td>
        </tr>
</table>
</body>
</html>

使用nginx反代连接后端两个tomcat容器

upstream tomcats {
    server 172.17.0.2:8080;
    server 172.17.0.3:8080;
}

location / {
	proxy_pass http://tomcats/;
}

测试访问

[root@final ~]# while true; do curl -s http://192.168.10.2 | tr ‘<>/‘ ‘ ‘ | grep -v ‘^$‘ | awk ‘/td/{print $2}‘ | head -2 ;sleep 1;done
Session
3C808C01CA43BF9AF1902C17F0B87AA8
Session
F910D10922F7CCFE7D04B59291773329
Session
99281DE4F28BC1F8F23413797552593E
Session
F7DD5D2AD540DC0F1FD8FFC3B8B514B5

可以观察到每次访问记录的session都不一样

示例 httpd反向代理 --> tomcata,tomcatb

查看模块

[root@final data]# httpd -M | grep -E "lbmethod|proxy"
 proxy_module (shared)
 lbmethod_bybusyness_module (shared)
 lbmethod_byrequests_module (shared)
 lbmethod_bytraffic_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_http_module (shared)

配置httpd

[root@final conf.d]# cat http_load_balance.conf
<Proxy balancer://tomcats>
    BalancerMember http://172.17.0.2:8080
    BalancerMember http://172.17.0.3:8080
    ProxySet lbmethod=byrequests	#此处定义的是负载均衡算法,轮询RR
</Proxy>

<VirtualHost *:80>                       
    ServerName node1.final.com
    ProxyRequests Off
    ProxyVia On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
        ProxyPass / balancer://tomcats/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

客户端测试访问

while true; do curl -s http://192.168.10.2 | tr ‘<>/‘ ‘ ‘ | grep -v ‘^$‘ | awk ‘/td/{print $2}‘ | head -2 ;sleep 1;done
Session
A07D064E71E351A22C4486419ADBEBF9
Session
F99DC7520BE5EAFBDA26624055B24A37
Session
A4C44FE396F1B968B5449A86778A9ACD
Session
9B02F5FDDD477F465F60305A01307796

示例 httpd反向代理做会话前粘性

修改httpd配置文件

[root@final conf.d]# cat http_load_balance.conf 
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<Proxy balancer://tomcats>
    BalancerMember http://172.17.0.2:8080 route=tca
    BalancerMember http://172.17.0.3:8080 route=tcb
    ProxySet lbmethod=byrequests
    ProxySet stickysession=ROUTEID
</Proxy>

<VirtualHost *:80>                       
    ServerName node1.final.com
    ProxyRequests Off
    ProxyVia On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
        ProxyPass / balancer://tomcats/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

客户端访问

tomcat3----进阶配置

使用curl请求

若只设置ROUTEID,页面不会改变,session会改变

先访问一次记录下COOKIE和ROUTEID的值

curl -b "JSESSIONID=70837D83AE4F91F159695CAF2EAE05C5;ROUTEID=.tcb" http://192.168.10.2/ -s | tr ‘<>/‘ ‘ ‘ | grep -v ‘^$‘ | awk ‘/td/{print $2}‘ | head -2

示例 httpd使用管理页面

配置httpd

[root@final conf.d]# cat http_load_balance.conf 
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<Proxy balancer://tomcats>
    BalancerMember http://172.17.0.2:8080 route=tca
    BalancerMember http://172.17.0.3:8080 route=tcb
    ProxySet lbmethod=byrequests
    ProxySet stickysession=ROUTEID
</Proxy>

<VirtualHost *:80>                       
    ServerName node1.final.com
    ProxyRequests Off
    ProxyVia On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
        ProxyPass / balancer://tomcats/
    <Location />
        Require all granted
    </Location>
    <Location /manager>
        sethandler balancer-manager
        proxypass !
        Require all granted
        #Require ip 127.1
   </Location> 
</VirtualHost>

客户测试访问

tomcat3----进阶配置

tomcat3----进阶配置

上一篇:C# 编码规范、命名规则总结


下一篇:利用API 建立Dependent Value Set