(1) 配置nginx的upstreamhtml
upstream tomcats { #weigth参数表示权值,权值越高被分配到的概率越大 server 127.0.0.1:8080 weight=5; server 127.0.0.1:8081 weight=1; }
(2) 配置nginx的80端口映射java
server { listen 80; server_name tomcats; location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://tomcats; } }
(1) 修改tomcat 的相关8080端口配置/conf/server.xmlnginx
(2) 修改Tomcat 8081端口修改方法同上的配置文件web
(1) 8080端口的tomcat的index.jspapache
(2) 8081端口的tomcat的index.jsp浏览器
(1) 启动Nginxtomcat
(2)启动tomcat8080,启动tomcat8081session
(3)运行效果截图:app
8080的权重比较大,因此默认是tomcat8080webapp
(4)关闭Tomat8080,再次刷新上面的localhost页面,运行效果图为:
(5) 怎么样,运行效果不错吧。嘿嘿...........
======================================== 华丽丽的分割线 =================================================
另外,可是若是Nginx负载2个tomcat以上,那么在发布工程的时候,岂不是要重复屡次地拷贝war工程包部署到webapps目录下,最后在重复的启动t多个omcat,那么咱们怎么解决一次部署就能影响到多个tomcat实例?
后来,在网上搜索了下,能够经过配置修改tomcat 的server.xml的配置文件,将这些个tomcat的webapps目录统一指定到同一个webapps目录下。
(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)若是还有不少个,那么重复以上的修改配置文件的方法
(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) 整个配置过程完成
<%@ 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>
<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>
<?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>
打开两个浏览器测试
经过Nginx访问
================================== 华丽丽的结束分割线=================================