tomcat集群 (自带Cluster集群)
生活随笔
收集整理的這篇文章主要介紹了
tomcat集群 (自带Cluster集群)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
不用借助其他任何工具,tomcat自身就可以實現session共享,實現集群。以下為大概步驟
?
1,如果是在同一臺機器上,請保持多個tomcat端口(一個tomcat對應三個端口)不相同;如果是不同機器則不用考慮端口
?
2,去掉server.xml中的Cluster的注釋(<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>)
?
3,在發布的web項目的web.xml中<web-app>標簽里面添加上<distributable />。
?
現在啟動tomcat,訪問http://localhost:8080/TestCluster/test.jsp可以看到一個sessionID,
然后在訪問http://localhost:8081/TestCluster/test.jsp可以看到和上一個tomcat的sessionID是相同的,
事實說明tomcat已經可以實現sesion共享了。
這種方式就是有局限:tomcat必須在同一局域網下,web項目名稱必須相同
附上網上的經典測試項目:
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4"><display-name>TomcatDemo</display-name><distributable /> </web-app>?
test.jsp
<%@ page contentType="text/html; charset=GBK" %> <%@ page import="java.util.*" %> <html><head><title>Cluster App Test</title></head> <body> Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%> <%out.println("<br> ID " + session.getId()+"<br>");// 如果有新的 Session 屬性設置String dataName = request.getParameter("dataName");if (dataName != null && dataName.length() > 0) {String dataValue = request.getParameter("dataValue");session.setAttribute(dataName, dataValue);}out.println("<b>Session 列表</b><br>");System.out.println("============================");Enumeration e = session.getAttributeNames();while (e.hasMoreElements()) {String name = (String)e.nextElement();String value = session.getAttribute(name).toString();out.println( name + " = " + value+"<br>");System.out.println( name + " = " + value);} %><form action="test2.jsp" method="POST">名稱:<input type=text size=20 name="dataName"><br>值:<input type=text size=20 name="dataValue"><br><input type=submit></form> </body> </html>?
轉載于:https://www.cnblogs.com/qlong8807/p/4530065.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的tomcat集群 (自带Cluster集群)的全部內容,希望文章能夠幫你解決所遇到的問題。