Redis shared session under Nginx load Balancing (Java) (ii)

Source: Internet
Author: User
Tags redis server nginx load balancing log4j

On the question of the session on the Internet has various aspects of the target, there is in Nginx processing: Ip_hash session, there is in Tomcat do processing: Modify the context file, there is a project to do processing. This article deals with the project

1, first of all my project is with Maven (if not with Maven can download), the use of the jar package has the following:

<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 File Management Pack--
<!--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. Configure Spring configuration file Applicationcontext-redis.xml

<context:annotation-config/> <context:property-placeholder location= "Classpath:redis.properties"/> & Lt;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= "+"/> <property name= "Maxidle" value= "ten"/> </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= "+"/> <prope Rty name= "Usepool" value= "true"/> <property name= "poolconfig" ref= "Jedispoolconfig"/> </bean>

3. Web. XML Join

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

Project Structure diagram

5. Successful interface

Three servers

ip:10.13.5.10 as a Load balancer server and Redis server

ip:10.13.5.11 Project Server 01

ip:10.13.5.12 Project Server 02

First comparison: Two servers with different IP

Second contrast (store value to session):

such as the above two groups of pictures, where SessionID has not changed, Redis in the

Finally, note the following:

1, must ensure that nginx.conf in the location/{}, '/' after the addition of any URI added will not be implemented.

2, must ensure that the same project, even if just changed the HTML of several characters, sometimes session sharing or will expire!!!

Redis shared session under Nginx load Balancing (Java) (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.