为什么80%的码农都做不了架构师?>>>
反正每次来做一个不熟悉的东西,就是各种的search ,前一次去做过一个apache的东西,各种蛋疼,各种不能用。好多的东西也是比较旧了的咯。
这次结合前辈的各种东借西拿,总算是成功了。
如果有些不明白,建议先看下一篇,,因为你的打开方式有些不对。
环境: win7 sp1.
jdk1.7
httpd2.4x64
tomcat7.59
我们这里是使用AJP模式来进行集群
sp 0: 首先是安装apache
修改httpd.conf中的一些路径,设置各种路径为当前你放置apache-httpd的路径
ServerRoot "D:\apache24-tct7\Apache24"
//.....省略n多路径配置
sp 1. 配置一下模块打开
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.soLoadModule speling_module modules/mod_speling.soLoadModule ssl_module modules/mod_ssl.so
#可能还需要这个
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule proxy_html_module modules/mod_proxy_html.so
1.1 配置反向代理
ProxyRequests Off
<proxy balancer://cluster>BalancerMember ajp://127.0.0.1:9001 loadfactor=1 route=jvm1BalancerMember ajp://127.0.0.1:9002 loadfactor=1 route=jvm2BalancerMember ajp://127.0.0.1:9003 loadfactor=1 route=jvm3
</proxy>
sp 2. 配置tomcat
2.1改 SHUTDOWN 端口
#同一服务器中必须不一样,因为可能冲突
<Server port="9006" shutdown="SHUTDOWN">
2.2改http port redirectPort
#同一服务器中必须不一样,因为可能冲突
<Connector port="8081" protocol="HTTP/1.1" executor="tomcatThreadPool"connectionTimeout="20000"redirectPort="9441" />
2.3 改ajp port redirectPort
#同一服务器中必须不一样,因为可能冲突
<Connector port="9001" protocol="AJP/1.3" redirectPort="8441" />
2.4 改 Engine 加上 jvmRoute 需要跟httpd.conf中保持一致
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
2.5 加上Cluster 具体如下
#其中,Receiver port="5001" 同一服务器下,不能一样
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"><Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6" /><Channel className="org.apache.catalina.tribes.group.GroupChannel"><Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000" /><Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="5001" selectorTimeout="100" maxThreads="6" /><Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"><Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" /></Sender><Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" /><Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" /><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" /></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" /><Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false" /><ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" /></Cluster>
sp 3. 改项目web.xml
在其中添加:
<distributable />
节点
放一个完整一点的tomcat的server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may notdefine subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html-->
<Server port="9006" shutdown="SHUTDOWN"><Listener className="org.apache.catalina.startup.VersionLoggerListener" /><!-- Security listener. Documentation at /docs/config/listeners.html<Listener className="org.apache.catalina.security.SecurityListener" />--><!--APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --><Listener className="org.apache.catalina.core.JasperListener" /><!-- Prevent memory leaks due to use of particular java/javax APIs--><Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /><!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users--><Resource name="UserDatabase" auth="Container"type="org.apache.catalina.UserDatabase"description="User database that can be updated and saved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml" /></GlobalNamingResources><!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note: A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html--><Service name="Catalina"><!--The connectors can use a shared executor, you can define one or more named thread pools--><!--<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="150" minSpareThreads="4"/>--><!-- A "Connector" represents an endpoint by which requests are receivedand responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)Java AJP Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL HTTP/1.1 Connector on port 8080--><Connector port="8081" protocol="HTTP/1.1" executor="tomcatThreadPool"connectionTimeout="20000"redirectPort="9441" /><!-- A "Connector" using the shared thread pool--><!--<Connector executor="tomcatThreadPool"port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />--><!-- Define a SSL HTTP/1.1 Connector on port 8443This connector uses the BIO implementation that requires the JSSEstyle configuration. When using the APR/native implementation, theOpenSSL style configuration is required as described in the APR/nativedocumentation --><!--<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"maxThreads="150" SSLEnabled="true" scheme="https" secure="true"clientAuth="false" sslProtocol="TLS" />--><!-- Define an AJP 1.3 Connector on port 8009 --><Connector port="9001" protocol="AJP/1.3" redirectPort="8441" /><!-- An Engine represents the entry point (within Catalina) that processesevery request. The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html --><!-- You should set jvmRoute to support load-balancing via AJP ie :<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"><Engine name="Catalina" defaultHost="localhost">--><Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"><!--For clustering, please take a look at documentation at:/docs/cluster-howto.html (simple how to)/docs/config/cluster.html (reference documentation) --><!--<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--><Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"><Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6" /><Channel className="org.apache.catalina.tribes.group.GroupChannel"><Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000" /><Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="5001" selectorTimeout="100" maxThreads="6" /><Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"><Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" /></Sender><Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" /><Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" /><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" /></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" /><Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false" /><ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" /></Cluster><!-- Use the LockOutRealm to prevent attempts to guess user passwordsvia a brute-force attack --><Realm className="org.apache.catalina.realm.LockOutRealm"><!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase". Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm. --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Host name="localhost" appBase="webapps"unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log." suffix=".txt"pattern="%h %l %u %t "%r" %s %b" /></Host></Engine></Service>
</Server>
具体根据需求的做法,请参考tomcat的文档
http://http://tool.oschina.net/apidocs/apidoc?api=tomcat-7.0-doc
apache2.4 x64win 下载地址:
http://www.apachelounge.com/download/
其他的,应该不用说了吧
测试就是写个jsp,打印下各tomcat下的项目中的session信息,以及是哪个tomcat下面的
jvm3 ID 3C5555DBBBE06EEC67B6F9508CAB0520.jvm3
Session 列表a = 123