Spring session + Redis for Web project session sharing

Source: Internet
Author: User
Tags redis tomcat

About session sharing, there are a lot of articles on the Internet, but very few articles are others have seen, can be practical operation can be realized, I do not know what is missing out of the article or what reason, may be my ability to not reach the required capacity of the article required it. Regardless of the number, I'll take the actual configuration of the installation deployment instance to tell me how to implement session sharing.

Before the article begins, let's start with a little episode. At first, I want to not make any changes to the program, only through the Nginx+tomcat+redis way to achieve session sharing, the actual configuration, with a simple test session of a JSP page to test, it is true to see the session is shared. But when I actually deployed my Web project (spring+springmvc+mybatis), I found that the session could not be shared, the content of the session was not exported to Redis, there was no reason for it, and eventually it was abandoned. decided to use Spring's own session management with Redis to achieve session sharing, this operation only needs to change the configuration, change the jar package, no need to change the business code.

First, clarify the environmental requirements: Windows systems, 1.7JDK,TOMCAT7, using spring-session, requires that spring's dependent packages must be above spring4.0, and Redis requirements are above 3.0. In addition to some core packages in the Spring Foundation, Project Engineering Additional Commons-pool2-2.4.2.jar,jedis-2.7.3.jar,spring-data-redis-1.6.2.release.jar,spring-session-1.1.1.release.jar required, the corresponding version number Also gives, at least these versions of the jar is used together, is compatible with each other, I am not sure if the other version of the jar after the introduction of the problem will be, we are interested to try the other version.

Here also paste the spring+session related jar package download address: http://download.csdn.net/detail/sdmanooo/9755249.

redis3.0 Download Address: http://download.csdn.net/detail/sdmanooo/9755259 After downloading the extract, run cmd in the directory, execute the Redis-server.exe redis.windows.conf start Redis, you cannot double-click Redis-server.exe, otherwise the Conf configuration is not read.


In fact, the whole process of combing is also very simple.

First, the project to integrate Spring-session

1. Introduce the required JAR package

Add in 2.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>

Online there is said that if there are other filter needs to put this piece of code in front of all filter, in fact, it is not necessary.

3. Add in Applicationcontext.xml

<!--session Settings--
<bean class= "Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration ">
<property name= "Maxinactiveintervalinseconds" value= "3600" ></property>
</bean>
<!--redis Connection pool--
<bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig"/>

<!--Redis Connection factory--
<bean id= "ConnectionFactory" class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name= "HostName" value= "127.0.0.1"/>
<property name= "Port" value= "6379"/>
<property name= "password" value= ""/>
<property name= "Timeout" value= "20000"/>
<property name= "Poolconfig" ref= "Poolconfig" ></property>
</bean>

Since I am a local redis and do not have a password set, it is 127.0.0.1, the port default 6379, can be adjusted according to the Redis address itself.

At this point, the program session sharing has been completed, as long as the guarantee can start normally, when starting, Redis first start, otherwise it will error.


Second, build the cluster environment test

1. Configure Nginx

My two tomcat ports are 20001 and 21001 respectively, so Nginx is configured as follows:

Upstream local_tomcat{
Server 127.0.0.1:20001 weight=5;
Server 127.0.0.1:21001 weight=5;
}

server {
Listen 80;
server_name localhost;

Location/{
Proxy_pass Http://local_tomcat;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}

Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}

Nginx root directory Start cmd, execute start nginx can start Nginx. You can see if nginx starts properly from the process.


2. Test session

You can output <%= Session.getid ()%> on the main page of the project;

To differentiate Tomcat, I also output 1111 on the main page of the project under 20001tomcat.

21001tomcat The main page of the project was output 2222.

In the browser, access the LOCALHOST/project name/Main Page

Constantly F5 Refresh the page, you can see the value of <%= Session.getid ()%> is constant, but Tomcat is constantly switching.




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.