2017.7.12 nginx+tomcat的简单集群入门,实操过程

写在前面的话:好多弄过的东西都是自己写到小本本上了,最近开窍了,觉得还是分享出去的好,万一有谁用得到呢,按我个人的经验,每次探索开发新的东西的时候,总是会去网上、csdn找知识点,找操作过程,但往往有一些不够详细,或者说不到点上是自己需要的,可能是因为每个人遇到的问题都不一样吧,所以开了这个博客讲述自己执行操作过程中遇到的问题,简单来说也是升级打怪的过程,可能也不会包含所有博友的需要,但我尽我所能,尽量讲请问题的解决办法。

写集群的太多了,我就尽量详细点,供各位入门。

1.软件准备

下载NginxTomcat

Nginxhttp://nginx.org/en/download.html 这里需要下载稳定版:Stable version
Tomcat
:下载就不说了,这里使用apache-tomcat-6.0.14版本

解压到一个目录

https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101933217-1713032165.jpg

2.修改Tomcat的端口

Tomcat1:修改Server.xml

D:\nginx_cluster\apache-tomcat-6.0.14_1\conf\server.xml
共修改3处内容:将以下端口都加1

  1. <!--1-->
  2. <Server port="18005" shutdown="SHUTDOWN">
  3. <!--2-->
  4. <Connector port="18080" protocol="HTTP/1.1"
  5.                connectionTimeout="20000"
  6.                redirectPort="8443" />
  7. <!--3-->
  8. <Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

Tomcat2:修改Server.xml

D:\nginx_cluster\apache-tomcat-6.0.14_2\conf\server.xml
共修改3处内容:将以下端口都加2

  1. <!--1-->
  2. <Server port="28005" shutdown="SHUTDOWN">
  3. <!--2-->
  4. <Connector port="28080" protocol="HTTP/1.1"
  5.                connectionTimeout="20000"
  6.                redirectPort="8443" />
  7. <!--3-->
  8. <Connector port="28009" protocol="AJP/1.3" redirectPort="8443" />

3.测试Tomcat是否正常运行

分别访问两个Tomcat

http://localhost:18080/
http://localhost:28080/
都出现猫的页面说明正常,为了区分不同的Tomcat,这里修改${Tmocat_home}\webapps\ROOT\ index.html文件内容,加入内容以便区分

  1. <h1>This Tomcat1</h1>

之后再次访问两个Tomcat
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101933702-1906019274.jpg
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101934045-1516029628.jpg

至此,两个Tomcat运行正常。

windows下多个tomcat间session共享的问题(以下代码在http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html)中可以看到

在两个tomcat的conf文件夹server.xml内分别添加如下内容:

<Engine name="Catalina" defaultHost="localhost">

    <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="228.0.0.4" 

                       port="45564" 

                       frequency="500" 

                       dropTime="3000"/> 

           <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 

                     address="auto" 

                     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=""/> 

         <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>

与之配合使用的便是在项目的web.xml文件中添加<distributable/>节点:如图所示:

 

4.配置Nginx

修改Nginx的主配置文件:
D:\nginx_cluster\nginx-1.10.2\conf\ nginx.conf

  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8.     worker_connections  1024;
  9. }
  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;
  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16.     #access_log  logs/access.log  main;
  17.     sendfile        on;
  18.     #tcp_nopush     on;
  19.     #keepalive_timeout  0;
  20.     keepalive_timeout  65;
  21.     #gzip  on;
  22.     #监听localhost80端口
  23.     server {
  24.            listen       80;
  25.            server_name  localhost; 
  26.            location / {
  27.                proxy_connect_timeout   3;
  28.                proxy_send_timeout      30;
  29.                proxy_read_timeout      30;
  30.                proxy_pass http://localhost;
  31.            }
  32.    }
  33.     # another virtual host using mix of IP-, name-, and port-based configuration
  34.     #
  35.     #server {
  36.     #    listen       8000;
  37.     #    listen       somename:8080;
  38.     #    server_name  somename  alias  another.alias;
  39.     #    location / {
  40.     #        root   html;
  41.     #        index  index.html index.htm;
  42.     #    }
  43.     #}
  44.     #集群配置:服务器列表
  45.     upstream localhost {
  46.       server localhost:18080 weight=2;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
  47.       server localhost:28080 weight=1;
  48.     }
  49.     # HTTPS server
  50.     #
  51.     #server {
  52.     #    listen       443 ssl;
  53.     #    server_name  localhost;
  54.     #    ssl_certificate      cert.pem;
  55.     #    ssl_certificate_key  cert.key;
  56.     #    ssl_session_cache    shared:SSL:1m;
  57.     #    ssl_session_timeout  5m;
  58.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  59.     #    ssl_prefer_server_ciphers  on;
  60.     #    location / {
  61.     #        root   html;
  62.     #        index  index.html index.htm;
  63.     #    }
  64.     #}
  65. }

主要配置

https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101934577-1654961034.jpg
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101934952-739806550.jpg
至此,Nginx的简单配置就完成了。下面开始测试

5.测试集群访问

启动Nginx

进入到Nginx目录
启动命令为:start nginx
停止命令为:nginx –s stop
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101935249-977776280.jpg

访问测试

   访问前需要开启几个tomcat的服务,否则会报504的错误。

访问:http://localhost/
Nginx内部配置了监听80端口,默认进行服务器的分发。
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101935670-153286985.jpg
https://images2015.cnblogs.com/blog/527668/201611/527668-20161111101936045-2070587458.jpg

随便刷新测试了10次,共访问了Tomcat18次,Tomcat22次。可以看到权重越大,访问到的概率越大。

6.访问项目

将项目的包分别放到tomcat的webapps下,http://localhost:18080/jeecg-bpm

http://localhost:28080/jeecg-bpm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值