Wednesday, December 31, 2014

Microservices: Shared libraries or big services

After some fun with microservices a fork in the road has appeared.

Say we're developing an app (just like I am):

The web-part doesn't exist yet, it will be the web GUI (Spring boot MVC).

We have two small services:

  • UserRegistration
  • UserInfo

Since they use the same data I choose to group them with the same database.

So when we register a new user, the info will be sent to the UserRegistration-service by AMQP-message via RabbitMQ.

The UserRegistration-service puts the data in the database. To control the structure we make a POJO (JPA or other, depends on the database eg. MongoDB).

And there is the problem. The structure (the POJO) is in the UserRegistration-service project!

So when the UserInfo-service retrieves the data you probably want the same structure as you put it in.

There is probably more solutions but...

I see two obvious roads to go about:

  1. Make the services bigger and have all the user-related stuff in one big service
  2. Have a shared library with the code that needs to be shared
Personally I think option 1 is too risky. The service might become a giant service so the "micro"-part might disappear. 

So how do we do option 2? 
The code could either be a separate project and use some form of distributer to a artifact repository or try to make it part of one of the services (probably the one who puts it in the database) and try to package that part of the code some how.

This adds another level to the complexity. The need to share libraries, that the services can't be 100% independent.

This is a big part of the microservice architecture, it is somewhat complex. There is a lot of benefits to reap but you can probably solve this much easier in a monolith. 
On the other hand there is a lot of benefits if you can get the microservices right:
  • Fast development
  • Independent deployment of services
  • Scaling
  • Services can be in different programming languages
So now I think I will deploy a Sonatype Nexus repository as a Docker-instance and share the User-structure.

To be continued...

Quick update:
After reading a bit about others experience concerning microservices another problem came to mind.
https://rclayton.silvrback.com/failing-at-microservices
If you have shared code between services, especially Models (entity structures) this is most likely written in a specific programming language. This takes away one of the aspects of microservices.

So how do we solve this? This will take some thinking (I would like a good place to discuss these problems). My first thought is to find some JSON-structure due to the AMQP and send all messages by JSON-format (formatted as Byte arrays). Hopefully this makes it possible to use the message in another language.

And Happy New Year!




Tuesday, December 23, 2014

Microservices continued: The messaging begins

So I've started with a fun project: http://java-viking.blogspot.com/2014/12/mircoservices-with-java-spring-boot-and.html

So the first piece of the puzzle was to have individual service running. This is easily solved with Spring Boot. Since most services doesn't even be need to run as Web services (run in a servlet-container) it can be as small as a few mb!

So I wanted all the communication between the services to be solved by messaging.
The "normal" way is point-to-point. The problem is that you are bound by the API, sure you can do good coding and use a interface so you actually can switch the implementation. But somewhere the implementation is bound. When you need to do a swap, that redeployment will disturb the things that uses it.

Can you do a redeploy without disturbing the system?

Answer: Not sure, but I will try!

My thought is this: If you have services that uses messages queues then you can theoretically have several instances of that service handling messages in the same queue (not the same messages but the same type of messages).

If that works then you could have an instance of the old version and an instance of a new version (where a bug is fixed) and then remove the old instance and work would probably not have been interrupted.

This is also the solution for scaling that I'm testing. Can you have several instances of the same service?
So far so good, my fear is when users and session come and messes things upp.

AMQP

So instead of JMS (Java Message System) I'm using AMQP (Advanced Message Queuing Protocol).
One of the reasons is that is has clients for several languages so you can have some services with another language.

"AMQP mandates the behavior of the messaging provider and client to the extent that implementations from different vendors are truly interoperable, in the same way as SMTP, HTTP, FTP, etc. have created interoperable systems. Previous attempts to standardizemiddleware have happened at the API level (e.g. JMS) and thus did not ensure interoperability.[2] Unlike JMS, which merely defines an API, AMQP is a wire-level protocol. A wire-level protocol is a description of the format of the data that is sent across the network as a stream of octets. Consequently any tool that can create and interpret messages that conform to this data format can interoperate with any other compliant tool irrespective of implementation language." (Wikipedia)

Spring, of course, has a project for this: Spring AMQP. I'm using Spring Boot Starter AMQP (not fully sure yet what the "starter" is about)

Status:

So where am I right now (December 23)?
I've done a service which listens to a queue on RabbitMQ (I named the queue and RabbitMQ is running in docker, use the official dockerfile/rabbitmq)

This service will handle every message in that queue, it will print what the message is and change the message.

If the service that sends the message demands that it wants a response, that service will get the changed message.

The other service just sends 10 messages (with Thread.sleep(1000) between each message) to the queue. This now works like a charm!

But I had some big problems with this! First off, I'm new to spring boot and messaging.
So I could quickly make these services with examples from the web.

Problems:

The problem was that this only worked with one instance at a time! No bueno!
I started the MessageHandler-service, no problem.
I started message-sending-service instance 1, no problem.
I started message-sending-service instance 2, the messages got jacked by instance 1!
And instance 1 saw it as the message was sent after the timeout (no kidding! since it actually already got a response before!)
The problem was that the MessageListenerContainer that was used was still running after the response-demand was fulfilled. 
If I stated instance 1 and 2 and then started the message-handler (love queues!) they got there responses correct.

So how do I fix this? (many hours of fun)

First came the o-so-fun task of actually finding out what it's called. (send-respond/receive-respond and more)

It seems that most people use either a separate queue for each client or each request!
This seemed like a performance hog.
But in RabbitMQ 3.4.0 there is a function for a pseudo-queue for just these occasions.
It's called Direct-to. We let the code and RabbitMQ handle the response (so that the right message comes to the right sender).

The Docker-RabbitMQ was version 3.4.2 lucky!
Next problem: this is supported in Spring AMQP 1.4.1.

What version does Spring Boot 1.2.0.RELEASE have of AMQP? 1.4.0....     O joy...

I tried that version and it didn't work. I'm not sure if Spring Boot allows me to switch a version of one of its sub-projects. I didn't get it to work, but it might just be my incompetence .

So I took my chance with version 1.2.1.BUILD-SNAPSHOT! 

And hey what do you know, it worked! I deleted the response-listener completely in the message-sender, the Direct-to function handles it for ous.


Minor setback:

The "pseudo-queue" for the Direct-to function https://www.rabbitmq.com/direct-reply-to.html#use
Have an official queue name " amq.rabbitmq.reply-to", it needs to be exactly this!

I stumbled upon this in a StackOverflow answer. The Spring Documentation actually had a typo of this: http://docs.spring.io/spring-amqp/reference/html/amqp.html#direct-reply-to

under section 3.7.1 it said " amqp.rabbitmq.reply-to" that "p" should not be there!
And I understand how easy of a mistake this is, I thought it was the other way around, why would it be "amq" instead of "amqp"?

But no problem, I submitted the typo to the Spring Jira issue site (issue AMQP-458) and the awesome Gary Russell has already fixed it, it'll be in the 1.4.2 version: https://github.com/spring-projects/spring-amqp/commit/fca82ce3d54bb2024efa6275a2e661cd20829de6

You got to love Open Source!

I'm happy to help fix this, cause you don't get any fun errors if you do this typo.
You just don't get any response. The message gets handled but that's it. Now the message-senders is just waiting for the postman that never will come.


So I hope this will help someone, I will keep you posted with my findings and results. In the end I will probably give some code, just now it's not so pretty ^^ except the gradle, that is pretty!

If your trying to replicate my idea and get stuck, don't hesitate to comment and I will try to help you as best as I can.

PS: These services will have there own Docker-containers and be run from there. They don't need to know about each other or there respective IP-numbers, you gotta love Messaging systems.

So code away!




Sunday, December 21, 2014

Mircoservices with Java, Spring Boot and Docker

Intro to microservices

One of the current buzz-words flying around is "Microservices".

Most big java apps (web-based at least) are packaged as a big WAR-file. The project is one big app, a monolith. This comes with some problems concerning development.

First the obvious, if you change anything you need to redeploy the whole thing. Even the smallest thing.

Second is scaling. If you need to scale up then you scale the whole application.


So what is microservices? Instead of having a monolith you make services as separate projects.
A service would have just one area of responsibility ex. Login-service.

Pros:
Fast development and deployments
Easier to write tests (small project > don't disturb other tests)
Can redeploy that service without redeploying everything
Scaling, just start another instance of the service (needs a good framework for this!)

Problems

So is it a hail mary-technique?

As always, this technique/architecture has its own flaws and problems.

The main con is the complexity of service-handling. The services needs some way of connecting to each other. And "hard-coding" the connection gives us a BIG risk. Because if you change one service, all the services that uses the API of that service might crash in runtime, fun to be that admin.

And how do we know that a service is down?
Say the login-service goes down, the positive is that the app might still work for the people that is already logged in. But no new logins can occur until the service is up again.
With a good health monitoring system ex. Nagios it can check if the service is down, some can even fix the problem!

Solutions

So how do we solve these problems?

First the decoupling from the API. If the services are decouple and doesn't use the API one-to-one we'll escape the hellish nightmare of the total system crash (in this case, might be others).

So we can use some form of integration system like an ESB (Enterprise Service Buss) or just a messaging system (ex. RabbitMQ or ActiveMQ).
This will send the message in to a queue, the service will then grab the message and handle it (complexity gets higher).


Second

A awesome system would be some form of clustering over different locations, load balancing, service discovery and health monitoring.

Some frameworks can actually call the "mother ship" and say "hey I'm bla bla service and I'm ready to Rock! I'm at this location".
The service would have an adjacent health monitoring script.

Is there an actual solution for ALL of this? Answer: Not that I know, but there is some puzzle-pieces.

My Experiment (the plan!)

I'm going to try to make a simple project using microservices.

I'm going to use Spring Boot for the service. Spring Boot is a small application which has a jetty/tomcat server in it and it can either be run as a WAR or by itself.
I'm going to run it by itself. This becomes a app with 12-20 mb per service.
Gradle will be used for the build and dependency scripts.

After the service-app is done, I will contain it in a simple Docker image that is built on start.

Ain't it a problem to start all these docker containers (I will have 3-5 services)?
No, I will use fig (http://www.fig.sh/index.html) to start all the services at once, and to document all the start-commands, linkages, port exposures and more. To sum it: A docker kick-starter.


Ok so Java: check
Containers: check


So regarding the health monitoring. I will try out Consul (https://consul.io/intro/index.html).
I hope I can rock it good with docker and use both the service discovery and the health monitoring.


Onwards

So right now I have made a test-service (hello world in spring boot) and made a docker image of it and ran it successfully.

I will keep you posted of my results, hopefully I can put it all on github when it's done so you can get all the source code.

Another hope I have is that Docker Swarm takes of and either becomes powerful enough on its own or that some awesome frameworks gets created that uses it.