问题
I am trying to configure an Apache load balancing solution with mod_jk. The clustering works but not load balancing.
I have Apache httpd 2.2 server running on my laptop. I have two VMWare Virtual Machine Guest Operating systems. All three are windows. The VMware machines hosts Apache Tomcat Server serving the web application. I have configured httpd.conf file with mod_jk and a workers properties file with the worker information. I am able to access my web application using the URL : http://localhost/Web-application
. If I stop one server then the application is served from the other. However not both at the same time. Some extracts are below:
httpd.conf file:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile "logs/mod_jk.log"
JkLogLevel info
JkMount /MovieBooking loadbalancer
JkMount /MovieBooking/* loadbalancer
workers.properties File
workers.tomcat_home=/worker1
workers.java_home=$JAVA_HOME
worker.list=loadbalancer,jkstatus,worker1,worker2
#Declare Tomcat server workers 1 through n
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2
worker.loadbalancer.sticky_session=1
worker.worker1.type=ajp13
worker.worker1.host=192.168.200.244
worker.worker1.port=8109
worker.worker1.lbfactor=1
worker.worker2.type=ajp13
worker.worker2.port=8109
worker.worker2.host=192.168.200.243
worker.worker2.lbfactor=1
worker.jkstatus.type=status
I have also set jvmroute in the server.xml files on these servers:
Server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
if any more extracts are required I can upload them
回答1:
The way you configured load balancing (looking ok to me) with sticky sessions means that once a session is created, subsequent requests will be directed to the same Tomcat instance. You can recognize this by looking at the session cookie values which should be appended with .worker1
or .worker2
. This postfix is used by the Apache HTTPD to decide which Tomcat instance to send an incoming request to.
If there is no session cookie, requests should be distributed among the available Tomcats in a round-robin fashion. So in order to test load-balacing, you will typically need several browser instances holding different session cookies. Or try setting sticky_session=false
to see all requests forwarded round-robin style.
回答2:
from apache:
- All your session attributes must implement java.io.Serializable
- Uncomment the Cluster element in server.xml
- If you have defined custom cluster valves, make sure you have the ReplicationValve - fined as well under the Cluster element in server.xml
- If your Tomcat instances are running on the same machine, make sure the tcpListenPort attribute is unique for each instance, in most cases Tomcat is smart enough to resolve this on it's own by autodetecting available ports in the range 4000-4100 Make sure your web.xml has the element
- If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine and that the jvmRoute attribute value matches your worker name in workers.properties
- Make sure that all nodes have the same time and sync with NTP service! Make sure that your loadbalancer is configured for sticky session mode.
i'll try first to make sure jvmRoute are ok
Session stickyness is not implemented using a tracking table for sessions. Instead each Tomcat instance gets an individual name and adds its name at the end of the session id. When the load balancer sees a session id, it finds the name of the Tomcat instance and sends the request via the correct member worker. For this to work you must set the name of the Tomcat instances as the value of the jvmRoute attribute in the Engine element of each Tomcat's server.xml. The name of the Tomcat needs to be equal to the name of the corresponding load balancer member. In the above example, Tomcat on host "myhost1" needs jvmRoute="worker1", Tomcat on host "myhost2" needs jvmRoute="worker2".
your tomcat 2 jvmRoute should be "worker2"
来源:https://stackoverflow.com/questions/12192636/apache-mod-jk-load-balancing-not-working-but-failover-works