tomcat的session問題

    記錄客戶端操做咱們通常使用cookie進行客戶身份識別,session存儲客戶端的信息,session保存在服務器端。當咱們負載均衡服務後,客戶端請求定位到另一臺服務器,session信息在另一臺服務器上,爲保證咱們能夠記錄客戶端進行的操做,咱們通常有三種解決方式css

     第一種,session綁定,但願同一客戶端的請求發往同一服務器,這樣會破壞負載均衡效果。這種解決方法雖然有缺陷可是也是一種解決方法。html

     第二種,session cluster,構建一個session保存集羣,提供同類型服務的服務器都把session共享,每一個服務器一份。java

     第三種,session server,準備一個服務器保存session,而後其餘服務器都到session服務器上取。nginx

     接下來,分佈介紹他們怎麼實現web

環境準備apache

     準備三臺主機,他們之間的關係圖如圖1.1vim

spacer.gifwKioL1iemXTDzxyMAAA7syPAZcM125.png

圖1.1tomcat

TomcatA的安裝腳本bash

yum install tomcat tomcat-webapps tomcat-lib tomcat-admin-webapps
mkdir -pv /var/lib/tomcat/webapps/test/WEB-INF/{classes,lib}
cat > /var/lib/tomcat/webapps/test/index.jsp <<eof
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
eof

還要配置Tomcat服務器

vim /etc/tomcat/server.xml
#在host配置段中添加以下一行
<Context path="/test" appBase="/var/lib/tomcat/webapps/test/" reloadable="true"/>
#更改這一行
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">

TomcatB的安裝腳本

yum install tomcat tomcat-webapps tomcat-lib tomcat-admin-webapps
mkdir -pv /var/lib/tomcat/webapps/test/WEB-INF/{classes,lib}
cat > /var/lib/tomcat/webapps/test/index.jsp <<eof
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.magedu.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
eof


還要配置Tomcat

vim /etc/tomcat/server.xml
#在host配置段中添加以下一行
<Context path="/test" appBase="/var/lib/tomcat/webapps/test/" reloadable="true"/>
#更改這一行
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">

session綁定的實現方法

     session綁定的實現方法有三種,

第一種

使用Nginx的作反向代理,添加以下內容就可

vim /etc/nginx/nginx.conf
#在http配置段中添加一個
    upstream tomcat {
        server 172.16.29.10:8080;
        server 172.16.29.20:8080;
        ip_hash;
    }
#在這個location調用upstream定義的負載均衡的兩個服務器
        location / {
            proxy_pass http://tomcat;
        }


第二種

使用http的http反向代理模塊,這裏的會話綁定使用的是cookie

vim /etc/httpd/conf.d/tc.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://tomcat>
    BalancerMember  http://172.16.29.10:8080 route=TomcatA loadfactor=1
    BalancerMember  http://172.16.29.20:8080 route=TomcatB loadfactor=1
    ProxySet  lbmethod=byrequests
    ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
    ServerName      tc
    ProxyRequests Off
    ProxyVia        On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / balancer://tomcat:8080/
    ProxyPa***everse / balancer://tomcat:8080/
    <Location />
        Require all granted
    </Location>
</VirtualHost>


第三種,使用http的ajp反向代理模塊

vim /etc/httpd/conf.d/tc.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://tomcat>
    BalancerMember  ajp://172.16.29.10:8009 route=TomcatA loadfactor=1
    BalancerMember  ajp://172.16.29.20:8009 route=TomcatB loadfactor=1
    ProxySet  lbmethod=byrequests
    ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
    ServerName      tc
    ProxyRequests Off
    ProxyVia        On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / balancer://tomcat/
    ProxyPa***everse / balancer://tomcat/
    <Location />
        Require all granted
    </Location>
</VirtualHost>

網頁管理工具

vim /etc/httpd/conf.d/balancer.conf
<Location /balancer-manager>
    SetHandler balancer-manager
    ProxyPass !
    Require all granted
</Location>

而後訪問172.16.29.2/balancer-manager

session cluster的實現

實現session cluster使用的是,tomcat自帶的cluster工具

在TomcatA的配置

 vim /etc/tomcat/server.xml
# 在Engine字段中添加以下內容
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
         channelSendOptions="8">
  <Manager className="org.apache.catalina.ha.session.DeltaManager"
           expireSessionsOnShutdown="false"
           notifyListenersOnReplication="true"/>
  <Channel className="org.apache.catalina.tribes.group.GroupChannel">
    <Membership className="org.apache.catalina.tribes.membership.McastService"
                address="228.0.120.14"
                port="45564"
                frequency="500"
                dropTime="3000"/>
    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
              address="172.16.29.10"
              port="4000"
              autoBind="100"
              selectorTimeout="5000"
              maxThreads="6"/>
    <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
      <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
    </Sender>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  </Channel>
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
         filter=""/>
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
            tempDir="/tmp/war-temp/"
            deployDir="/tmp/war-deploy/"
            watchDir="/tmp/war-listen/"
            watchEnabled="false"/>
  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
vim /var/lib/tomcat/webapps/test/WEB-INF/web.xml
# 在servlet字段前加以下內容
<distributable/>
在TomcatB的配置
 vim /etc/tomcat/server.xml
在Engine字段中添加以下內容
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
         channelSendOptions="8">
  <Manager className="org.apache.catalina.ha.session.DeltaManager"
           expireSessionsOnShutdown="false"
           notifyListenersOnReplication="true"/>
  <Channel className="org.apache.catalina.tribes.group.GroupChannel">
    <Membership className="org.apache.catalina.tribes.membership.McastService"
                address="228.0.120.14"
                port="45564"
                frequency="500"
                dropTime="3000"/>
    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
              address="172.16.29.20"
              port="4000"
              autoBind="100"
              selectorTimeout="5000"
              maxThreads="6"/>
    <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
      <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
    </Sender>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  </Channel>
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
         filter=""/>
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
            tempDir="/tmp/war-temp/"
            deployDir="/tmp/war-deploy/"
            watchDir="/tmp/war-listen/"
            watchEnabled="false"/>
  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
vim /var/lib/tomcat/webapps/test/WEB-INF/web.xml
# 在servlet字段前加以下內容
<distributable/>


session server的配置

     這裏的session server選擇memcached,架構圖如圖1.2

spacer.gifwKioL1iemi6BbjJsAABiJ8xcZxo967.png

圖1.2

TomcatA和B的配置同樣以下

vim /etc/tomcat/server.xml
# 把本來的<Context>字段替換爲以下的
        <Context path="/test" appBase="/var/lib/tomcat/webapps/test/" reloadable="true">
            <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
            memcachedNodes="n1:172.16.29.30:11211,n2:172.16.29.40:11211"
            failoverNodes="n1"
            requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
            transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
            />
        </Context>

同時提供調用的模塊,調用模塊調用模塊的地址以下http://down.51cto.com/data/2287135,這裏的模塊僅適用於Tomcat7

下載完導入/usr/share/java/tomcat/目錄下就能夠了

Tomcat8的把memcached-session-manager-tc7-1.8.3.jar替換爲memcached-session-manager-tc8-1.8.3.jar就能夠了,地址http://down.51cto.com/data/2287136


兩個memcached配置

yum install memcached
systemtcl start memcached

使用Nginx的作反向代理

vim /etc/nginx/nginx.conf
#在http配置段中添加一個
    upstream tomcat {
        server 172.16.29.10:8080;
        server 172.16.29.20:8080;
    }
#在這個location調用upstream定義的負載均衡的兩個服務器
        location / {
            proxy_pass http://tomcat;
        }
相關文章
相關標籤/搜索