1. 程式人生 > >spring+redis 實現 session 共享

spring+redis 實現 session 共享

使用Nginx+Tomcat進行負載均衡時,希望使用輪詢方式進行負載。但是如果使用輪詢方式的話,可能會訪問不同的Tomcat,此時如果不進行Session共享,則相當於是一個新的Session。就比如現有系統都是需要認證登入的系統,如果沒有Session共享,則會導致使用者退出登入

目前實現 session 共享的方式有以下幾種:

1、使用Tomcat內建的Session複製方案
   只適合Tomcat小叢集,不適合大叢集,因為session複製是all to all的方式
2、使用第三方(個人)基於Tomcat實現的Session管理
    第三方支援,支援力度不夠,尤其是不能提供對Tomcat8的支援
3、使用Spring Session實現

本文介紹的是第三種解決辦法,即使用 spring session + redis 的方案

 

一、引入 pom 

<!-- 使用Spring Session來解決Session共享問題  -->
<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
  <version>1.3.0.RELEASE</version>
  <type>pom</type>
</dependency>
<dependency>
  <groupId>biz.paluch.redis</groupId>
  <artifactId>lettuce</artifactId>
  <version>3.5.0.Final</version>
</dependency>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

 

二、配置 web.xml

在web.xml中加入以下過濾器,注意如果web.xml中有其他過濾器,一般情況下Spring Session的過濾器要放在第一位

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

 

三、配置 spring-mvc.xml

<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

<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="50" />
    <property name="maxIdle" value="10" />
    <property name="testOnBorrow" value="false" />
</bean>

<bean id="jedisConnectionFactory"
      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
    <property name="hostName" value="${redis.ip}"/>
    <property name="port" value="${redis.port}"/>
    <property name="password" value="${redis.password}" />
    <property name="timeout" value="3000"/>
    <property name="usePool" value="true"/>
    <property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

 

四、編輯 redis.properties

redis.ip=127.0.0.1
redis.port=6379
redis.password=123456

 

五、測試程式碼

寫一個 請求方法 轉發到 session_share.jsp

@RequestMapping("/session")
public String session(HttpSession session, HttpServletRequest request){
    request.setAttribute("id", request.getSession().getId());
    return "session_share";
}

 

接著 編寫 session_share.jsp

tomcat1
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>session共享</title>
</head>
<body>

1 我的session:${id}
<br>sessionid=<%=session.getId()%>

</body>
</html>

-------------------------------------------------------

tomcat2
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>session共享</title>
</head>
<body>

2 我的session:${id}
<br>sessionid=<%=session.getId()%>

</body>
</html>

 

六、配置 nginx 負載均衡

upstream load_balance_server {
        #weigth引數表示權值,權值越高被分配到的機率越大
        server 192.168.0.131:8080   weight=1;
        server 192.168.0.167:8090   weight=1;
    }
		
	server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
              proxy_pass http://load_balance_server;
              index  dashboard index;
              proxy_set_header Host       $http_host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
              proxy_set_header X-Forwarded-Proto $scheme;
	    } 
	 }

 

 

nginx 所在 IP 為 192.168.0.131

連續多次訪問  192.168.0.131:80/session     頁面上的 sessionId  沒有發生變化 , 1 和 2 在不停