Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis3.0 to realize Session sharing

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use Redis3.0 to achieve Session sharing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Redis3.0 to achieve Session sharing.

Tomcat version: Tomca8

If you are not familiar with this chapter, you can refer to https://my.oschina.net/u/3155476/blog/3070575(Keepalived + Nginx to achieve highly available Web load balancing.

1. Access http://192.168.186.129:8080/dubbo-web-boss/login.do on a single node:

2. Add one more consumer node: 192.168.186.129 to deploy the dubbo-web-boss project in the same way. (the same principle of another consumer node, which will not be described many times here)

First verify that the new node can also access the http://192.168.186.129:8080/dubbo-web-boss/login.do normally.

3. Synchronously add the following two configurations to the three nodes in the reverse proxy cluster composed of Keepalived+Nginx:

User root

Worker_processes 1

# error_log logs/error.log

# error_log logs/error.log notice

# error_log logs/error.log info

# pid logs/nginx.pid

Events {

Worker_connections 1024

}

Http {

Include mime.types

Default_type application/octet-stream

# log_format main'$remote_addr-$remote_user [$time_local] "$request"'

#'$status $body_bytes_sent "$http_referer"'

#'"$http_user_agent"$http_x_forwarded_for"'

# access_log logs/access.log main

Sendfile on

# tcp_nopush on

# keepalive_timeout 0

Keepalive_timeout 65

# gzip on

# # web-boss

Upstream web_boss {

Server 192.168.186.129:8080 weight=1 max_fails=2 fail_timeout=30s

Server 192.168.186.132:8080 weight=1 max_fails=2 fail_timeout=30s

Server 192.168.186.133:8080 weight=1 max_fails=2 fail_timeout=30s

}

Server {

Listen 88

Server_name localhost

# charset koi8-r

# access_log logs/host.access.log main

Location / {

Root html

Index index.html index.htm

}

# error_page 404 / 404.html

# redirect server error pages to the static page / 50x.html

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

# # web-boss Cluster

Location / dubbo-web-boss {

Root html

Index index.html index.htm

Proxy_pass http://web_boss/dubbo-web-boss;

Proxy_set_header Host $http_host

Proxy_set_header Cookie $http_cookie

Proxy_set_header X-Real-IP $remote_addr

Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

Proxy_set_header X-Forwarded-Proto $scheme

Client_max_body_size 100m

}

}

}

4. Restart Nginx

# / usr/local/nginx/sbin/nginx-s reload

5. When you access dubbo-web-boss through the VIP of the reverse proxy cluster, sometimes you can log in successfully, but sometimes you will be prompted with an error in the verification code, because the Session is not synchronized.

Http://192.168.186.50:88/dubbo-web-boss/login.do

The next step is to solve the Session sharing problem of Tomcat, using open source projects:

Https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0.2

Note that because we are using Redis3.0 clusters, the corresponding plug-ins must support Redis3.0 clusters.

192.168.186.129:7111

192.168.186.129:7112

192.168.186.132:7113

192.168.186.132:7114

192.168.186.133:7115

192.168.186.133:7116

6. Click the red box to download the latest version of tomcat-cluster-redis-session-manager

Extract and find the dependent jar package in the lib directory

And upload these jar packages to the lib directory in Tomcat8

Find the properties under the conf directory

Edit the file as follows, adjust as needed

8. Add the environment variable of Tomcat (optional)

Catalina.base= "/ home/huangkejie/dubbo/web/tomcat8.5.40/"

9. Add the following two lines to conf/context.xml in Tomcat8:

11. Check and confirm the Session timeout of Tomcat in the conf/web.xml of Tomcat. The default is 30 minutes.

thirty

Can be modified as needed.

Finally, the author's readme description is attached.

/ * Tomcat clustering with Redis data-cache implementation. * * author: Ranjith Manickam * / Supports:-Apache Tomcat 7-Apache Tomcat 8-Apache Tomcat 9Pre-requisite: 1.jedis.jar 2.commons-pool2.jar 3.slf4j-api.jarmore details.. Https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wikiSteps to be done 1. Move the downloaded jars to tomcat/lib directory-tomcat/lib/ 2. Add tomcat system property "catalina.base"-catalina.base= "/ home/huangkejie/dubbo/web/tomcat8.5.40/" 3.Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory -tomcat/conf/redis-data-cache.properties 4. Add the below two lines in tomcat/conf/context.xml 5.Verify the session expiration time (minutes) in tomcat/conf/web.xml 60 Note:-All your session attribute values must implement java.io.Serializable. -Supports redis default, sentinel and cluster based on the redis-data-cache.properties configuration. At this point, I believe you have a deeper understanding of "how to use Redis3.0 to achieve Session sharing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report