Dynamic and Static Discovery cluster with Apache TomEE

Hey there!

On my last post Are there different types of clusters? we saw two different types of clusters: Dynamic Discovery and Static Discovery. To see details, see the post! 😉

Now let’s see quickly how we can set them up at the Apache TomEE. One of the types – the Dynamic – we have already seen in the post How to build an Apache TomEE cluster. So we will do just a little zoom to see where the things are done.

 

Dynamic Discovery

At the “Engine” node you should put this code (see the post mentioned above for detailed code):

<Cluster
className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
channelSendOptions=”6″>
<Manager…
<Channel…
<Valve…
<Deployer…
<ClusterListener…
</Cluster>

 

Static Discovery

At the same “Engine” node you should put this:

<Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”channelSendOptions=”6″>
<Channel className=”org.apache.catalina.tribes.group.GroupChannel”>
<Interceptor className=”org.apache.catalina.tribes.group.interceptors.StaticMembershipInterceptor”>
<Member className=”org.apache.catalina.tribes.membership.StaticMember” port=”4000″ host=”server1″ uniqueId=”{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}” />
<Member className=”org.apache.catalina.tribes.membership.StaticMember” port=”4000″ host=”server2″ uniqueId=”{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}” />
<Member className=”org.apache.catalina.tribes.membership.StaticMember” port=”4000″ host=”server3″ uniqueId=”{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}” />
</Interceptor>
</Channel>
</Cluster>

 

So as you can see, with the Static Discovery you use the StaticMember at the Member node instead of McastService used at the Dynamic Discovery. To see the functional differences between them see my last post Are there different types of clusters?.

Hope you enjoy building your own TomEE cluster! If you have any particular experience to share, please leave it at the comments bellow. See you!

Are there different types of clusters?

In my last post “How to Build an Apache TomEE cluster” we saw how easy and fast is to deploy your application in a cluster and take all its advantages.

It’s interesting to keep in mind we have basically two different types of clusters. Let’s see them.

 

Dynamic Discovery

In a Dynamic Discovery cluster each node is ready to find another nodes in its network as soon as they are up and running.

Pros

It’s the most flexible (and dynamic!) way to build your cluster, as you can scale your architecture by just turning a server on or off.

Cons

You need to take care to not put on your cluster a node you don’t want to. If you have different clusters for different applications in the same network and all of them are Dynamic Discovery clusters, they will “see” each other nodes and you can imagine the mess! Of course that are some configurations to avoid it, but you need to do it very carefully.

 

Static Discovery

In a Static Discovery cluster each node has a previous knowledge of all nodes that are in the cluster. No surprises!

Pros

You have much more control of your cluster and your architecture than in a Dynamic Discovery cluster. As the nodes are previously defined you can set another cluster in the same network without worrying about if they will see each other, because they won’t.

Cons

There are not much flexibility for scalability as you need to configure all your nodes if you want to add a new one (to remove just shut it down). It’s ok if you have 2 or 3 servers (nodes), but imagine 10… 20… 100!!

 

So… what’s the best one? Depends! Different scenarios and architectures should use the best features of one type or another. If you want my opinion just ask it!

 

How to build an Apache TomEE cluster

If you have an application or a system and it is running, you surely want it to be available. Depending on the scenario, you need it to be highly available! Or perhaps you just want to balance the loading on your server.

In any of those cases a good choice maybe is to build a cluster. Just to give you an overview and in the case this is a new word for you, cluster is a set of independent servers that communicate to each other thru a network in order to make a service or a system to be more available than if you used just one server.

Each server of the cluster is called a node. That are some different types of cluster, but we will cover it in another post.

If you are a Java developer and are familiar with Tomcat you will find TomEE quite easy to use. They have almost the same structure and are very likely to setup. The “plus” for TomEE is: it is Java EE (6) compatible for web profile.

What does it mean? Means that it implements all the Java EE 6 web API’s related. So if you are a Web Developer and wanna take advantage of Java EE 6 on your project, the Apache TomEE could be a good choice.

So you have an application, want to run it in a TomEE and want to do it in a cluster? Easy! Let’s do it step by step.

  • Download and install the JDK and the Apache TomEE. I’ll assume you are familiar with it. I am wrong, please leave a message at the comments bellow;
    • The versions used were: JDK 1.8.73 and Apache TomEE 1.7.3 Web Profile. The SO was a Mac OS X 10.11.3 (El Captain).
  • After installing try to run your TomEE and see if it is working;
  • Go to your TomEE Home folder and edit the following file:
    • /conf/server.xml
  • Add those lines to the “Engine” node:
<Cluster
className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
channelSendOptions=”6″>
<Manager
className=”org.apache.catalina.ha.session.BackupManager”
expireSessionsOnShutdown=”false”
notifyListenersOnReplication=”true”
mapSendOptions=”6″ />
<Channel
className=”org.apache.catalina.tribes.group.GroupChannel”>
<Membership
className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.4″
port=”45564″
frequency=”500″
dropTime=”3000″ />
<Receiver
className=”org.apache.catalina.tribes.transport.nio.NioReceiver”
address=”auto”
port=”5000″
selectorTimeout=”100″
maxThreads=”6″ />
<Sender
className=”org.apache.catalina.tribes.transport.ReplicationTransmitter”>
<Transport
className=”org.apache.catalina.tribes.transport.nio.PooledParallelSender” />
</Sender>
<Interceptor
className=”org.apache.catalina.tribes.group.interceptors.TcpFailureDetector” />
<Interceptor
className=”org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor” />
<Interceptor
className=”org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor” />
</Channel>
<Valve className=”org.apache.catalina.ha.tcp.ReplicationValve”
filter=”.*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt” />
<Deployer
className=”org.apache.catalina.ha.deploy.FarmWarDeployer”
tempDir=”/tmp/war-temp/”
deployDir=”/tmp/war-deploy/”
watchDir=”/tmp/war-listen/”
watchEnabled=”false” />
<ClusterListener
className=”org.apache.catalina.ha.session.ClusterSessionListener” />
</Cluster>
  • Save and close your file, then restart your TomEE. If everything is ok, your log file should have something like this:
org.apache.catalina.ha.tcp.SimpleTcpCluster startInternal
Cluster is about to start
org.apache.catalina.tribes.transport.ReceiverBase bind
Receiver Server Socket bound to:/192.168.0.104:5000
org.apache.catalina.tribes.membership.McastServiceImpl setupSocket
Setting cluster mcast soTimeout to 500
org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
Sleeping for 1000 milliseconds to establish cluster membership, start level:4
org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
Done sleeping, membership established, start level:4
org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
Sleeping for 1000 milliseconds to establish cluster membership, start level:8
org.apache.catalina.tribes.membership.McastServiceImpl waitForMembers
Done sleeping, membership established, start level:8
  • Great! Now you have the first node of your cluster up and running. Not enough, right? So do it to another server, could be another machine or even a VM. Do it to as many servers as you want according to your needs. Each time you add a new node, the log file of other nodes should contain something like this:
org.apache.catalina.tribes.io.BufferPool getBufferPool
Created a buffer pool with max size:104857600 bytes of type:org.apache.catalina.tribes.io.BufferPool15Impl
org.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
Replication member added:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 0, 107}:5000,{192, 168, 0, 107},5000, alive=1016, securePort=-1, UDP Port=-1, id={111 -38 59 -66 -68 -29 72 -69 -103 12 -121 -120 -13 -25 -90 17 }, payload={}, command={}, domain={}, ]
  • Now you have your beautiful cluster fully happy and working! There is just one thing missing: your application deployed to it. Simple:
    • Edit the web.xml of your application;
    • Add this attribute: <distributable />;
    • Save the file, close it and deploy to your cluster.

Couldn’t be easier, right? Of course there are some other aspects, some architectural gaps and another options, but I will leave it for your comments and for another posts.

See ya!

Blogging course

I have never thought it would exist a thing like a blogging course. I always seen blogging as an empirical thing, specially for technical guys like me (though I always love to write).

Then someday somebody told me: “Do you want to create a blog? So check this site…”:

http://simpleprogrammer.com/

This person told me if I REALLY wanna try to run a blog, I should take the free course offered by this site.

Man, why the heck somebody would teach someone anything for free?“.

Well, the same answer again: “Why not?“.

I applied for the course and still was trying to get how the things would work… and if they would work.

Now I really can tell you that was one of the best decisions I ever made. The course is given by John Sonmez, a successful programmer who find out how to use a blog to boost your career. And he teaches from his own experience.

The course also took me to start listening to Simple Programmer Podcast and it’s been such a great time in the middle of terrible traffic of Sao Paulo.

You may say: “Why are you so excited? You have just started your blog!“. Yes, this is true. And that’s why I am so excited!

The point of the course is not only creating a blog itself, but the whole things around it. John teach us about planning, focus, theme selection, career, search engines, self marketing and some other things. If I never become a successful blogger (though I believe I will), even then I can assure you that the lessons will be still valuable for my career.

If you wanna check it out, take a look at the site above or at the course hot site:

http://devcareerboost.com/blog-course/

You won’t regret!

Talking about dreams

Hey folks! What’s up?

Today I will talk about dreams. Yup, dreams. If you think that dreaming has nothing to do with your career, so maybe you need to make up your mind…

In the end of 2015 I received an e-mail from Bruno “Javaman” Souza (http://anonovoprojetonovo.com) talking about dreams for 2016. Don’t need to share too much details, but in the end of message he asked for anybody who feel like to send him their dreams for 2016.

Well I said “why not?”. At least to write a list would help me to organize my thoughts… And the list was:

  1. Get the OCM-JEA certification
  2. Write a technical article
  3. Be a speaker in some event

And then we started to talk to each other about those items and how to make them real. In a couple of weeks he told me about an opportunity to write an article to a major publication (will told you when it is published). Wow! Ok, let’s do it.

Next conversations was about themes, topics and how we would split the work to be done.

We choose a very nice topic and the article was done successfuly… but the purpose here is to share with you how this was an amazing experience for me and how I think i will affect my career and the way I think about somethings:

  • First of all being oriented by one of the most Java influencers in the world was an unique opportunity and a great experience. Bruno is a very nice person, has tons and tons of knowledge and is totally willing to help others;
  • Writing a technical article is not a kind of easy job. You need to research, test, write code, then research again, test again, write code again… And then write about it, and write again… Maybe while you are learning something new! If you are not used to it in your daily activities, maybe you feel a little uncomfortable in the beginning;
  • Having a target of saying something valuable to others is great! You have to learn, you have to think twice before say anything and you need to be very careful on what you are doing.

This was only the first article. I am not willing to stop! And I am very sure that this will impact my career in a very effective way. Not only by learning and sharing things with a highly active community of developers, but also by the whole process needed to achieve it.

I truly recommend you to try it! If you want to just leave a comment to this post and will be my pleasure to help you as somebody did to me.

In the beginning

Hey you! Glad you are here, whoever you are…

I’ve started this blog specially to help myself in my preparation to the Java EE Enterprise Architect Certification. I think if I keep writing and working on it, my results will get better constantly.

As a side affect, maybe my posts would help somebody anytime anywhere. Who knows?

My formal preparation will begin em January (yes, I will enjoy Christmas and New Year’s Day first). So for now I’ll try to keep in touch with WordPress. Maybe one post here and there…

Oh! At last, but not at least: my English is not so bad, and not so good. But so does yours, so I am cool with this.