2年前一直折腾Apache,现如今更习惯Nginx。![]() ![]() ![]() 今天简要说明一下Nginx+Tomcat负载均衡实现,重点介绍Nginx+Tomcat+Session共享实现。 相关内容: 征服 Apache + SSL 征服 Apache + SVN 征服 Apache + SVN + LDAP 征服 Apache + Tomcat 征服 Nginx 征服 Nginx + Tomcat Nginx负载均衡,其实主要就是用upstream、server指令,再配以权重等等参数。如果为了让nginx支持session共享,还需要额外增加一个模块。 一、Nginx负载均衡 在http{...}中配置一个upstream{...},参考如下: 引用
upstream tomcat { server 10.11.155.26:8080; server 10.11.155.41:8080; } 接着修改location节点,配置代理: 引用
location / { ... proxy_pass http://tomcat; ... } 当访问根路径时,会轮播路由到两台服务器上,至于后端服务器是tomcat还是jetty之类的,都无所谓,照葫芦画瓢就是了。 ![]() 当然,有的机器性能好,或者负载低,可以承担高负荷访问量,可以通过权重(weight),提升访问频率。数值越高,被分配到的请求数越多。 server指令参数如下:
例如,可以这样配置: 引用
upstream tomcat { server 10.11.155.26:8080 weight=5; server 10.11.155.41:8080 weight=10; } 后者分得的请求数就会较高。 二、Nginx+Tomcat+Session共享 为了让Nginx支持Tomcat的Session共享,需要对其升级,增加jvmroute模块。 1.下载nginx-upstream-jvm-route组件、对nginx源码做补丁。 我把nginx-upstream-jvm-route下载到了/opt/software路径下。 先切换到nginx源码目录下,执行:
patch -p0 < /opt/software/nginx_upstream_jvm_route/jvm_route.patch 引用
patching file src/http/ngx_http_upstream.c Hunk #1 succeeded at 4095 (offset 358 lines). Hunk #3 succeeded at 4227 (offset 358 lines). Hunk #5 succeeded at 4326 (offset 358 lines). patching file src/http/ngx_http_upstream.h Hunk #1 succeeded at 90 (offset 5 lines). Hunk #3 succeeded at 118 (offset 5 lines). 说明补丁做好了! ![]() 2.升级nginx 先别急着折腾nginx-upstream-jvm-route,先看看nginx当时安装时的参数:
nginx -V 引用
nginx version: nginx/1.2.0 configure arguments: --prefix=/opt/servers/nginx --user=nginx --group=www --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre=/opt/software/pcre-8.10 --with-zlib=/opt/software/zlib-1.2.5 --with-http_stub_status_module --with-http_realip_module --with-http_gzip_static_module 记得先备份nginx.conf! ![]() 使用追加参数(--add-module),增设nginx-upstream-jvm-route模块,--add-module=/opt/software/nginx_upstream_jvm_route,编译安装。
./configure --prefix=/opt/servers/nginx --user=nginx --group=www --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-pcre=/opt/software/pcre-8.10 --with-zlib=/opt/software/zlib-1.2.5 --with-http_stub_status_module --with-http_realip_module --with-http_gzip_static_module --add-module=/opt/software/nginx_upstream_jvm_route && make && make insatll 如果没有错误提示,nginx就成功升级了! ![]() 3.修改upstream配置 要让Nginx支持Tomcat的jvmRoute,并共享session,在upstream下作如下修改: 引用
upstream tomcat { server 10.11.155.26:8080 srun_id=tomcat1; server 10.11.155.41:8080 srun_id=tomcat2; jvm_route $cookie_JSESSIONID|sessionid reverse; } srun_id跟tomcat配置有关。 4.Tomcat集群配置(Tomcat6、7通用) 该配置参考征服 Apache + Tomcat,以下仅作简要说明。 a.修改server.xml 找到Engine节点,并设置jvmRoute,这里指定tomcat1。
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> 可以直接粘贴以下代码,并对应修改Receiver节点中的address属性,指向本机:
<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"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="224.0.0.0" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="10.11.155.26" port="4000" 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"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> 注:如果本机上有多个tomcat并存,Receiver节点中的port属性,使其绑定在不同的端口上。 Membership节点address属性配置多播地址,可使用route命令将其打开,参考如下:
route add -net 224.0.0.0/8 dev eth0 b.修改应用的web.xml 在web.xml末尾增加<distributable />
<web-app> ... <distributable /> </web-app> 至此,已完成所有配置,重启tomcat、nginx,访问服务测试页面(见附件),强行关闭其中一台tomcat,令请求转向另一个台tomcat,测试session是否同步: ![]() session共享成功,非粘性实现。 ![]() |
|