vendredi 27 janvier 2012

Load balancing JK - apache2 - JBoss


Sommaire

  • 1 Produits utilisés
  • 2 Schéma de déploiement du Load Balancer
  • 3 Installation du module apache2 JK
  • 4 Configuration du module JK
  • 5 Configuration des serveurs JBoss & activation du protocole AJP/1.3

 Produits utilisés

- JBoss 4.2.3 GA
- Apache2.2.x
- module apache2 JK 1.2.31


Schéma de déploiement du Load Balancer

Installation du module apache2 JK

1- Télécharger le module apache2 suivant : mod_jk-1.2.31-httpd-2.2.x.so ou
wget http://mirror.ibcp.fr/pub/apache//tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/i386/mod_jk-1.2.31-httpd-2.2.x.so
2- Copier le fichier .so dans le répertoire module du server web apache
cp mod_jk-1.2.31-httpd-2.2.x.so /usr/lib/apache2/modules/mod_jk.so

Configuration du module JK

1- Dans le fichier httpd.conf du server web apache2, ajouter les lignes suivantes : /etc/apache2/httpd.conf
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location (put workers.properties next to httpd.conf)
JkWorkersFile Workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile logs/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
#JkMount /* loadbalancer
#JkMount /index.php loadbalancer
#JkMount /jmx-console/* loadbalancer
#JkMount /jkstatus loadbalancer
JkMount /gsi_engine/* loadbalancer

        StartServers 5
        MaxClients 150
        MinSpareThreads 25
        MaxSpareThreads 75
        ThreadsPerChild 25
        MaxRequestsPerChild 0

2- Créer le fichier Workers.properties dans /etc/apache2/
# Define 1 real worker using ajp13

worker.list=loadbalancer,status
# Set properties for worker1 (ajp13) JBoss 1
worker.worker1.type=ajp13
worker.worker1.host=10.0.3.61
worker.worker1.port=8109
worker.worker1.lbfactor=1
worker.worker1.connection_pool_size=10
worker.worker1.redirect=worker2
# Set properties for worker2 (ajp13) JBoss2
worker.worker2.type=ajp13
worker.worker2.host=10.0.3.58
worker.worker2.port=8109
worker.worker2.lbfactor=1
worker.worker2.connection_pool_size=10
#fonctionnement de l'equilibrage de charge
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2
worker.loadbalancer.sticky_session=True
worker.status.type=status
# Disable worker3 for all requests except failover
worker.worker2.activation=disabled
3- Créer le fichier uriworkermap.properties dans /etc/apache2/
/status=status
4- Créer le répertoire de log pour le module JK
mkdir /etc/apache2/logs
5- Redémarrer le server apache2
/etc/init.d/apache2 restart

Configuration des serveurs JBoss & activation du protocole AJP/1.3

Pour chaque Serveur JBoss, il faut faire 1- Dans le fichier de config $JBOSS_HOME/server/$SERVER_INSTANCE/deploy/jboss-web.deployer/META-INF/jboss-service.xml, modifier la balise suivante :
false
true
2- Dans le fichier de config $JBOSS_HOME/server/$SERVER_INSTANCE/deploy/jboss-web.deployer/server.xml, décommenter le bloc suivant pour activer l'AJP/1.3 :
    

3- Redémarrer le server JBoss
/etc/init.d/jboss restart

jeudi 12 janvier 2012

Apache Thrift Example


- Used third party tools
  • Eclipse
  • thrift-0.8.0
  • JDK1.7
1- Download thrift-0.8.0.exe from the following url (http://www.apache.org/dyn/closer.cgi?path=/thrift/0.8.0/thrift-0.8.0.exe)


2-  Create the thrift description file:


time.thrift



# time.thrift
namespace java tserver.gen
typedef i64 Timestamp
service TimeServer 
{
   Timestamp time()
}


3 - Generate the java classes for server and client


Command:
> thrift-0.8.0.exe --gen java time.thrift
-> We will find all java generated classes in gen-java directory


4- Implements Iface server class:


package com.sample.thrift.server;


import org.apache.thrift.TException;
import tserver.gen.*;


class TimeServerImpl implements TimeServer.Iface 
{
   @Override
   public long time() throws TException 
   {
      long time = System.currentTimeMillis();
      System.out.println("time() called: " + time);
      return time;
   }
}


5- Create server class:


Server.java


package com.sample.thrift.server;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import tserver.gen.TimeServer;


public class Server
{
private void startSampleServer()
{
try 
{
TServerSocket serverTransport = new TServerSocket(7911);         
TimeServer.Processor processor = new TimeServer.Processor(new TimeServerImpl());
Factory protFactory = new TBinaryProtocol.Factory(true, true);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor).protocolFactory(protFactory));
System.out.println("Starting server on port 7911 ...");
server.serve();

catch (TTransportException e) 
{
e.printStackTrace();
}
}


private void startSslServer()
{
try 
{
TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters();
params.setKeyStore("C:/Users/jamel/eclipse.helios.workspace/ThriftClientServer/certificat_ssl/saskeystore.jks", "sas001");
TServerSocket serverTransport = TSSLTransportFactory.getServerSocket(7912, 10000, InetAddress.getByName("localhost"), params);
TimeServer.Processor processor = new TimeServer.Processor(new TimeServerImpl());
Factory protFactory = new TBinaryProtocol.Factory(true, true);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor).protocolFactory(protFactory));
System.out.println("Starting server on port 7912 ...");
server.serve();

catch (TTransportException e) 
{
e.printStackTrace();
}
catch (UnknownHostException e) 
{


}
}


public static void main(String args[])
{
Server srv = new Server();
//srv.startSampleServer();
srv.startSslServer();
}
}


6- Create Client Class:


TimeClient.java

package com.sample.thrift.client;


import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import tserver.gen.TimeServer.Client;


public class TimeClient 
{
private void startSampleClient()
{
TTransport transport;
try 
{
transport = new TSocket("localhost", 7911);
TProtocol protocol = new TBinaryProtocol(transport);
Client client = new Client(protocol);
transport.open();
long time = client.time();
System.out.println("Time from server:" + time);
transport.close();
}
catch (TTransportException e) 
{
e.printStackTrace();
}
catch (TException e) 
{
e.printStackTrace();
}
}


private void startSslClient()
{
TTransport transport;
try 
{
TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters();
params.setTrustStore("C:/Users/jamel/eclipse.helios.workspace/ThriftClientServer/certificat_ssl/sastruststore.jks", "sas001");
transport = TSSLTransportFactory.getClientSocket("localhost", 7912, 10000, params);
TProtocol protocol = new TBinaryProtocol(transport);
Client client = new Client(protocol);
//transport.open();
long time = client.time();
System.out.println("Time from server:" + time);
transport.close();
}
catch (TTransportException e) 
{
e.printStackTrace();
}
catch (TException e) 
{
e.printStackTrace();
}
}


public static void main(String[] args) 
{
TimeClient c = new TimeClient();
//c.startSampleClient();
c.startSslClient();
}
}


7- Create keystore and trustore for SSL


createSQSKeystoreaAndTruststore.bat


@echo off


if "%JAVA_HOME%" == "" (
set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0\"
)
set "KEYTOOL_CMD="%JAVA_HOME%bin\keytool.exe""


rem créer le fichier de keystore
%KEYTOOL_CMD% -genkeypair -alias sascertificatekey -keyalg RSA -validity 7 -keystore saskeystore.jks -storepass sas001 -keypass sas001
echo saskeystore.jks crée


rem créer le fichier cert.cer
%KEYTOOL_CMD% -export -alias sascertificatekey -keystore saskeystore.jks -rfc -file cert.cer -storepass sas001 -keypass sas001
echo cert.cer crée


rem créer le fichier de sastruststore
%KEYTOOL_CMD% -import -alias certificatekey -file cert.cer -keystore sastruststore.jks -storepass sas001 -keypass sas001
echo Certificat importée avec succée


@pause



END