zoukankan      html  css  js  c++  java
  • tomcat session共享快速入门

    通过Spring Session实现新一代的Session管理

    利用spring session解决共享Session问题

    包依赖

    <!-- redis start -->
        <dependency>
          <groupId>org.springframework.session</groupId>
          <artifactId>spring-session-data-redis</artifactId>
          <version>1.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <!-- 这个是redis的来凝结器可以选择自己喜欢的 -->
          <groupId>biz.paluch.redis</groupId>
          <artifactId>lettuce</artifactId>
          <version>3.5.0.Final</version>
        </dependency>

    XML配置

    • spring.xml
    <!-- redis start -->
        <cache:annotation-driven/>
        <!-- redis连接工厂 -->
        <bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
            <property name="hostName"    value="${redis.host}"/>
            <property name="port"        value="${redis.port}"/>
            <property name="password"    value="${redis.pass}"/>
        </bean>
    
        <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory"   ref="lettuceConnectionFactory" />
            <property name="keySerializer" >
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
            </property>
            <property name="valueSerializer" >
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
            </property>
        </bean> >
    
        <!-- 把配置类加载到spring容器 **重要**-->
        <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
            <!-- 过期时间100分钟 -->
            <!-- 配置RedisSession -->
            <property name="maxInactiveIntervalInSeconds" value="6000"></property>
        </bean>
        <!-- redis end -->
    • web.xml
    <!-- 添加过滤器,这个过滤器会自动包装session,request,response -->
    <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
      </filter-mapping>

    java-config方式

    这是在redis连接已经配置好的情况下
    
    • 创建一个类来启用EnableRedisHttpSession
    //这里用@Configuration注解来启用配置类,你也就可以用其他方式
    @Configuration
    @EnableRedisHttpSession 
    public class Config { 
    }
    • 创建一个初始化类继承AbstractHttpSessionApplicationInitializer
    //这个类会在初始化的时候插入filter
    public class Initializer extends AbstractHttpSessionApplicationInitializer { 
            /*
            你也可以把刚刚的配置类放在这里引入
            public Initializer() {
                    super(Config.class); 
            }
            */
    }
  • 相关阅读:
    一次惨痛的debug的经历-RuntimeError: CUDA error: an illegal memory access was encountered
    Rank loss调研
    守护进程 supervisor
    PHP实现异步请求非阻塞
    PHP实现图片和文字水印(PHP给图片添加水印功能)
    虚拟机相关博客
    小师妹学JavaIO之:文件系统和WatchService
    后端 Java ActionEvent getModifiers()
    Java中常见的锁简述
    关键系统的JVM参数推荐
  • 原文地址:https://www.cnblogs.com/A-yes/p/9894164.html
Copyright © 2011-2022 走看看