Configurando um Cluster de Tomcat com Balanceamento de Carga – Parte 2
Autor: Adriel Lucas | Publicado: 2011-10-15T06:54:00-07:00





Configuração do
Cluster de Tomcat



Levarei em consideração que você já tenha duas instancias do
tomcat, o apache e o java instalados no seu servidor. Segue abaixo a descrição
do ambiente que será utilizado nesse pequeno projeto, vale lembrar que é apenas
um projeto de teste:

-Duas instancias de máquinas virtuais no virtualbox;

- Cada VM com 512MB de RAM e 8GB de HD;

- Cada VM com o sistema Operacional CentOS 5.7 (Final);

Obs.: essa configuração poderá ser realizada em qualquer
servidor Linux.



Requisitos:



Para realizar a replicação de sessões entre os nós do
cluster alguns pontos devem ser verificados:

- todos os atributos de sessão devem implementar java.io.Serializable;

- Ter no arquivo web.xml da aplicação o atributo
&lt;distributable/&gt; ou definir um propriedade na configuração do contexto da
aplicação no arquivo server.xml;

- configurar o balanceador de carga com a opção sticky
session ativa, para manter as requisições de um mesmo usuário do sistema indo
para um mesmo nó do cluster, exceto quando falhar este nó.



Iniciando a
Configuração:



A Configuração dos tomcat’s para o cluster é realizada a
partir da edição do arquivo server.xml, localizado na pasta conf (&lt;diretório
do tomcat&gt;/conf/server.xml) de cada tomcat. O que deve ser feito é
descomentar toda a tag cluster e acrescentar a configuração adequando os
parâmetros ao ambiente de hospedagem. Logo abaixo a configuração do cluster:



&lt;Cluster
className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; channelSendOptions="8"&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Manager
className="org.apache.catalina.ha.session.DeltaManager"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
expireSessionsOnShutdown="false"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
notifyListenersOnReplication="true"/&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Channel
className="org.apache.catalina.tribes.group.GroupChannel"&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Membership
className="org.apache.catalina.tribes.membership.McastService"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
address="228.0.0.4"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port="45564"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
frequency="500"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dropTime="3000"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Receiver
className="org.apache.catalina.tribes.transport.nio.NioReceiver"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; address="auto"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;port="4000"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; autoBind="100"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
selectorTimeout="5000"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maxThreads="6"/&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Sender
className="org.apache.catalina.tribes.transport.ReplicationTransmitter"&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/Sender&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;Interceptor
className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Interceptor
className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Channel&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Valve
className="org.apache.catalina.ha.tcp.ReplicationValve"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filter=""/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Valve
className="org.apache.catalina.ha.session.JvmRouteBinderValve"/&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Deployer
className="org.apache.catalina.ha.deploy.FarmWarDeployer"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
tempDir="/tmp/war-temp/"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
deployDir="/tmp/war-deploy/"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
watchDir="/tmp/war-listen/"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
watchEnabled="false"/&gt;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ClusterListener
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ClusterListener
className="org.apache.catalina.ha.session.ClusterSessionListener"/&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Cluster&gt;&nbsp;&nbsp; 



Em cada tomcat temos a mesma tag cluster com uma pequena
diferença na configuração:

 

  
Tag Menbership

  
Tag Receiver

 

  
Tomcat A

  
Tomcat B

  
Tomcat A

  
Tomcat B

 

  
address=”224.0.0.4”

  
address=”224.0.0.4”

  
address=”auto”

  
address=”auto”

 

  
port=”45564”

  
port=”45564”

  
port=”4000”

  
port=”4000”

 




Agora temos que
identificar cada nó no mesmo arquivo “server.xml”. No tomcat A e no
tomcat B será preciso fazer algumas alterações para os nós poderem se
comunicar:



 

  
TOMCAT
  A

 

  
Onde tem:

  
Coloque:

 

  
&lt;Engine
  name="Catalina" defaultHost="localhost"
  jvmRoute="jvm1"&gt;

  
&lt;Engine
  name="Catalina" defaultHost="localhost" jvmRoute=”node01”&gt;

 



 

  
TOMCAT
  B

 

  
Onde tem:

  
Coloque:

 

  
&lt;Server port="8005" shutdown="SHUTDOWN"&gt;

  
&lt;Server port="9005"
  shutdown="SHUTDOWN"&gt;

 

  
&lt;Connector port="8080" ...

  
&lt;Connector
  port="9080"…

 

  
&lt;Connector
  port="8006" protocol="AJP/1.3"
  redirectPort="8443" /&gt;

  
&lt;Connector port="9006"
  protocol="AJP/1.3" redirectPort="8443" /&gt;

 

  
&lt;Engine
  name="Catalina" defaultHost="localhost"
  jvmRoute="jvm1"&gt;

  
&lt;Engine
  name="Catalina" defaultHost="localhost" jvmRoute=”node02”&gt;

 


OBS.: As alterações
estão destacadas em negrito, porém essas alterações são um exemplo de
configuração não necessariamente terá que utilizar estes mesmo valores pode ser
feito com outros valores. 



Depois de fazer essas
configurações, basta só iniciar os tomcat’s que os mesmos começarão a replicar
suas sessões, ou seja, começarão a trabalhar em cluster.

Para iniciar os
tomcats basta entrar no diretório Bin/ dentro de cada tomcat e executar o
seguinte comando:

#sh startup.sh

No próximos
posts irei mostrar como configurar o balanceador de carga e fazer os testes com
uma simples página .jsp.&nbsp;