Spring+redis+nginx implementation of distributed session sharing

Source: Internet
Author: User

1,spring must be in the version 4.3 or above

2,maven Configuration

Add two important dependencies

  

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

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

3,spring.xml File Configuration
<!--distributed session-->
<bean id= "Redishttpsessionconfiguration"
class= "Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
<property name= "maxinactiveintervalinseconds" value= "/>"
</bean>

<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" >
<property name= "Maxtotal" value= "${jedis.pool.maxtotal}"/>
<property name= "Maxidle" value= "${jedis.pool.maxidle}"/>
</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= "/>"
<property name= "Usepool" value= "true"/>
<property name= "Poolconfig" ref= "Jedispoolconfig"/>
</bean>

4,redis Configuration
 #redis配置 
#*****************jedis\u8fde\u63a5\u53c2\u6570\u8bbe\u7f6e*********************#
#redis \ U670D\U52A1\U5668IP #
#redis. ip=47.92.128.123
redis.ip=192.168.9.168
#redis \u670d\u52a1\u5668\u7aef\ u53e3\u53f7#
redis.port=6379
redis.password=123456
#************************jedis\u6c60\u53c2\u6570\ u8bbe\u7f6e*******************#
#jedis \u7684\u6700\u5927\u5206\u914d\u5bf9\u8c61#
jedis.pool.maxactive=
#jedis \u6700\u5927\u4fdd\u5b58idel\u72b6\u6001\u5bf9\u8c61\u6570 #
jedis.pool.maxidle=1000
#jedis \U6C60\U6CA1\U6709\U5BF9\U8C61\U8FD4\U56DE\U65F6\UFF0C\U6700\U5927\U7B49\U5F85\U65F6\U95F4 #
jedis.pool.maxtotal=1500
#jedis \u8c03\u7528borrowobject\u65b9\u6cd5\u65f6\uff0c\u662f\u5426\u8fdb\u884c\ u6709\u6548\u68c0\u67e5#
Jedis.pool.testonborrow=true
#jedis \u8c03\u7528returnobject\u65b9\u6cd5\u65f6\ Uff0c\u662f\u5426\u8fdb\u884c\u6709\u6548\u68c0\u67e5 #
Jedis.pool.testonreturn=true

5,web.xml Configuration
<!--spring  Redis  shared session blocker--
<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>

After the above configuration, you can use the
To achieve session sharing also need to access the same time domain name, if the domain name inconsistency is not able to share
For example :
Load Balancing Two servers
a:192.168.1.101:8080
b:192.168.1.102:8081
The use of IP addresses in the browser to access services under two servers is not to achieve session sharing
Only through Nginx do load balancer Two servers bind the same domain name as www.test.com
Through the domain name to visit at this time regardless of the request distribution to which machine can realize session sharing



Spring+redis+nginx implementation of distributed session sharing

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.