In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the analysis of the solution to the Nginx Session sharing problem, which is introduced in great detail through the sample code, which has a certain reference value for your study or work. Friends who need it can refer to it.
Nginx addresses Session sharing issues:
Load balancers made by 1.nginx or haproxy. Load balancers using nginx can add the configuration of ip_hash. Load balancers using haproxy can use the configuration of balance source, so that requests from one IP can be sent to the same server.
two。 Using database to synchronize session
3. Using cookie to synchronize session data, but the security is poor. Http requests need to take parameters, which increases the bandwidth consumption.
4.Tomcat configure session sharing
5 using session cluster to store Redis
1: create a project and start two Tomcat
2: write a servlet test
Package com.zn.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet ("/ nginxSessionServlet") public class SessionIPServlet extends HttpServlet {protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println ("current request port:" + request.getLocalPort ()); String action=request.getParameter ("action") / store a data if (action.equals ("setSession")) {request.getSession (). SetAttribute ("username", "zhangsan");} else if (action.equals ("getSession")) {response.getWriter (). Write ((String) request.getSession (). GetAttribute ("username")) in Session;}} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request,response);}}
3. Display the access effect without Nginx
Visit 8080 and 8081 respectively
4. Configure the nginx.conf file
Upstream myserver {ip_hash; server 127.0.0.1 ip_hash; server 8080; server 127.0.0.1 VR 8081;} server {listen 81; server_name www.bproject.com; location / {root html; proxy_pass http://myserver; index index.html index.htm;}}
5. Visit again
Method 2. Using spring-session+Redis to realize session sharing
1: import dependencies
Org.springframework.boot spring-boot-starter-redis org.springframework.session spring-session-data-redis
2: create a controller test
@ RestControllerpublic class SessionController {@ RequestMapping ("/ setSession") public String setSession (HttpServletResponse response, HttpServletRequest request) throws IOException {request.getSession (). SetAttribute ("username", "wang"); return "success";} @ RequestMapping ("/ getSession") public String getSession (HttpServletRequest request,HttpServletResponse response) {String username = (String) request.getSession (). GetAttribute ("username"); return username;}}
3:application.properties file
Server.port=8082#server.port=8083#redis configuration spring.redis.password: wang2003
4: start the project test
Conclusion: the scheme has the advantages of simple configuration, safe and stable data, high efficiency, and is widely used.
Note: if you delete this packet in Redis, ports 8082 and 8083 can no longer reach session, indicating that session does not exist in JVM, but is transferred to Redis.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.