Home  >  Article  >  Backend Development  >  session sharing

session sharing

WBOY
WBOYOriginal
2016-07-30 13:30:091208browse
Due to the concurrency bottleneck problem of tomcat, it can be said that almost all web applications using tomcat have session desynchronization problems. Learning from information on the Internet, I also found time to experiment. The downloading and installation of software involved in this article will be skipped one by one. I think you don’t need to read it. Note: This article will not make any verbal discussion about memcached and redis. I hope all netizens will ask Google and Du Niang themselves. (In my humble opinion, as a software, they can get many supporters of their own. They must have their own advantages. The key is to choose the one that suits you based on actual needs.)一, nginx+tomcat+memcached (Dependency package download)

1.memcached configuration: (v1.4.13)

Node 1 (192.168.159.131:11444)

Node 2 (192.168.159.131:11333)

2.tomcat configuration

tomcat1 (192.168.159.128:8081)

tomcat2 (192.168.159.128:8082)

3.nginx is installed at 192.168.159.131.

First, configure tomcat so that it saves the session to memcached. There are two methods:

Method 1: Configure in server.xml.

Find the host node and add

 

Method 2: Configure in context.xml.

Find the Context node and add

Secondly, configure nginx to test that the session remains shared. upstream  xxy.com  {       server   192.168.159.128:8081 ;       server   192.168.159.128:8082 ; } log_format  www_xy_com  '$remote_addr - $remote_user [$time_local] $request '                '"$status" $body_bytes_sent "$http_referer"'                 '"$http_user_agent" "$http_x_forwarded_for"'; server {       listen  80;       server_name  xxy.com;       location / {                proxy_pass        http://xxy.com;                proxy_set_header   Host             $host;                proxy_set_header   X-Real-IP        $remote_addr;                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;       }       access_log  /data/base_files/logs/www.xy.log  www_xy_com; }

Finally, put your application into two tomcats, and start memcached, tomcat, and nginx in sequence. Access your nginx and you will find that the sessions in the two tomcats can remain shared.

2. nginx+tomcat+redis (Dependency package download)

1.redis configuration (192.168.159.131:16300) (v2.8.3)

2.tomcat configuration

tomcat1( 192.168.159.130:8081)

tomcat2 (192.168.159.130:8082)

3.nginx is installed at 192.168.159.131.

First, configure tomcat so that it saves the session to redis. There are two methods, which are also configured in server.xml or context.xml. The difference is that memcached only needs to add a manager tag, while redis needs to add the following content: (Note: the valve tag must be in front of the manager.)

Secondly, configure nginx to keep the test session shared.

upstream  redis.xxy.com  {       server   192.168.159.130:8081;       server   192.168.159.130:8082; } log_format  www_xy_com  '$remote_addr - $remote_user [$time_local] $request '                '"$status" $body_bytes_sent "$http_referer"'                 '"$http_user_agent" "$http_x_forwarded_for"'; server {       listen  80;       server_name redis.xxy.com;        location / {                proxy_pass        http://redis.xxy.com;                proxy_set_header   Host             $host;                proxy_set_header   X-Real-IP        $remote_addr;                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;       }       access_log  /data/base_files/logs/redis.xxy.log  www_xy_com; }

Finally, put your application into two tomcats, and start redis, tomcat, and nginx in sequence. Access your nginx and you will find that the sessions in the two tomcats can remain shared.

In the above article, one thing needs to be explained: If the manager is placed in server.xml in the tomcat configuration, then when using maven for hot deployment, it will fail. Therefore, I recommend placing it in context.xml.

The above has introduced session sharing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn