負載均衡-session共享的三種處理方法
阿新 • • 發佈:2019-02-18
方案一、Tomcat 廣播方案(Tomcat Session Clustering)
1.修改 conf 資料夾下面的 server.xml 配置檔案,在 Engine 模組下新增如下程式碼
<Engine name="Catalina" defaultHost="localhost">
-- 新增下面程式碼
<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.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
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>
2.在 web 專案的 web.xml 檔案中新增
<distributable/>
方案二、Tomcat-redis-session-manager 外掛方案,將sessionId放在redis進行儲存
1.新增 redis session 叢集依賴的jar包到 tomcat/lib 目錄下
tomcat-redis-session-manager-2.0.0.jar
jedis-2.5.2.jar
commons-pool2-2.2.jar
2.修改 conf 目錄下的 context.xml 檔案
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />
3.啟動 DOS 環境進入 redis 目錄,啟動 redis
redis-server.exe redis.windows.conf
方案三、tomcat-memcached-session-manmger 外掛方案
1.新增 memcached session 叢集依賴的 jar 包到 tomcat/lib 目錄下
couchbase-client-1.4.4.jar
javolution-5.4.3.1.jar
memcached-session-manager-1.6.5.jar
memcached-session-manager-tc7-1.6.5.jar
msm-javolution-serializer-1.6.5.jar
msm-kryo-serializer-1.6.5.jar
msm-xstream-serializer-1.6.5.jar
spymemcached-2.11.4.jar
2.修改 conf 目錄下的 context.xml 檔案
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:localhost:11211"
sticky="false"
requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false"/>
3.安裝 memcached
windows:
解壓 memcached 安裝包,啟動 DOS 環境,進入 memcached 的目錄,輸入 memcached.exe -d install 命令安裝 memcached
安裝後的 memcached 安裝為系統服務,進入工作管理員,在服務中找到 memcached 服務,右鍵啟動
3種方式的配置Demo在http://download.csdn.net/download/qq_34125349/10115601,親測可用,有什麼描述問題請大家指教;