This article mainly introduces the solution to the Nginx Session sharing problem. The example code in the article is very detailed and has a certain reference value for everyone's study or work. Friends in need can refer to it. Nginx solves the Session sharing problem: 1. Load balancing with nginx or haproxy. For load balancing with nginx, you can add the ip_hash configuration; for load balancing with haproxy, you can use the balance source configuration, so that requests using one IP are sent to the same server; 2. Use the database to synchronize sessions; 3. Use cookies to synchronize session data, but the security is poor, and http requests need to carry parameters, which increases bandwidth consumption; 4.Tomcat configures session sharing; 5. Use session cluster to store Redis; 1: Create a project and start two Tomcats 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 in Session if(action.equals("setSession")){ request.getSession().setAttribute("username","zhangsan"); }else if(action.equals("getSession")){ response.getWriter().write((String)request.getSession().getAttribute("username")); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } } 3. Access effect display without Nginx Access 8080 and 8081 respectively 4. Configure nginx.conf file upstream myserver{ ip_hash; server 127.0.0.1:8080; server 127.0.0.1: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: Use spring-session+Redis to implement session sharing 1: Import dependencies <!--Spring boot and redis application basic environment configuration--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <!--Spring session and redis application basic environment configuration, you need to enable redis before you can use it, otherwise you will get an error when starting Spring boot--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> 2: Create a controller test @RestController public 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 project testing Conclusion: This solution is easy to configure, data is secure and stable, efficient, and widely used; Note: After deleting this data packet in Redis, the session cannot be obtained on ports 8082 and 8083, which means that the session does not exist in the JVM but is transferred to Redis. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Vue-cli framework implements timer application
>>: Implementation of MySQL multi-version concurrency control MVCC
Preface Slow system calls refer to system calls t...
1. Introduction Recently I found that there are m...
Often you will encounter a style of <a> tag ...
Table of contents First, configure package.json T...
"Development is more than just writing code&q...
Recently, there is a particularly abnormal busine...
I reinstalled the system some time ago, but I did...
Servermanager startup connection database error R...
This article example shares the specific code of ...
Let me first explain why the text is not vertical...
Use apk add ansible to add the ansible service to...
Docker tag detailed explanation The use of the do...
Table of contents background 1. The query conditi...
A static node is fixed on a machine and is starte...
Table of contents Environmental Description Docke...