美文网首页
session 共享-spring-整合redis

session 共享-spring-整合redis

作者: lmandy | 来源:发表于2017-06-17 19:51 被阅读0次

一.环境准备(windows下nginx+tomcat,linux同理)

1.nginx一台

2.tomcat 2台(端口号不同,我这里是8081和8082)

3.web项目一个(我这里项目名tomcat-session)

二.详细配置

1.nginx

在nginx的conf文件下找到nginx.conf将以下位置改成


image.png
2.tomcat

在tomcat的conf文件夹下找到server.xml配置文件进行端口配置,分别配置成8081和8082,和nginx中相同。

image.png
添加虚拟路径
image.png
3.web项目(tomcat-session)

在build.gradle文件中添加依赖(maven同理)

compile group: 'org.springframework.session', name: 'spring-session-data-redis', version: '1.3.0.RELEASE'

在WEB-INF下创建spring-session.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">

    <context:annotation-config/>

    <bean id="sessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!--修改session的有效时间-->
        <property name="maxInactiveIntervalInSeconds" value="1800"></property>
    </bean>
<!--本地redis配置,大家自定安装redis-->
    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"></property>
        <property name="port" value="6379"></property>
        <property name="password" value="123"></property>
    </bean>
</beans>

项目加载的时候加载spring-session.xml,在web.xml中加载它。并添加过滤器。(注意,在所有的过滤器之前)

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-session.xml</param-value>
    </context-param>

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

在index.jsp中添加如下代码(首页能访问的到)

项目1:
 <body>
  SessionID:<%=session.getId()%>
  <BR>
  SessionIP:<%=request.getServerName()%>
  <BR>
  SessionPort:<%=request.getServerPort()%>
  <%
    out.println("This is Tomcat Server 1111");
  %>
  </body>
项目2:
 <body>
  SessionID:<%=session.getId()%>
  <BR>
  SessionIP:<%=request.getServerName()%>
  <BR>
  SessionPort:<%=request.getServerPort()%>
  <%
    out.println("This is Tomcat Server 222");
  %>
  </body>

三.启动访问

启动redis,(先启动resdis)和2台tomcat,nginx。

1,直接访问2个项目

项目1:


image.png

项目2:


image.png
redis 2次访问后分布查看结果
image.png

通过上述图片可以发现两次的结构是一样的。

2.通过nginx访问,(先使用flushall命令清空redis)使用nginx中server_name : localhost 访问

分发请求到项目1

image.png

分发请求到项目2

image.png

redis:

image.png
同样通过上述图片可以看出也实现了session的共享。
四.参考文献
http://blog.csdn.net/xiaozhaorui/article/details/51326833
http://www.10tiao.com/html/591/201702/2247483939/1.html

相关文章

网友评论

      本文标题:session 共享-spring-整合redis

      本文链接:https://www.haomeiwen.com/subject/xmsmqxtx.html