tomcat7下配置session复制和nginx做负载均衡

 2023-09-05 阅读 209 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 基于Nginx+Tomcat集群的Session共享 (如果启用了防火墙请开发配置中用到的端口) 在Linux环境下 第一步:下载对应的nginx 和 tomcat 包并解压、安装、配置(此步骤之前文章有提过,在此省略。。&#

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

基于Nginx+Tomcat集群的Session共享

(如果启用了防火墙请开发配置中用到的端口)

在Linux环境下 
第一步:下载对应的nginx 和 tomcat 包并解压、安装、配置(此步骤之前文章有提过,在此省略。。) 

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;pid        logs/nginx.pid;events {worker_connections  1024;
}http {include mime.types;default_type  application/octet-stream;#隐藏nginx的版本号server_tokens off;#自定义日志格式log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''upstream_response_time $upstream_response_time $upstream_addr' '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  combined;#access日志存放路径和格式access_log  logs/access.log  main;#提升文件传输性能sendfile on;#tcp_nopush on;#设置客户端能够请求的单个文件大小client_max_body_size 300m;keepalive_timeout  75;#proxy参数#跟后端服务器连接的超时时间,发起握手等候响应超时时间proxy_connect_timeout 5;#连接成功后,等候后端服务器的响应时间proxy_read_timeout 600;#后端服务器数据回传时间proxy_send_timeout 600;#代理请求缓存区proxy_buffer_size 16k;#同上,告诉nginx保存单个用的几个buffer、最大用多空间proxy_buffers 4 64k;#如果系统很忙时可以申请更大的proxy_buffers,官方推荐*2proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;#开启压缩功能gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_comp_level 2;#压缩级别从低到高1-9gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;#设置负载均衡池,此处是测试环境中的2个Tomcat的IP地址和端口号。upstream tomcat_pool {server 192.168.1.198:8081;server 192.168.1.198:8082;}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   /usr/local/nginx/html/dist;index  views/index.html;}location /api/v1 {proxy_pass   http://tomcat_pool;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

Tomcat在linux的基本配置

第一处端口修改:Xml代码

<!--  修改port端口:18005, 5个tomcat不能重复,端口随意,别太小。本文测试中的5个tomcat设置分别为:18005,18006,18007,18008,18009-->   
<Server port="18005" shutdown="SHUTDOWN">

 

第二处端口修改:xml代码


<!-- port="18081" tomcat监听端口,随意设置,别太小 本文档中,5个tomcat分别设置为:8801-8443;8802-8444;8803-8445;8804-8446;8805-8447.-->  
<Connector port="8801" protocol="HTTP/1.1" maxThreads="1500" maxSpareThreads="500"  minSpareThreads="100"acceptCount="1000" enableLookups="false"  connectionTimeout="20000"redirectPort="8443"URIEncoding="UTF-8" />

 

第三处端口修改:XML代码

<!-- 本文档中,5个tomcat分别设置为:8005-8443;8006-8444;8007-8445;8008-8446;8009-8447.-->
<Connector port="8005" protocol="AJP/1.3" redirectPort="8443" />

 

Engine元素增加jvmRoute属性:xml代码

<!-- 本文档中,5个tomcat分别设置为:node1,node2,node3,node4,node5.-->
<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1"> 

 

集群配置 
Tomcat集群配置主要是在conf目录下的server.xml中的<Engine>添加以下代码:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">           <Manager className="org.apache.catalina.ha.session.DeltaManager"expireSessionsOnShutdown="false"  notifyListenersOnReplication="true"/>   <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <--此处可以不做修改,Tomcat集群的默认组播地址就是228.0.0.4,默认端口为:45564-->  <Membership className="org.apache.catalina.tribes.membership.McastService"address="228.0.0.4"  port="45564"frequency="500" dropTime="3000"/>  <--此处需要根据需要进行修改,address为Tomcat所在主机的IP地址,port为接收组播消息的端口,默认为4000到5000,可以在该范围内随意设置。确保集群内端口号唯一即可。-->    <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"address="10.0.5.43"    port="4001"   autoBind="100"    selectorTimeout="5000"    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"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>  </Channel>  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>   <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/><Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  tempDir="/tmp/war-temp/"     deployDir="/tmp/war-deploy/"     watchDir="/tmp/war-listen/"      watchEnabled="false"/>  <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>   <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

 最后,在需要进行session共享的应用中WEB-INF目录下的web.xml中添加属性。否则将不会实现Tomcat集群应用中的session数据共享。

<distributable/>

上述代码的详细说明: 
1、Cluster

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

 

1)Tomcat集群主元素,在这个元素里面可以配置集群的所有详细信息 
2)className 主要的集群类,当前只提供了 org.apache.catalina.ha.tcp.SimpleTcpCluste作为实现类 
3)channelSendOptionssession发送方式,默认值是8,这个标识确定通过SimpleTcpCluste如何发送消息 
4)Channel.SEND_OPTIONS_SYNCHRONIZED_ACK = 0x0004 
Channel.SEND_OPTIONS_ASYNCHRONOUS = 0x0008 
Channel.SEND_OPTIONS_USE_ACK = 0x0002 
如果使用(ASYNCHRONOUS)加(USE_ACK)方式来发送消息,那么值应该是10(8+2)或者0x000B 
如果使用(SYNCHRONIZED_ACK)加(USE_ACK)方式来发送消息,那么值应该是6(4+2)或者0x0006 
2、Manager

 <Manager className="org.apache.catalina.ha.session.BackupManager"expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> 

 

1)管理tomcat之间的session复制 
2)className当前有两个实现类:org.apache.catalina.ha.session.DeltaManager和org.apache.catalina.ha.session.BackupManager 
3)DeltaManager复制并发送Session数据到集群下所有的节点,这个实现类被证明非常可靠、运行得非常好。不过有个局限性就是集群的节点类型要相同,而且要部署相同的应用程序 
4)expireSessionsOnShutdown当一个web程序被结束时,tomcat分发销毁命令到每个Session,并通知所有session listener执行。当集群下某个节点被停止时,如果想销毁所有节点下的的Session,设置为true,默认为false 
5)notifyListenersOnReplication如果设置为true,当session属性被复制和移动的时候,session listener被通知 
3、Channel

<Channel className="org.apache.catalina.tribes.group.GroupChannel">

 

Channel是Apache Tribes的主组件,channel管理一组子组件,并和它们一起组成了tomcat实例间的通讯框架。在tomcat集群中,DeltaManager通过SimpleTcpCluster调用channel来实现信息传递,而BackupManager自己调用channel以及子组件这些组件来实现信息传递。ReplicatedContext也会调用channel传递context属性。 
4、Membership

<Membership  className="org.apache.catalina.tribes.membership.McastService"   address="228.0.0.4" port="45564"  frequency="500" dropTime="3000"/> 

 

MemberShip组件自动检索发现集群里的新节点或已经停止工作的节点,并发出相应的通知。默认使用组播(Multicast)实现。 
5、Receiver

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  address="auto" port="5000"  selectorTimeout="100" maxThreads="6"/>

 

负责监听接收其他节点传送过来的数据。默认使用non-blocking TCP Server sockets。 
6、Sender

<Sender  className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  <Transport  className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> 
</Sender>

 

管理从一个节点发送到另外一个节点的出站连接和数据信息,允许信息并行发送。默认使用TCP Client Sockets。 
7、Interceptor

<Interceptor  className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  <Interceptor  className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  <Interceptor  className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>  

 

Channel通过Interceptor堆栈进行消息传递,在这里可以自定义消息的发送和接收方式,甚至MemberShip的处理方式。 
8、Value

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>

 

1)Value在调用Http Request 链中起着拦截器的作用,用来决定什么情况下数据需要被复制。 
org.apache.catalina.ha.tcp.ReplicationValve,ReplicationValue在Http Request结尾判断当前数据是否需要被复制。 
2)Filter内容为url或者文件结尾,当访问链接配置filter时,不论实际session有没有改变,集群会认为session没有任何变化,从而不会复制和发送改变的session属性。 
9、Deployer

<DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>

 

使集群支持farmed deployment 
10、ClusterListener

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

 

Clusterlistener用来追踪信息发送和接收。 
ClusterSessionListener用来监听集群组件接收信息,当使用DeltaManager的时候,信息被集群接收,并通过ClusterSessionListener传递给Session Manager。

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3575212/blog/1586440

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/122.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息