(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訪問
================================== 華麗麗的結束分割線=================================