服务器环境:
win7,Nginx 1.13.2, tomcat8 , java 1.8
1
首先修改tomcat8下的配置文件,打开tomcat的conf目录下的server.xml,以第一个tomcat的配置文件为例,
<Server port="8005" shutdown="SHUTDOWN">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
第二个tomcat配置文件
<Server port="8015" shutdown="SHUTDOWN">
<Connector connectionTimeout="20000" port="8090" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/>
保证tomcat端口号不重复,以上配置,只有端口号(port)是必要配置,其他按平常配置即可,多个tomcat以此类推。
在项目页面加入如下代码,以便区分访问的tomcat到底是哪一个并验证sessionID是否相同。
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
sessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<%
//为了区分,第二个可以是222
out.println("This is Tomcat Server 1111");
%>
2
第二步配置Nginx负载均衡配置,可以使用ip_hash进行分流,也可以默认进行轮询,这里的项目由于一些原因无法使用默认轮询,只能使用ip_hash进行访问。
打开Nginx目录下的conf/nginx.conf,
upstream local {
ip_hash;
server 127.0.0.1:8080 max_fails=1 fail_timeout=1s weight=1;
server 127.0.0.1:8090 max_fails=1 fail_timeout=1s weight=1;
}
server {
listen 80;
server_name 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://local;
proxy_connect_timeout 3;
proxy_read_timeout 10;
proxy_send_timeout 10;
}
proxy_pass 后面的local,与之相对应的是上方upstream后面的名字,两个应该对应
max_fails:最大失败次数
max_timeouts:最大等待时间
weight:权重,越大权重访问的几率越大
打开Nginx的流程,找到你的nginx安装目录,在cmd里输 cd 复制nginx的目录,例如cd C:\Users\dong\Downloads\nginx-1.13.2\nginx-1.13.2
运行后 输入start nginx指令,会有弹窗闪一下,那就证明开启了,也可以在任务管理器中查看有无nginx相关进程,会有两个nginx.exe,说明启动正常。
每次修改完nginx.conf文件后,在控制台输入nginx -s reload,重新加载配置,不需要重新启动nginx服务器。也可以先输入nginx -t,验证配置文件有无问题,在进行加载。
3
下面配置一下tomcat8的集群,tomcat是自带复制集群的功能的,我们打开tomcat目录下conf/server.xml,找到如下代码位置
<Engine defaultHost="localhost" name="Catalina">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
首先将被注释掉的<Cluster>标签取消注释,如果是在本机进行测试,tomcat8只需要我们取消注释后我们就能够使用,暂时不需要进行其他配置。
在<Engine>标签里的name后面加上jvmRoute="apache-tomcat-8.0.31",引号里面为tomcat服务器的文件夹名字,保证和文件夹名相同,本机测试的情况下,文件夹姓名也不能重复,否则会无法使用,多台机器这里暂时还没有测试。配置后如下:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="apache-tomcat-8.0.31">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
最后我们进行web.xml的配置,在<webapp></webapp>中添加<distributable />标签。网上很多说这个标签加到tomcat的web.xml文件下,但我这里测试不成功,我们需要把这个标签加入到我们自己的项目的web.xml配置文件中。
在浏览器中输入我们的访问路径,例如我的127.0.0.1/test
看图片可以看出,我们进入了第二个tomcat的界面,这时候我们关闭第二个tomcat,刷新界面
我们可以看到我们进入了第一个tomcat的测试界面,查看sessionID,没有发生变化,session共享成功
到此tomcat8的集群配置就完成了。