欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx+redis实现负载均衡和session共享 负载均衡session共享 

程序员文章站 2022-04-12 09:47:39
...
nginx+redis实现session共享和负载均衡

最近想实现一个通过nginx做负载,然后共享session的东西
需要到的资源
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

首先是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;
       }    
    }

刚开始nginx一直报80端口冲突,按照网上的方法,怎么试怎么不行,最后放弃了,把80端口改成其他的了,我改成了8080,就能启动nginx了

再者是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" />

然后把上面的jar包放进tomcat的lib文件夹下,这里的redis配置和redis的搭建这里就详写了,这个很简单的,下个redis的安装包,安装就行。

然后在tomcat里面的ROOT底下新建一个index.jsp
<%@ 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自己下载替换。
然后,复制tomcat,改端口,此时我的两个tomcat的端口分别是8280,8380
好了,启动nginx和两个tomcat,访问http://localhost:8180/index.jsp
nginx+redis实现负载均衡和session共享
            
    
    
        负载均衡session共享 
nginx+redis实现负载均衡和session共享
            
    
    
        负载均衡session共享 




  • nginx+redis实现负载均衡和session共享
            
    
    
        负载均衡session共享 
  • 大小: 16.9 KB
  • nginx+redis实现负载均衡和session共享
            
    
    
        负载均衡session共享 
  • 大小: 18.8 KB