nginx+tomcat+redis(session共享)实现负载均衡

(此文章不介绍安装redis和tomcat)

  1. redis的session共享的包和nginx的windows版本下载地址
链接:http://download.csdn.net/download/u014464624/10103218
  1. 安装windows版nginx,在虚拟机上安装两台tomcat服务器,我采用不同的端口号区分两台tomcat服务器
    nginx的安装目录
cmd窗口关闭nginx命令: nginx.exe -s stop
  1. 配置nginx.conf的配置文件
 worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致  

events {  
    worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数)  
}  

http {  
    include       mime.types; #文件扩展名与文件类型映射表  
    default_type  application/octet-stream;#默认文件类型  

    sendfile        on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。  

    keepalive_timeout  65; #长连接超时时间,单位是秒  

    gzip  on;#启用Gizp压缩  

    #服务器的集群  
    upstream  netitcast.com {  #服务器集群名字,windows需要修改hosts文件   
        server    192.168.1.159:18080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
        server    192.168.1.159:28080  weight=2;  
    }     

    #当前的Nginx的配置  
    server {  
        listen       80;#监听80端口,可以改成其他端口  
        server_name  localhost,netitcast.com;##############   当前服务的域名  

    location ~.*\.(jsp|action)$ {  
            proxy_pass http://netitcast.com;  
            proxy_redirect default;  
        }  

    location ~* .(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {   
            root   static;  
            expires     7d; #过期时间为7天
        }  
    location ~* .(js|css)$ {  
            root   static;  
            expires     6h; #过期时间为7天
    }   

    error_page   500 502 503 504  /50x.html;  
    location = /50x.html {
         root   html;
       }    

    }  
}  
  1. 在nginx的安装目录下配置静态资源
    静态资源
构建路径和项目发布路径保持一致,我的项目地址为http://ip:端口(域名)/data,我的js加载地址为
http://ip:端口(或者域名)/data/appzhongzhi/tymanager/config/js/query.js
  1. 将commons-pool2-2.3.jar,jedis-2.7.2.jar和tomcat-redis-session-1.0-SNAPSHOT.jar放到tomcat的lib文件夹下

  2. 配置tomcat的context.xml添加redis配置信息

      <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
        <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
         host="192.168.1.122"   
         port="6379"   
         database="1" 

         maxInactiveInterval="60" />   
tomcat1的struts.jsp
<%
System.out.print("tomcat1的sessionId为:"+session.getId());
response.sendRedirect("config-browser/actionNames.action");
%>

tomcat2的struts.jsp
<%
System.out.print("tomcat2的sessionId为:"+session.getId());
response.sendRedirect("config-browser/actionNames.action");
%>

访问struts.jsp第一次访问的是tomat2
多刷新几次,第一次访问的是tomcat2服务器,随后随机访问的tomcat1服务器
访问struts.jsp随后随机访问的是tomat1

他们的sessionId都是一样的

  • 加载图片资源,两个tomcat服务器都不存在01.jpg,将图片资源放到nginx\nginx-1.12.2\static\data\upload
tomat服务器(注意是两个,我这里写了一个)项目根路径下放一个index1.jsp,代码内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>这是tomcat2的访问页面</title>
</head>
<body>
      <center>    
          <p>this is tomcat2 example</p>
          <p>sessionId为:<%=session.getId()%></p>
          <img alt="这是个妹子" src="./upload/01.jpg">
      </center>
</body>
</html>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值