Tomcat6.0でセッションレプリケーション

フェイルオーバーを目的とした冗長構成実現のため、セッションレプリケーション設定を行ってみることにした。
備忘録として以下に。

前提

ー 構成は以下

web
  |
[Cent1,IP:192.168.0.11]             [Cent2,IP:192.168.0.12]
Apache 
(mod_proxy_balancerでバランシング)
  |___________________________________
  |                                   | 
Tomcat                              Tomcat

サーバ側

ポート設定

初期状態ではCentOSのFWのマルチキャスト用のポートが空いていなかったので設定。
ポートはMembership=44564/UDP、Receiver=5000/tcpを利用する。

$ /sbin/iptables -I INPUT -p udp -m udp --dport 45564 -j ACCEPT
$ /sbin/iptables -I INPUT -p tcp -m tcp --dport 5000 --syn -j ACCEPT

上記コマンドを両方のサーバで実行。

マルチキャスト用route設定

両方のサーバでマルチキャストのルーティングを指定
1. /etc/sysconfig/static-routesに以下を追加

any net 224.0.0.0/4 eth0

2. ネットワーク再起動

$ service network restart

Tomcat設定

server.xmlに対して、Tomcat5.5までのサンプルを元に以下の設定を行う。

変更前
<!-- ここあたり
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" />
-->
変更後
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
  <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.0.4" port="45564" frequency="500" dropTime="3000" />
    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
              address="192.168.0.11" port="5000" selectorTimeout="100" maxThreads="6" /> ←※cent2では192.168.0.12に設定
    <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" />
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />
  </Channel>
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
         filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>

注意点としてReceiverのaddressは"auto"(デフォルト値)だとlocalhostになってしまうため、明示的にサーバのIPアドレスを指定しておく必要があった。

アプリ設定

搭載アプリのweb.xmlに以下の一文を追記

<distributable />

Apache設定

mod_proxy_balancer設定をhttp.confに追記

ProxyPass / balancer://test/ nofailover=On timeout=2
<Proxy balancer://test/>
  BalancerMember ajp://192.168.0.11:8009 keepalive=On loadfactor=100 retry=5 
  BalancerMember ajp://192.168.0.12:8009 keepalive=On loadfactor=100 retry=5 
</Proxy>