在分佈式系統中,咱們想要實現統一會話有如下5種經常使用的實現方式:css
MSM( memcached-session-manager )是什麼前端
MSM是一個高可用的Tomcat集羣中Session共享解決方案。 MSM特性 支持黏性、非黏性Session 可處理Tomcat故障轉移 可處理Memcached故障轉移 插件式session序列化 容許異步保存session 黏性、非黏性Session的方案 最大的區別在於: 1:黏性Session的方案,適合於前端的負載均衡器能保證,每一個用戶的請求都路由到同一個Tomcat上這種場景 2:黏性Session的方案,會把Memcached中的數據同步存放到本地的Http Session區。 SESSIONID的格式 因爲MSM知道Memcached節點列表,所以SESSIONID中會帶着這些節點,好比: 09D01FE736F0C682A2AA71D464EC5ED9-n1 MSM安裝配置 1:環境是Tomcat8,須要的jar包參見下面 2:把這些jar包添加到Tomcat8/lib,或者是應用自身的lib裏面 3:修改context.xml或server.xml文件中的<Context>節點,示例以下: (1)使用non-sticky session,memcached緩存 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211,n2:192.168.1.106:2222" sticky="false" sessionBackupAsync="false" lockingMode="auto" requestUriIgnorePattern=「.*\.(ico|png|gif|jpg|css|js)$」 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFac tory" /> (2)使用sticky session,memcached緩存 a:添加jvmroute參數,每一個Tomcat配置的這個值要不同,以下: <Engine name="Catalina"defaultHost="localhost「 jvmRoute="tomcat1"> b:把上面配置中的sticky設置爲true c:不用配置lockingMode,這個是隻有非黏性session才使用,默認值爲none none: 從不對session進行鎖定 all: session將一直被鎖定,直到請求結束 auto: 對於只讀請求,session將不會被鎖定,若是是非只讀請求,則會被鎖定 uriPattern:<regexp>: 經過正則表達式的方式來對請求uri以及查詢字符串進行匹配,只有匹配上的纔會被鎖定。 d:添加failoverNodes配置,用來指定備用的memcached節點 4:配置後啓動Tomcat,若是沒有報錯就表示配置好了 5:能夠自定義序列化的方案,除了默認的Java序列化外,常見的還有:kryo、 javolution、xstream、flexjson等,能夠下載相應的jar,並修改 transcoderFactoryClass。 建議使用maven來管理並下載須要的jar
查看Memcached的數據 java
1:鏈接到Memcached服務器:telnet 192.168.1.106 2222 2:查看多有的Item:stats items 3:輸出Item:stats cachedump itms後面的數據 0 ,0表示顯示所有數據 4:輸出具體的數據:get key ,key就是上面item後面的數據
參考連接:http://blog.csdn.net/a__java___a/article/details/8738932git
首先下載解壓github
https://github.com/ran-jit/TomcatClusterRedisSessionManager/releases/download/1.1.1/TomcatRedisSessionManager-1.1.1.zip
第一步:上面解壓裏面的jar包拷貝到tomcat目錄裏面lib下面
第二步:把RedisDataCache.properties拷貝到 tomcat/conf/下面
第三步:在tomcat/conf目錄裏面的content.xml文件中加上下面兩行web
#添加到content.xml <Valve className="com.r.tomcat.session.management.RequestSessionHandlerValve"/> <Manager className="com.r.tomcat.session.management.RequestSessionManager"/>
參考連接:https://github.com/ran-jit/TomcatClusterRedisSessionManager/wiki正則表達式
第一步: 添加shiro須要的資源redis
第一步:添加依賴 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-aspectj</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.2.2</version> </dependency> 第二步: 在spring-mvc.xml中添加對shiro的filter的配置 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/toLogin"/> <property name="unauthorizedUrl" value="/unauthorized.jsp"/> <property name="filterChainDefinitions"> <value>/jcaptcha* = anon /logout = anon</value> </property> </bean>
import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import org.apache.shiro.session.Session; import org.apache.shiro.session.UnknownSessionException; import org.apache.shiro.session.mgt.eis.AbstractSessionDAO; import org.springframework.beans.factory.annotation.Autowired; import com.danga.MemCached.MemCachedClient; public class MySessionDAO extends AbstractSessionDAO{ private static final int EXPIRE_TIME = 1*60; @Autowired private MemCachedClient mcc; public void update(Session session) throws UnknownSessionException { mcc.set(session.getId().toString(), session,new Date(EXPIRE_TIME)); } public void delete(Session session) { mcc.delete(session.getId().toString()); } public Collection<Session> getActiveSessions() { /** * @todo */ return new ArrayList<Session>(); } @Override protected Serializable doCreate(Session session) { Serializable sid = this.generateSessionId(session); assignSessionId(session, sid); mcc.add(sid.toString(), session,new Date(EXPIRE_TIME)); return sid; } @Override protected Session doReadSession(Serializable sessionId) { Session s = (Session)mcc.get(sessionId.toString()); //計算過時時間,是從最後一次使用開始 mcc.set(sessionId.toString(),s, new Date(EXPIRE_TIME)); return s; } }
第四步: 在web.xml中添加對shiroFilter的配置 <filter> <filter-name>shiroFilter</filter-name> <filter- class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
第五步使用自定義的MySessionDAOspring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:repository="http://www.springframework.org/schema/data/repository" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd "> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" /> <property name="arguments" ref="securityManager" /> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager" /> <property name="sessionManager" ref="sessionManager" /> </bean> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="sessionDAO"></property> </bean> <bean id="sessionDAO" class="com.sishuok.shiro.MySessionDAO" /> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" /> </beans>
若是是springboot就很是簡單apache
添加依賴
<!-- spring session --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
配置application.properties
# spring session使用存儲類型 #spring.session.store-type=redis # spring session刷新模式:默認on-save #spring.session.redis.flush-mode=on-save #spring.session.redis.namespace= # session超時時間,單位秒 #server.session.timeout=30 #redis #spring.redis.host=localhost #spring.redis.port=6379 #spring.redis.password=123456 #spring.redis.database=0 #spring.redis.pool.max-active=8 #spring.redis.pool.max-idle=8 #spring.redis.pool.max-wait=-1 #spring.redis.pool.min-idle=0 #spring.redis.timeout=0
若是是傳統的spring項目請參考:http://blog.csdn.net/u012515742/article/details/54973075