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 spring+redis to realize session sharing

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use spring+redis to achieve session sharing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When using Nginx+Tomcat for load balancing, you want to use polling for load balancing. However, if polling is used, different Tomcat may be accessed, and if there is no Session sharing, it is equivalent to a new Session. For example, all existing systems require authentication and login. If there is no Session share, it will cause the user to log out.

Currently, there are several ways to achieve session sharing:

1. Use Tomcat's built-in Session replication scheme

It is only suitable for Tomcat small clusters, but not for large clusters, because session replication is the way of all to all.

2. Use third party (personal) Session management based on Tomcat.

Third-party support is insufficient, especially for Tomcat8.

3. Use Spring Session to implement

This article introduces the third solution, that is, the scheme of using spring session + redis

First, introduce pom org.springframework.session spring-session-data-redis 1.3.0.RELEASE pom biz.paluch.redis lettuce 3.5.0.Final redis.clients jedis 2.9.02.Configuring web.xml

Add the following filter to the web.xml. Note that if there are other filters in the web.xml, generally the Spring Session filter should be placed first.

SpringSessionRepositoryFilter org.springframework.web.filter.DelegatingFilterProxy springSessionRepositoryFilter / * III, configure spring-mvc.xml IV, edit redis.propertiesredis.ip=127.0.0.1redis.port=6379redis.password=123456 V, test code

Write a request method to forward to session_share.jsp

RequestMapping ("/ session") public String session (HttpSession session, HttpServletRequest request) {request.setAttribute ("id", request.getSession (). GetId ()); return "session_share";}

Then write session_share.jsp

Tomcat1 session shares 1 my session:$ {id}

Sessionid=---tomcat2 session share 2 my session:$ {id}

Sessionid= VI. Configure nginx load balancer upstream load_balance_server {# weigth parameter to indicate the weight. The higher the weight, the greater the probability of being assigned. Server 192.168.0.131 server 8080 weight=1; server 192.168.0.167 upstream load_balance_server 8090 weight=1;} server {listen 80; server_name localhost; # charset koi8-r # access_log logs/host.access.log main; location / {proxy_pass http://load_balance_server; index dashboard index; proxy_set_header Host $http_host; 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;}}

The IP of nginx is 192.168.0.131.

The sessionId on the 192.168.0.131:80/session page has not changed after many visits in a row. 1 and 2 are constantly switching.

"how to use spring+redis to achieve session sharing" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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