Tomcat clustering

In this tutorial I will share how to setup two tomcat nodes with FramWarDeployer, this will allow you to autodeploy all wars to all nodes automatically with session sharing cross cluster.

Tomcat Cluster configuration

Assuming that you have downloaded and installed both tomcats, you must update server.xml located in config folder of the tomcats.

Locate the Server -> Service -> Engine and set jvmRoute  attribute to tomcat1 and tomcat2 like this:

Locate Server -> Service -> Engine -> Host and add :

For the second tomcat you can if you choose so to use different receiver port.

Observe that there are two Manager tags, one is for BackupManager and other is for DeltaManager. Both are used for session replication.

  • DeltaManager copies sessions to all other nodes
  • BackupManager copies to only one node, with this difference there is a huge difference in performance when more then two nodes are included.

Check the load test results for both managers here.

FarmWarDeployer

To setup auto deployment cross cluster you need to add FarmWarDeployer , in each server.xml you should add the with corrections in deployDir and tempDir paths. With enabling watchEnabled to only one tomcat that you think of as manager.

After you add/edit/remove war file in watchDir this war will be automatically deployed cross your cluster.

Load balancing with apache2

To allow load balancing with apache2 you should utilize this configuration:

Problem with it is that two consecutive requests from same user/client might not go to the same tomcat. This can be a problem depending of the manager you used to configure the cluster and of how many tomcats you have. If this is a problem you can simply setup load balancing with stickiness, you can read more on apache2 mod_proxy documentation page. Here is the example.

 

To force the server to listen to IP v4 instead of IPv6 set next line into bin/setenv.sh .

 

I hope that this post was helpful to you.

 

 

Docker Swarm on local MacOS

Recently docker has released Docker for Mac and it works really well since it is a native application and does not use docker machine concept, but if you want to play with docker swarm you can not.

In this tutorial I will show you how to start with docker swarm with all required commands.

Docker Machine

First of all you need to utilize docker-machine and create 3 machines ( you can do more if you want but 3 will do it). These machines are going to be our cluster to play with. Execute next commands:

To make sure that all is executed correctly execute:

output should be something like this:

Awesome now we have created 3 docker machines and are ready to make the swarm.

Docker Swarm

Docker swarm is simple to create but first we must ssh into the manager1 node using

To initialize the cluster execute (observe the IP address that is bolded above):

output is going to be similar to this one (copied from official documentation). Command made this node the manager.

In the message above we have explanation how to add a worker node  and how to add manager node into the swarm. To get us started with our playground we need only two worker nodes, but if you want go and see what happens.

Once we have our first manager we will ssh to both worker nodes using ssh command and paste the swarm join command.

After you add both worker nodes to the swarm it is time to verify that all is correctly done using

Here is the glitch , it prints an error. Reason for this error is that docker client does not point to correct docker machine.

To fix this issue you need to execute ( this just sets the environment variables like dockerHost, machineName, certPath and TLSVerify):

now if you repeat the command docker node ls you should see something like this (observe that manager1 is elected as a Leader):

Here is a few commands that you will use:

  • docker node ls

  • docker node ps

  • docker node inspect manager1

  • docker node update –availability active worker1

  • docker node update –availability drain worker1

Docker Swarm In Action

Using above commands we have created a swarm with the manager and have pointed our docker client to our swarm. Now we need to start creating the services.

To get started I will deploy nginx and scale it to 3 nodes with some extra parameters for fun.

  • replicas, specifies how many replicas do you want
  • name, is the name of the service
  • reserve-memory, specifies the RAM available for the service
  • publish, is the port that is swarm listening to and where our app can be accessed
  • update-delay is related to the deploying the new version

Lets make sure all is ok using

output would be similar to

To access the service you should open the browser to any of the next addresses, each address is the IP address of the node. Regardless if this node contains the job or not all will work fine.

  1. http://192.168.99.101:8080
  2. http://192.168.99.102:8080
  3. http://192.168.99.103:8080

Change the scaling

Deploy new version

docker service update --image nginx:version webapp

Apache2 load balancing

Lets assume that you want to add apache2 in front of the docker swarm that acts as a load balancer, here is how to do this

 

JPA @Temporal annotation – hibernate

JPA @Temporal annotation should be used to annotate class attributes of type java.util.Date  and java.util.Calendar in order to specify time precision. Precision can be date, time or datetime where datetime is default.

Here are all examples of the annotation usage

Lets observe SQL create table command

 

Data saved into the date related columns like this:

  • DATE_ =>2016-07-27
  • DEF_ =>2016-07-27 19:12:45
  • TIME_ =>19:12:45
  • TIMESTAMP_ =>2016-07-27 16:12:45

Happy coding.