一.Nginx实现多Tomat负载配置
1.下载Nginx,修改Nginx的配置文件nginx.conf
(1) 配置nginx的upstream
upstream tomcats {
#weigth参数表示权值,权值越高被分配到的几率越大
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:8081 weight=1;
}
(2) 配置nginx的80端口映射
server {
listen 80;
server_name tomcats;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://tomcats;
}
}
2.下载tomcat安装包,分别部署的结构截图:
(1) 修改tomcat 的相关8080端口配置/conf/server.xml
(2) 修改Tomcat 8081端口修改方法同上的配置文件
3.修改/webapps/ROOT/index.jsp的文件下的内容,作标识。
(1) 8080端口的tomcat的index.jsp
(2) 8081端口的tomcat的index.jsp
4.测试配置效果
(1) 启动Nginx
(2)启动tomcat8080,启动tomcat8081
(3)运行效果截图:
8080的权重比较大,所以默认是tomcat8080
(4)关闭Tomat8080,再次刷新上面的localhost页面,运行效果图为:
(5) 怎么样,运行效果不错吧。嘿嘿...........
======================================== 华丽丽的分割线 =================================================
另外,但是如果Nginx负载2个tomcat以上,那么在发布工程的时候,岂不是要重复多次地拷贝war工程包部署到webapps目录下,最后在重复的启动t多个omcat,那么我们怎么解决一次部署就能影响到多个tomcat实例?
后来,在网上搜索了下,可以通过配置修改tomcat 的server.xml的配置文件,将这些个tomcat的webapps目录统一指定到同一个webapps目录下。
二. 多个tomcat实例共享同一个webapps目录,达到一次部署到多个tomcat实例的效果
1.修改tomcat 的/conf/server.xml的配置文件
(1)修改tomcat8080的配置文件
<?xml version="1.0" encoding="UTF-8"?>
--><Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<!-- 重点关注在这里 appBase 置顶webapps的目录地址 -->
<Host appBase="D:\APP\Java\SLBTomcats\webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
</Host>
</Engine>
</Service>
</Server>
(2)修改tomcat8081的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8444"/>
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<!-- 重点关注在这里 appBase 置顶webapps的目录地址 -->
<Host appBase="D:\APP\Java\SLBTomcats\webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
</Host>
</Engine>
</Service>
</Server>
(3)如果还有很多个,那么重复以上的修改配置文件的方法
2.看看测试效果
(1)创建一个测试的Dynamic的web工程,只有一个index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
测试结果:<br>
1.Nginx实现负载多个tomcat.<br>
2.实现多个tomcat共享同一个webapps目录,实现一次部署运行到多个tomcat实例的效果。
</h1>
</body>
</html>
(2)将测试工程打包成war包,复制到 这个 D:\APP\Java\SLBTomcats\webapps 目录下。
(3)启动Nginx和两个tomcat,效果图为:
(4) 关闭tomcat8080的tomcat,然后再次刷新上面的页面,同样会出现上面的页面。
(5) 整个配置过程完成
三.利用多Tomcat集群功能,实现简单的Session共享
1.在JavaWeb工程的index.jsp页面中,添加Session获取代码,方便后面测试。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
测试结果:<br>
1.Nginx实现负载多个tomcat.<br>
2.实现多个tomcat共享同一个webapps目录,实现一次部署运行到多个tomcat实例的效果。
</h1>
<div>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
</div>
</body>
</html>
2. 修改Tomcat8080 和Tomcat8081两个的server.xml中的配置如下:
<Engine defaultHost="localhost" name="Catalina">
<!-- 配置简单的Tomcat集群-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<!-- 重点关注在这里 appBase 置顶webapps的目录地址 -->
<Host appBase="D:\APP\Java\SLBTomcats\webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
</Host>
</Engine>
3.在JavaWeb工程的web.xml中修改配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>myProject</display-name>
<!-- 配置Session复制共享-->
<distributable/>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.重启两个Tomcat
打开两个浏览器测试
通过Nginx访问
================================== 华丽丽的结束分割线=================================