zoukankan      html  css  js  c++  java
  • 集群部署时的分布式session如何实现

    tomcat + redis

      这个其实还挺方便的,就是使用session的代码跟以前一样,还是基于tomcat原生的session支持即可,然后就是用一个叫做Tomcat RedisSessionManager的东西,让所有我们部署的tomcat都将session数据存储到redis即可。

      在tomcat的配置文件中,配置一下

     

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

     

      搞一个类似上面的配置即可,你看是不是就是用了RedisSessionManager,然后指定了redis的host和 port就ok了。

     

     

    spring session + redis

      分布式会话的这个东西重耦合在tomcat中,如果我要将web容器迁移成jetty,难道你重新把jetty都配置一遍吗?

      因为上面那种tomcat + redis的方式好用,但是会严重依赖于web容器,不好将代码移植到其他web容器上去,尤其是你要是换了技术栈咋整?比如换成了spring cloud或者是spring boot之类的。

      所以现在比较好的还是基于java一站式解决方案,spring了。人家spring基本上包掉了大部分的我们需要使用的框架了,spirng cloud做微服务了,spring boot做脚手架了,所以用sping session是一个很好的选择。

     

    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>

     

    示例代码

     1 @Controller
     2 
     3 @RequestMapping("/test")
     4 
     5 public class TestController {
     6 
     7  
     8 
     9 @RequestMapping("/putIntoSession")
    10 
    11 @ResponseBody
    12 
    13     public String putIntoSession(HttpServletRequest request, String username){
    14 
    15         request.getSession().setAttribute("name",  “leo”);
    16 
    17  
    18 
    19         return "ok";
    20 
    21     }
    22 
    23  
    24 
    25 @RequestMapping("/getFromSession")
    26 
    27 @ResponseBody
    28 
    29     public String getFromSession(HttpServletRequest request, Model model){
    30 
    31         String name = request.getSession().getAttribute("name");
    32 
    33         return name;
    34 
    35     }
    36 
    37 }

     

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

     

    转自:中华石杉Java工程师面试突击

     

  • 相关阅读:
    Git简介
    git 目录
    版本控制系统介绍
    python 爬虫 基于requests模块发起ajax的post请求
    python 爬虫 基于requests模块发起ajax的get请求
    POJ 2575
    POJ 2578
    POJ 2562
    POJ 2572
    POJ 2560
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/10095022.html
Copyright © 2011-2022 走看看