shiroはehcacheを使用してクラスタ同期とセッションレプリケーションを実現


一般的にWEBアプリケーションクラスタのセッションレプリケーションはコンテナによって実現され、shiro権限フレームワークではセッションDAOが直接セッションをキャッシュに永続化することを定義することができ、これによりキャッシュをクラスタ化するだけでセッションのレプリケーションに代わることができる.
実現構想.
1、セッションDAOでセッションをehcacheキャッシュに保存する
2、ehcacheのjgroupsクラスタレプリケーションを構成し、クラスタサーバが比較的多い場合、キャッシュサーバにアップグレードできる
考え方は簡単ですが、実現過程には多くの問題が発生しています.今、過程と配置ファイルを記録して、忘れないようにしてください.
最初のステップでは、SessionDAOをehcacheキャッシュに保存します.このステップは簡単で、問題もありません.ネット上の例では簡単です.次はプロファイルの主なセグメントです.
<!--        SessionDAO -->
 <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  <property name="sessionDAO" ref="sessionDAO"/>
 </bean>
<!-- SessionDAO shiro        -->
 <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> 
       <!-- <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/> -->
        <property name="cacheManager" ref="shiroCacheManager" />
</bean>
<!--             -->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
     <property name="realm" ref="userRealm"/>
     <property name="sessionManager" ref="sessionManager"/>
     <!-- By default the servlet container sessions will be used.  Uncomment this line
          to use shiro's native sessions (see the JavaDoc for more): -->
     <!-- <property name="sessionMode" value="native"/> -->
 </bean>
<!--         ,        ,           -->
 <bean id="userRealm" class="cn.ys.security.UserRealm">
  <property name="cacheManager" ref="shiroCacheManager"/>
  <property name="userService" ref="securityService" />
  <property name="credentialsMatcher" ref="credentialsMatcher"></property>
 </bean>
<!--  spring ehcache   -->
 <bean id="shiroCacheManager" class="cn.ys.security.ehcache.EhCacheManager">
  <property name="cacheManager" ref="ehcacheFactory"></property>
 </bean>

第2歩は多くの問題に直面して、ネット上の資料は比較的に少なくて、jgroupsに対してまた熟知していないで、以下は配置ファイルと私が出会った問題です:
ehcache.xml
<!--     jgroup   -->
 <cacheManagerPeerProviderFactory
 class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
 properties="file=jgroups_tcp.xml"  /> 
 <diskStore path="java.io.tmpdir/ehcache"/>
   <defaultCache  
            maxElementsInMemory="10000"  
            eternal="false"  
            timeToIdleSeconds="120"  
            timeToLiveSeconds="120"  
            overflowToDisk="true"  
            maxElementsOnDisk="10000000"  
            diskPersistent="false"  
            diskExpiryThreadIntervalSeconds="120"  
            memoryStoreEvictionPolicy="LRU"  
            /> 
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="1000"
           overflowToDisk="true"
           timeToLiveSeconds="0"
           timeToIdleSeconds="0"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600">
   <cacheEventListenerFactory
          class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
          properties="replicateAsynchronously=true, replicatePuts=true,
          replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />
    </cache>
    <cache name="cn.ys.security.UserRealm.authorizationCache"
           maxElementsInMemory="1000"
           eternal="true"
           overflowToDisk="true">
   <cacheEventListenerFactory
          class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
          properties="replicateAsynchronously=true, replicatePuts=true,
          replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />
 </cache>

    <cache name="org.apache.shiro.realm.text.PropertiesRealm-0-accounts"
           maxElementsInMemory="1000"
           eternal="true"
           overflowToDisk="true">
   <cacheEventListenerFactory
          class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
          properties="replicateAsynchronously=true, replicatePuts=true,
          replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true" />
 </cache>

jgroups_tcp.xml
 <config>
     <TCP bind_port="7800" bind_addr="localhost"
  loopback="false"
         recv_buf_size="${tcp.recv_buf_size:20M}"
         send_buf_size="${tcp.send_buf_size:1M}"
         discard_incompatible_packets="true"
         max_bundle_size="2M"
         max_bundle_timeout="30"
         enable_bundling="true"
         use_send_queues="true"
         sock_conn_timeout="300"

         timer_type="new"
         timer.min_threads="4"
         timer.max_threads="10"
         timer.keep_alive_time="3000"
         timer.queue_max_size="500"
         
         thread_pool.enabled="true"
         thread_pool.min_threads="1"
         thread_pool.max_threads="10"
         thread_pool.keep_alive_time="5000"
         thread_pool.queue_enabled="false"
         thread_pool.queue_max_size="100"
         thread_pool.rejection_policy="discard"

         oob_thread_pool.enabled="true"
         oob_thread_pool.min_threads="1"
         oob_thread_pool.max_threads="8"
         oob_thread_pool.keep_alive_time="5000"
         oob_thread_pool.queue_enabled="false"
         oob_thread_pool.queue_max_size="100"
         oob_thread_pool.rejection_policy="discard"    
     />
     <TCPPING timeout="3000"
              initial_hosts="localhost[7800]"
              port_range="5"
              num_initial_members="5"/>
    <MERGE2  min_interval="10000"
             max_interval="30000"/>
    <FD_SOCK/>
    <FD timeout="3000" max_tries="3" />
    <VERIFY_SUSPECT timeout="1500"  />
    <BARRIER />
    <pbcast.NAKACK use_mcast_xmit="false"
                    retransmit_timeout="300,600,1200,2400,4800"
                    discard_delivered_msgs="true"/>
    <pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000" max_bytes="400000"/>
    <pbcast.GMS print_local_addr="true" join_timeout="5000" view_bundling="true"/>
 </config>

ネット上で検索した例jgroupsバージョンは古いかもしれませんが、ehcacheの例も同じで、start_のように新しいjgroupsが認識されていない属性もあります.portはbind_に変更されましたport、また新しいバージョンjgroupsにbind_が追加されましたaddrは、jvmにipv 4パラメータを追加する必要がなく、ipアドレスバインドを直接指定することができます.
jgroupsを構成するいくつかの理解すべき点
1、jgroupsのTCPのポートは自動的に割り当てられて自動的に発見され、同じサーバ上の複数のインスタンスをinitial_に配置する必要はない.hostsでは、initial_にIPがあります.hostsでは一度だけ定義する必要があります.ポートとbind_port一貫性を保つ
2、port_rangeは、bind_などのポートの自動割り当ておよび自動検出に使用されます.port値は7800で、同サービス上の第1のインスタンスの起動が7800ポートを使用し、第2のインプリメンテーションの起動が7801を使用し、最大7804までしか使用されません.
3、max_bundle_sizeはあまり小さくしないでください.例では64 Kしかありません.結果としてコピーできない場合があります.
4、インスタンスが閉じて再起動すると、jgroupsは別のhostとみなされ、元のhostはタイムアウトを設定しないとずっと存在するので、keep_の設定に注意してください.alive_time