Nginx 負載平衡條件下 Redis 共用Session (Java)(二)

來源:互聯網
上載者:User

標籤:mave   logs   target   config   tomcat   lan   targe   ons   三台   

 

關於Session的問題 網上有各個方面的針對,有在nginx 上處理: ip_hash的session,有在 tomcat 做處理:修改Context檔案,有針對項目做處理。本篇就是對項目處理

1、首先我的項目是用Maven(如果沒有用maven可以去下載),使用到的jar包有下:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>


<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>


<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>

<!-- 記錄檔管理組件 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->

2、配置spring 設定檔 applicationContext-redis.xml

  <context:annotation-config />    <context:property-placeholder location="classpath:redis.properties"/>    <bean id="redisHttpSessionConfiguration"          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">        <property name="maxInactiveIntervalInSeconds" value="3600"/>    </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.host}"/>        <property name="port" value="${redis.port}"/>        <property name="password" value="${redis.pass}" />        <property name="timeout" value="3000"/>        <property name="usePool" value="true"/>        <property name="poolConfig" ref="jedisPoolConfig"/>    </bean>

3、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>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:applicationContext*.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>

項目結構圖

5、成功介面

三台伺服器

IP:10.13.5.10  作為負載平衡伺服器 和 redis 伺服器

IP:10.13.5.11  專案服務器01

IP:  10.13.5.12 專案服務器02

 

第一張對比: 出現了兩個IP 不同的伺服器

 

 

 

 

 

第二張對比(向Session 存值): 

 

 

 

如上面 兩組圖片,其中 SessionID沒有變,redis中的

 

最後是注意事項:

1、一定要保證nginx.conf 中的location / {}  , ‘/’ 之後不要加任何uri  加了就實現不了。

2、一定要保證是同一個項目,就算只是改了html 中的幾個字元,有時候Session 共用還是會失效!!!

 

Nginx 負載平衡條件下 Redis 共用Session (Java)(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.