View comments | RSS feed

Implementing a load-balancing filter

To implement a custom load balancing filter:

  1. Create a cluster. For instructions, see JRun Administrator's Guide.
  2. Write a filter class that stores a single data point in the JRunProxyService's SERVER_LOAD_ATTRIBUTE field, as the following code shows:
    ...
    long bef = System.currentTimeMillis();
    chain.doFilter(request,response);
    long aft = System.currentTimeMillis();
    long in_request = aft - bef;
    request.setAttribute(JRunProxyService.SERVER_LOAD_ATTRIBUTE, 
    new Long(in_request));
    ...
    

    For a complete example of a filter that uses this sample code, see "Reviewing a simple custom load-balancing filter".

  3. Compile the filter. You must include jrun_root/lib/jrun.jar in your classpath; for example:
    %> javac -classpath /opt/jrun4/lib/jrun.jar SimpleLBFilter.java 
    -d /opt/jrun4/servers/default/default-ear/default-war/WEB-INF/classes
    
  4. Add the filter to the web application that you deploy on the cluster. You can put the filter in the JRun server's jrun_root/servers/server_name/WEB-INF/classes directory or in the web application's directory structure.

    Note:   Install the filter on every JRun server in the cluster.

  5. Define the filter for each JRun server by adding a filter definition and filter mapping to the server's web.xml file, as the following example shows:
    ...
    <filter>
    ��<filter-name>MILF</filter-name>
    ��<filter-class>MetricsLoadMonitorFilter</filter-class>
    </filter>
    ...
    <filter-mapping>
    ��<filter-name>MILF</filter-name>
    ��<url-pattern>/*</url-pattern>
    </filter-mapping>
    ...
    
  6. Update the jrun.xml file for all JRun servers in the cluster. Change the LoadBalancingAlgorithm attribute of the JRunProxyService attribute to MIN or MAX.
    <service class="jrun.servlet.jrpp.JRunProxyService" name="ProxyService">
    ��<attribute name="bindToJNDI">true</attribute>
    ��<attribute name="port">51010</attribute>
    ��<attribute name="deactivated">false</attribute>
    ��<attribute name="LoadBalancingAlgorithm">MIN</attribute>
    ��<attribute name="ServerWeight">3</attribute>
    ��<attribute name="StickySessions">true</attribute>
    </service>
    
  7. Restart all JRun servers in the cluster.

    You are required to restart the JRun server when you add a servlet filter.

Comments


No screen name said on Mar 10, 2005 at 5:07 AM :
I have used the various options of load balancing requests i.e. roundrobin, weighted roundrobin and random weighted, and custom load balancing using the servlet filter.
I have a cluesterd environment of 2 servers connected via Apache and have enabled session replication.
The Jrun.xml is as follows:
****************************************
<service class="jrun.servlet.jrpp.JRunProxyService" name="ProxyService">
<attribute name="activeHandlerThreads">25</attribute>
<attribute name="backlog">500</attribute>
<attribute name="deactivated">false</attribute>
<attribute name="interface">*</attribute>
<attribute name="maxHandlerThreads">1000</attribute>
<attribute name="minHandlerThreads">1</attribute>
<attribute name="port">51002</attribute>
<attribute name="threadWaitTimeout">300</attribute>
<attribute name="timeout">300</attribute>
<attribute name="bindToJNDI">true</attribute>
<attribute name="StickySessions">true</attribute>
<attribute name="LoadBalancingAlgorithm">ROUNDROBIN</attribute>
<!--
<attribute name="ServerWeight">1</attribute>
<attribute name="keyStore">{jrun.rootdir}/lib/keystore</attribute>
<attribute name="keyStorePassword">changeit</attribute>
<attribute name="trustStore">{jrun.rootdir}/lib/trustStore</attribute>
<attribute name="socketFactoryName">jrun.servlet.jrpp.JRunProxySSLServerSocketFactory</attribute>
-->
<attribute name="mapCheck">0</attribute></service>
*****************************************
In all cases, the requests goes to only one server, the primary server in the cluster. ONLY when the primary server is stopped, the request goes to the other server.
Please let me know how to achieve ROUNDROBIN load balancing. We use Jrun4 with updater 4.
jrunrandy said on Mar 14, 2005 at 1:49 PM :
I don't know if this will fix your issue, but in my testing, I needed to set sticky sessions to false to get round robin load balancing to work.
No screen name said on Mar 15, 2005 at 5:03 AM :
Thank you - I believe it solved my problem too. I will confirm after I do a bit of testing on my side.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/jrun/4/JRun_Service_Pack_1_Guide/customloadbalancing3.htm