單機配置Tomcat Cluster

來源:互聯網
上載者:User

 

1. 下載並安裝Tomcat,這裡使用Tomcat的6.0.24版本;

2. 修改conf/server.xml檔案,將其中<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>前後的注釋開啟儲存;

3. 建立一個web app,這裡為了簡單使用tomcat內建的examples web app(webapps/examples),修改其中的WEB-INF/web.xml檔案,在其中<display-name>Servlet and JSP Examples</display-name>節點後添加<distributable/>表明此應用與叢集伺服器複製Session;

4. 在examples目錄下建立一個test.jsp檔案,用來測試Cluster中兩個Tomcat使用的Session是同一個Session,並且當其中一個Tomcat修改Session中的值時,第二個Tomcat會同樣得到更新;代碼如下:

<html> <head> <title></title> </head> <body> <% String mydata = request.getParameter("mydata"); if (mydata != null && mydata.length() != 0) { session.setAttribute("mydata", mydata); } out.println("request.getLocalAddr(): " + request.getLocalAddr()); out.println("<br/>"); out.println("request.getLocalPort(): " + request.getLocalPort()); out.println("<br/>"); out.println("Session ID: " + session.getId()); out.println("<br/>"); out.println("mydata: " + session.getAttribute("mydata")); %> <form> <input type=text size=20 name="mydata"> <br> <input type=submit> </form> </body> </html> 

 

5. 複製此Tomcat(假定命名為Tomcat1)完整目錄到別處(假定命名為Tomcat2),然後修改Tomcat2/conf/server.xml檔案中的連接埠(8080 -> 9080,8009 -> 9009,8443 -> 9443) 

6. 啟動Tomcat1和Tomcat2,然後分別訪問http://localhost:8080/examples/test.jsp和http://localhost:9080/examples/test.jsp,會發現其中使用的Session ID是同一個Session ID,相應的修改其中一個的值,另外一個Tomcat Session中的值也會改變。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.