zoukankan      html  css  js  c++  java
  • 分布式session

    1. tomcat+redis

        在tomcat配置文件中配置RedisSessionManager ,tomcat会把session存入redis

       

    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
             host="{redis.host}"
             port="{redis.port}"
             database="{redis.dbnum}"
             maxInactiveInterval="60"/>

       缺点,如果移植web容器,会很麻烦。

    2 springsession+redis

       通过springsession直接写入redis 

    pom.xml
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
      <version>1.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.1</version>
    </dependency>

    Spring配置文件中

    <bean id="redisHttpSessionConfiguration"
         class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="600"/>
    </bean>
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="100" />
        <property name="maxIdle" value="10" />
    </bean>
    
    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
        <property name="hostName" value="${redis_hostname}"/>
        <property name="port" value="${redis_port}"/>
        <property name="password" value="${redis_pwd}" />
        <property name="timeout" value="3000"/>
        <property name="usePool" value="true"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

    web.xml

    <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>

    示例代码:

    @Controller
    @RequestMapping("/test")
    public class TestController {
    
    @RequestMapping("/putIntoSession")
    @ResponseBody
        public String putIntoSession(HttpServletRequest request, String username){
            request.getSession().setAttribute("name",  "leo");
    
            return "ok";
        }
    
    @RequestMapping("/getFromSession")
    @ResponseBody
        public String getFromSession(HttpServletRequest request, Model model){
            String name = request.getSession().getAttribute("name");
            return name;
        }
    }

    给sping session配置基于redis来存储session数据,然后配置了一个spring session的过滤器,这样的话,session相关操作都会交给spring session来管了。接着在代码中,就用原生的session操作,就是直接基于spring sesion从redis中获取数据了。

  • 相关阅读:
    java网络编程(UDP详解)
    java网络编程(TCP详解)
    java-单例详解
    java-接口和抽象类的联系和区别。
    java-集合小结
    Java异常(一)Java异常简介及其框架
    JAVA的接口
    深入理解Java的接口和抽象类
    linux动态库编译和使用详细剖析
    C基础 time.h 简单思路扩展
  • 原文地址:https://www.cnblogs.com/dingpeng9055/p/11206081.html
Copyright © 2011-2022 走看看