<div class="output\_wrapper" id="output\_wrapper\_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;"><h3 id="nginx" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193) none repeat scroll 0% 0%; color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-left: 5px; margin-right: 2px;">一、Nginx的概述</span></h3> <h2 id="1nginx" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">1、什么是Nginx:</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。<br>其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。<br>Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。</p> <h2 id="2nginx" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">2、为什么使用Nginx:</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">互联网飞速发展的今天,大用户量高并发已经成为互联网的主体.怎样能让一个网站能够承载几万个或几十万个用户的持续访问呢?这是一些中小网站急需解决的问题。用单机tomcat搭建的网站,在比较理想的状态下能够承受的并发访问量在150到200左右。按照并发访问量占总用户数量的5%到10%这样计算,单点tomcat网站的用户人数在1500到4000左右。对于一个为全国范围提供服务的网站显然是不够用的,为了解决这个问题引入了负载均衡方法。负载均衡就是一个web服务器解决不了的问题可以通过多个web服务器来平均分担压力来解决,并发过来的请求被平均分配到多个后台web服务器来处理,这样压力就被分解开来。<br>负载均衡服务器分为两种一种是通过硬件实现的负载均衡服务器,简称硬负载例如:f5。另一种是通过软件来实现的负载均衡,简称软负载:例如apache和nginx。硬负载和软负载相比前者作用的网络层次比较多可以作用到socket接口的数据链路层对发出的请求进行分组转发但是价格成本比较贵,而软负载作用的层次在http协议层之上可以对http请求进行分组转发并且因为是开源的所以几乎是0成本,并且阿里巴巴,京东等电商网站使用的都是Nginx服务器。</p> <h3 id="nginx-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193) none repeat scroll 0% 0%; color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-left: 5px; margin-right: 2px;">二、Nginx完成负载均衡</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">完成Nginx负载均衡,那么需要先来介绍Tomcat的安装和配置,我们首先要来配置Tomcat完成集群的配置.因为我们没有多台服务器运行Tomcat.那么我们可以模拟在一台服务器上运行多个Tomcat程序.</p> <h2 id="21tomcattomcat" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">2.1、Tomcat配置Tomcat集群:</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">步骤一:下载Tomcat:</strong></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">http://tomcat.apache.org/download-90.cgi<br><img src="http://img.blog.csdn.net/20180201112014216?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">步骤二:安装和配置Tomcat:</strong></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">直接将下载后的Tomcat解压在本地磁盘:解压两个分别命名为tomcat1和tomcat2.<br><img src="http://img.blog.csdn.net/20180201112030362?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><br>配置tomcat环境变量:<br> <img src="http://img.blog.csdn.net/20180201112039647?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><br> <img src="http://img.blog.csdn.net/20180201112047090?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">分别完成如下配置:(需要将tomcat带有端口号的地方改成不同的端口即可.)分别打开两个tomcat的conf下的server.xml</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">tomcat1/conf/server.xml:</strong></p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs xml" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: white none repeat scroll 0% 0%; color: black; display: block !important; word-wrap: normal !important; word-break: normal !important; white-space: pre !important; overflow: auto !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Server</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8005"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">shutdown</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"SHUTDOWN"</span>></span><br><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Connector</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8080"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">protocol</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"HTTP/1.1"</span><br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">connectionTimeout</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"20000"</span><br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">redirectPort</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8443"</span> /></span><br><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Connector</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8009"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">protocol</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"AJP/1.3"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">redirectPort</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8443"</span> /></span><br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">tomcat2/conf/server.xml:</strong></p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs xml" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: white none repeat scroll 0% 0%; color: black; display: block !important; word-wrap: normal !important; word-break: normal !important; white-space: pre !important; overflow: auto !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Server</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"80065"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">shutdown</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"SHUTDOWN"</span>></span><br><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Connector</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8081"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">protocol</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"HTTP/1.1"</span><br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">connectionTimeout</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"20000"</span><br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">redirectPort</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8443"</span> /></span><br><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Connector</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8010"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">protocol</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"AJP/1.3"</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">redirectPort</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"8443"</span> /></span><br></code></pre> <h3 id="nginx-2" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193) none repeat scroll 0% 0%; color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-left: 5px; margin-right: 2px;">三、Nginx的安装和部署</span></h3> <h2 id="31nginx" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">3.1、Nginx的安装</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">将nginx-1.8.0.zip解压包某个盘符下运行:目录结构如下:<br><img src="http://img.blog.csdn.net/20180201112333084?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><br>双击nginx.exe即可运行:打开浏览器http://localhost:80显示如下页面:<br><img src="http://img.blog.csdn.net/20180201112341085?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"><br>说明安装成功!!!</p> <h2 id="32nginx" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">3.2、Nginx的负载均衡的配置</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">打开C:\\nginx-1.8.0\\conf\\nginx.conf这个文件:<br><img src="http://img.blog.csdn.net/20180201112550490?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"></p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">修改:C:\\Windows\\System32\\drivers\\etc\\hosts文件</p> <h3 id="tomcat" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid rgb(0, 172, 193);"><span style="font-size: inherit; line-height: inherit; margin: 0px; display: inline-block; font-weight: normal; background: rgb(0, 172, 193) none repeat scroll 0% 0%; color: rgb(255, 255, 255); padding: 3px 10px 0px; border-top-right-radius: 3px; border-top-left-radius: 4px; margin-left: 5px; margin-right: 2px;">四、tomcat共享配置</span></h3> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">通过以上的配置我们已经可以通过访问www.taoge.com访问到不同的tomcat来分担服务器端的压力.请求负载过程中会话信息不能丢失.那么需要在多个tomcat中session需要共享.</p> <h2 id="41tomcatsession" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">4.1、配置Tomcat的session共享可以有三种解决方案</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">第一种</strong>是以负载均衡服务器本身提供的session共享策略,每种服务期的配置是不一样的并且nginx本身是没有的。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">第二种</strong>是利用web容器本身的session共享策略来配置共享。针对于weblogic这种方式还是靠普的。但是针对于tomcat这种方式存在很大的缺陷,主要因为是依靠广播方式来实现的session复制,会浪费很多带宽导致整个网络反映缓慢。官网也建议这种方式最好不要超过4台tomcat,具体的内容可参考/webapps/docs/cluster-howto.html里面有详细的说明。下面是具体的配置过程</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">第三种</strong>是Tomcat集群+redis的Session共享配置方法.<br>在这里我们以第二种方式为例</p> <h2 id="42tomcatsession" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1.4em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">4.2、配置Tomcat中session的共享</span></h2> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">步骤一:</strong>修改server.xml文件,最简单的集群配置只需要将节点中注释掉的下面这句取消注释即可:<br>Xml代码:<br><code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248) none repeat scroll 0% 0%;"> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> </code> <br>使用这样方法配置的集群会将Session同步到所在网段上的所有配置了集群属性的实例上(此处讲所在网段可能不准确,是使用Membership 的address和port来区分的。tomcat集群的实例如果在Membership配置中有相同的address和port值的tomcat被分到同一个集群里边。他们的session是相互共享的,同一个session的集群被称为一个cluster。可以配置多个cluster,但是cluster和cluster之间的session是不共享的)。也就是说如果该广播地址下的所有Tomcat实例都会共享Session,那么假如有几个互不相关的集群,就可能造成Session复制浪费,所以为了避免浪费就需要对节点多做点设置了,如下:<br>Xml代码</p> <pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs xml" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: white none repeat scroll 0% 0%; color: black; display: block !important; word-wrap: normal !important; word-break: normal !important; white-space: pre !important; overflow: auto !important;"> <span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Cluster</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">className</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"org.apache.catalina.ha.tcp.SimpleTcpCluster"</span>></span> <br> <span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Channel</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">className</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"org.apache.catalina.tribes.group.GroupChannel"</span>></span> <br> <span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"><<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Membership</span> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">className</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"org.apache.catalina.tribes.membership.McastService"</span> <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">address</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"228.0.0.4"</span> <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">port</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"45564"</span> <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">frequency</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"500"</span> <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(102, 0, 102); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">dropTime</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 136, 0); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">"3000"</span>/></span> <br> <span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Channel</span>></span> <br> <span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;"></<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(0, 0, 136); word-wrap: inherit !important; word-break: inherit !important; white-space: inherit !important;">Cluster</span>></span> <br></code></pre> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">加了一个Channel,里面包了个Membership,咱们要关注的就是membership的port属性和address属性,不同的集群设置不同的port值或address值,从目前的使用来看,基本上是隔离开了。</p> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; font-weight: bold;">步骤二:</strong>修改项目的web.xml文件:<br>web.xml文件的修改很简单:只需要在节点中添加这个节点<distributable style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">就可以了。<br>OK,有了这二步就实现了Tomcat的集群和Session的共享了。</distributable></p> <blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251) none repeat scroll 0% 0%; overflow: auto; overflow-wrap: normal; word-break: normal;"> <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">如果想获取更多源码或者视频教程,欢迎关注我的微信公众号 <code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248) none repeat scroll 0% 0%;">好好学java</code>,在公众号里,回复:<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248) none repeat scroll 0% 0%;">java基础、html5、javaEE基础、struts2、spring、redis、luncene、oracle</code>等,将可获得以上的优质视频教程及源码。 <br><img src="http://img.blog.csdn.net/20180122123856885?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2loYWkxMjM0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; display: block; margin: 0px auto; max-width: 100%;"></p> </blockquote></div>