session共享**最常用的是Tomcat的Session共享(复制)的几种实现方案>
方案一 Tomcat内置的session复制方案
<!-- 第1步:修改server.xml,
在Host节点下添加如下Cluster节点 -->
<!-- 用于Session复制 -->
<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" />
<!-- address="288.0.0.4"是利用tomcat之间的组播功能实现session 的共享 -->
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="localhost" port="4000"
autoBind="100" selectorTimeout="5000" maxThreads="6" />
<!--注:同一服务器多个tomcat port="4000"要进行更改-->
<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.MessageDispatchInterceptor" />
</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.ClusterSessionListener" />
</Cluster>
方案二 利用第三方Tomcat实现的session的共享>
利用tomcat-redis-session-manager实现的。 这种方式支持Tomcat7和Tomcat8两种方式配置不一样。 从github上获取(仅支持Tomcat7,关于tomcat8的不同暂未介绍): (https://github.com/jcoleman/tomcat-redis-session-manager)
方案三是使用spring session实现的
这种方式对于spring版本支持4.1以上就可以(要不项目升级spring)。 1.我们项目中先对spring做了升级,由原来spring 2.5.5升级为spring 4.3.23。 2.我们配置的是以下jar包,请勿擅自改动 commons-pool2:2.4.2、spring-data-redis:1.7.3 spring-session:1.2.2 、 jedis:2.9.0
3.1进行Spring Session配置
<!--
第1步:在Spring配置文件中添加如下bean
以后在web.xml中配置session超时时间就无效了,如果需要指定session超时时间,则使用maxInactiveIntervalInSeconds来指定,默认是1800s=30min
-->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"
p:maxInactiveIntervalInSeconds="1800"/>
3.2没有Redis连接配置,请看RedisHttpSessionConfiguration类中的如下代码:
@Bean
public RedisTemplate<Object, Object> sessionRedisTemplate(
RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
if (this.defaultRedisSerializer != null) {
template.setDefaultSerializer(this.defaultRedisSerializer);
}
template.setConnectionFactory(connectionFactory);
return template;
}
3.3注入的jedisConnectionFactory Bean如下:
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.hostname}"
p:port="${redis.port}"
p:database="${redis.database}"
p:poolConfig-ref="redispoolconfig"
p:use-pool="${redis.usepool}">
</bean>
3.4如果你的项目中没使用过Redis,可以使用以下配置:
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<!--
Jdeis连接工厂Bean
注意:这种方式没有使用连接池,生产环境下务必需要使用连接池
-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="192.168.1.233" p:port="6379" p:database="15" p:usePool="false">
</bean>
3.5 过滤器的配置
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>