Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Java MicroService

I been searching on the web but im a little confused on what exactly is a java microservice. I mean i know what a web service is, and im told that a microservice is the following per wiki:

In computing, microservices is a software architecture style in which complex applications are composed of small, independent processes communicating with each other using language-agnostic APIs.

and the properties of a microservice are:

Properties of microservices architecture:

It's a kind of architecture The services are easy to replace Services are organized around capabilities, e.g. user interface front-end, recommendation, logistics, billing, etc Services can be implemented using different programming languages, databases, hardware and software environment, depending on what fits best Architectures are symmetrical rather than hierarchical (producer - consumer)

but i need a concrete java example to understand how i can make a microservice. Does anyone have a example you could provide ?

like image 466
j2emanue Avatar asked Nov 12 '15 20:11

j2emanue


1 Answers

A microservice is exactly like its name suggests. It's a tiny service that performs a very simple function.

So yes, in terms of code, you're probably looking at a REST service. Note that any other API style would work. It doesn't have to be REST, however it must be language agnostic for you to draw all the benefits.

But the idea is bigger than that. The idea behind it is that they are very specialized and there's no big business workflow attached to they. For example, if you have a service that processes payments, then writes an audit log, then notifies the customer. I wouldn't count that as a microservice. Writing an audit log, that'd probably be a microservice, notifying the customer too, processing the credit card as well. Your system would coordinate that business workflow (the 3 steps mentioned above) by calling the 3 necessary microservices. So your system is a coordinator and you don't worry about implementing the business functions.

Microservices don't try to think too much, they just do as they're told, but they do it quickly.

So in short. Take a very simple business function, put a REST API in front of it. And you have a microservice.

There are several interesting properties with microservices:

  1. They can be deployed independently. That's nice because you can deploy pieces of your app without taking everything down all at once.
  2. They can run in their own silo. So if you have a microservice that is very memory hungry, it can be deployed on a separate server so it doesn't affect the rest of your system.
  3. They can use different technology. You may have microservices in Java, some on .Net
  4. They keep dependencies in check. Developers have a tendency to bleed dependencies from one component to the next when they live together. Here you can't.

But you should also consider some drawbacks

  1. It's a lot harder to maintain a consistent transaction across all these calls. If you need to roll back, you'll need JTA to rollback all the REST calls. That can be a pain and doesn't perform very well in my experience.

  2. Tracking a transaction through the system in order to troubleshoot can be pretty rough if you don't have consistent logging and a consistent transaction id

  3. It might get tricky to find where a defect was introduced if the ground is constantly shifting due to deployments that may have side effects.

  4. Obviously all these REST calls can stack up to be pretty expensive. I'm not necessarily talking about the transport since it's likely all your microservices would live in the same data center. But every time you make a call over the network, you have to marshal/unmarshal your data and that could quickly get expensive CPU-wise.

like image 170
mprivat Avatar answered Oct 07 '22 18:10

mprivat