Nginx+redis realizes load balancing and session sharing

nginx+redis realizes session sharing and load balancing.

Recently , I want to implement a load through nginx and then share the session
. Resources needed
nginx-1.13.4
apache-tomcat-7.0.63
commons-pool2-2.3.jar
commons-pool- 1.2.jar
jedis-2.1.0.jar
tomcat-juli-8.0.23.jar
tomcat-redis-session-manager-1.2-tomcat-7.jar

First is the configuration of nginx
upstream cluster_redis {    
        server localhost:8280 max_fails=1 fail_timeout=60s;    
        server localhost:8380 max_fails=1 fail_timeout=60s;    

    }    
    server {
       listen 8180;    
       server_name localhost;    
       location / {    
        proxy_pass http://cluster_redis;
		proxy_set_header Host $host:$server_port;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Scheme $scheme;
		proxy_connect_timeout 10;
		proxy_read_timeout 10;
		proxy_send_timeout 10;
       }    
    }

At the beginning, nginx kept reporting port 80 conflicts. According to the online method, no matter what I tried, I finally gave up and changed port 80 to something else. I changed it to 8080, and then nginx can be started.

Furthermore , it is the configuration of tomcat.
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />    
	<Manager className="com.radiadesign.catalina.session.RedisSessionManager"    
             host="127.0.0.1"    
             port="6379"    
             database="0"    
			 password="123456"
             maxInactiveInterval="60" />

Then put the above jar package into the lib folder of tomcat. The redis configuration and redis construction here are detailed here. This is very simple. Just install the next redis installation package.

Then create a new index.jsp under ROOT in tomcat
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  

<%    
    String sessionId=session.getId(); //获取session ID号
    String userName=request.getParameter("userName");
	String password=request.getParameter("password");
	String logout=request.getParameter("logout");
	
    if(userName!=null&&!"".equalsIgnoreCase(userName.trim())){
		System.out.println("userName:"+userName);
        session.setAttribute("userName", userName);
    }
	
	if(logout!=null){
		session.setAttribute("userName", "");
		System.out.println("logout....");
	}
	System.out.println("tomcat 1");
	out.println("tomcat 1");
%>
<!DOCTYPE html>  
<html>  
<head>  
<title>test</title>  
<script src="jquery-1.8.3.min.js"></script>
<script>
function logout11(){
	$.ajax({
		type:"POST",
		url:"index.jsp",
		dataType:"json",
		data:{logout:"logout"},
		success:function(result) {
			location.href="index.jsp";
		},
		error:function(result){
			
		}
	});
}

function login(){
	var userName=$("#userName").val();
	
	$.ajax({
		type:"POST",
		url:"index.jsp",
		dataType:"json",
		data:{userName:userName},
		success:function(result) {
			alert(1111);
			location.href="index.jsp";
		},
		error:function(result){
			
		}
	});
}
</script>
</head>
<body>  
<div>
current sessionID:<%=sessionId %>
</div>
<%if(session.getAttribute("userName")!=null&&!"".equals((String)session.getAttribute("userName"))){%>
<div>
userName:<%=(String)session.getAttribute("userName")%>
<p>
<form action="index.jsp" method="post">
	<input name="logout" type="hidden">
	<input type="button" value="logout" onclick="logout11()">
</form>
</p>
</div>
<%}else {%>
<div>
	<form action="index.jsp" method="post">
		<table>
			<tr>
				<td>userName:</td>
				<td><input id="userName" name="userName" type="text" /></td>
			</tr>
			<tr>
				<td>password:</td>
				<td><input name="password" type="text" /></td>
			</tr>
		</table>
		<p>
			<input type="button" value="login" onclick="login()">
		</p>
	</form>
</div>
<%}%>
</body>  
</html>

jQuery js download and replace by itself.
Then, copy tomcat and change the port. At this time, the ports of my two tomcats are 8280 and 8380 respectively
. Start nginx and two tomcats and visit http://localhost:8180/index.jsp






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326224393&siteId=291194637