nginx+tomcat实现session共享

最近网站做负载均衡,为保证session能在两台机器上共享,题主参考网上各位大神的教程,实现了tomcat 下的session共享,亲测可用。现将一些配置和注意的地方分享给各位客官。


首先看nginx配置,这里如果做ip_hash的负载均衡,貌似session共享的意义不大。 如果做权重均衡,可以使用这种方式。先贴代码了。


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 {  #服务器集群名字   

 # ip_hash;  根据ip hash均衡

        server    127.0.0.1:18080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。 
        server    127.0.0.1:28080  weight=2; 
    }     

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

    location / { 
            proxy_pass http://netitcast.com; 
            proxy_redirect default; 
        } 


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

tomcat  session 共享


 博主用的tomcat 7.0 实现了。之前在做测试时,两台机器分别使用tomcat7.0 和tomcat8.0 结果各种问题,最后才发现 tomcat8.0不支持session共享配置


tomcat1   配置如下,主要是Cluster 的配置,在server.xml文件中 的  <Engine name="Catalina" defaultHost="localhost"> 节点下配置:


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
            channelSendOptions="6">

            <Manager className="org.apache.catalina.ha.session.DeltaManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"/>
            <!--
            <Manager className="org.apache.catalina.ha.session.BackupManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"
                mapSendOptions="6"/>
            -->
            <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService"
                    address="228.0.0.5"     # 注意自己的改成广播地址
                    bind="127.0.0.1"           # 注意改成自己的ip地址
                    port="45564"
                    frequency="500"
                    dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                    address="127.0.0.1"          # 注意改成自己的ip地址
                    autoBind="100"
                    port="4001"                           #使用一个端口接收
                    selectorTimeout="100"
                    maxThreads="6"/>
                <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                    <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
                </Sender>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            </Channel>
            <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

            <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
            <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>


tomcat2 配置


  <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
            channelSendOptions="6">

            <Manager className="org.apache.catalina.ha.session.DeltaManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"/>
            <!--
            <Manager className="org.apache.catalina.ha.session.BackupManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"
                mapSendOptions="6"/>
            -->
            <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService"
                    address="228.0.0.5"
                    bind="127.0.0.1"
                    port="45564"
                    frequency="500"
                    dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                    address="127.0.0.1"
                    autoBind="100"
                    port="4000" #两个端口不能一样
                    selectorTimeout="100"
                    maxThreads="6"/>
                <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                    <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
                </Sender>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
                <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            </Channel>
            <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

            <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
            <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>


还需要注意一点的是:   需要在你自己的web.xml中增加:   

     

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>



页面测试:  tomcat1 的web工程 index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    This is my tomcat1 <br>
    
    <%session.setAttribute("tomcat", "tomcat1");
    out.println((String)session.getAttribute("tomcat"));
    
     %>
    
  </body>
</html>



tomcat 2的 web工程,index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    This is my tomcat2 <br>
    
    <%//session.setAttribute("tomcat", "tomcat1");
    out.println((String)session.getAttribute("tomcat"));
    
     %>
    
  </body>
</html>










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值