写在前面的话:好多弄过的东西都是自己写到小本本上了,最近开窍了,觉得还是分享出去的好,万一有谁用得到呢,按我个人的经验,每次探索开发新的东西的时候,总是会去网上、csdn找知识点,找操作过程,但往往有一些不够详细,或者说不到点上是自己需要的,可能是因为每个人遇到的问题都不一样吧,所以开了这个博客讲述自己执行操作过程中遇到的问题,简单来说也是升级打怪的过程,可能也不会包含所有博友的需要,但我尽我所能,尽量讲请问题的解决办法。
写集群的太多了,我就尽量详细点,供各位入门。
1.软件准备
下载Nginx和Tomcat
Nginx:http://nginx.org/en/download.html 这里需要下载稳定版:Stable version
Tomcat:下载就不说了,这里使用apache-tomcat-6.0.14版本
解压到一个目录
2.修改Tomcat的端口
Tomcat1:修改Server.xml
D:\nginx_cluster\apache-tomcat-6.0.14_1\conf\server.xml
共修改3处内容:将以下端口都加1
- <!--第1处-->
- <Server port="18005" shutdown="SHUTDOWN">
- <!--第2处-->
- <Connector port="18080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <!--第3处-->
- <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处-->
- <Server port="28005" shutdown="SHUTDOWN">
- <!--第2处-->
- <Connector port="28080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <!--第3处-->
- <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文件内容,加入内容以便区分
- <h1>This Tomcat1</h1>
之后再次访问两个Tomcat
至此,两个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
- #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;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- #监听localhost的80端口
- server {
- listen 80;
- server_name localhost;
- location / {
- proxy_connect_timeout 3;
- proxy_send_timeout 30;
- proxy_read_timeout 30;
- proxy_pass http://localhost;
- }
- }
- # 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;
- # }
- #}
- #集群配置:服务器列表
- upstream localhost {
- server localhost:18080 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
- server localhost:28080 weight=1;
- }
- # 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;
- # }
- #}
- }
主要配置
至此,Nginx的简单配置就完成了。下面开始测试
5.测试集群访问
启动Nginx
进入到Nginx目录
启动命令为:start nginx
停止命令为:nginx –s stop
访问测试
访问前需要开启几个tomcat的服务,否则会报504的错误。
访问:http://localhost/
Nginx内部配置了监听80端口,默认进行服务器的分发。
随便刷新测试了10次,共访问了Tomcat1共8次,Tomcat2共2次。可以看到权重越大,访问到的概率越大。
6.访问项目
将项目的包分别放到tomcat的webapps下,http://localhost:18080/jeecg-bpm