集群部署时的分布式session如何实现

时间:2023-03-09 14:59:04
集群部署时的分布式session如何实现

集群部署时的分布式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>

示例代码

 @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工程师面试突击