Translating and Adapting Java Microservices: A Practical Guide . Link to the first part of the guide . Any Java server program, and therefore any microservice, is just a .jar or .war file. There's one great thing about the Java ecosystem, or rather the JVM: you only need to write Java code once and it can run on almost any operating system, as long as you don't compile your code with a newer version of Java than your target JVM version. . This is important to understand, especially when it comes to topics like Docker, Kubernetes, or (drumroll!) The Cloud. Why? Let's look at different deployment scenarios.
Minimalistic Java Microservice Deployment Example
We continue with the bank example. So we've got the monobank.jar file (monolith) and our newly extracted riskengine.jar (the first microservice for risk checking). Let's also assume that both applications, like every other application in the world, need a .properties file. In our case, it will only contain the database URL and credentials. A minimal deployment might consist of two directories that look something like this: First:
-r-r------ 1 ubuntu ubuntu 2476 Nov 26 09:41 application.properties
-r-x------ 1 ubuntu ubuntu 94806861 Nov 26 09:45 monobank-384.jar
ubuntu@somemachine:/var/www/www.monobank.com/java$ java -jar monobank-384.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
...
Second:
-r-r------ 1 ubuntu ubuntu 2476 Nov 26 09:41 application.properties
-r-x------ 1 ubuntu ubuntu 94806861 Nov 26 09:45 risk-engine-1.jar
ubuntu@someothermachine:/var/www/risk.monobank.com/java$ java -jar risk-engine-1.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
...
This leaves the question open: how will the .properties and .jar files get on the server? Alas, there are many answers.
How to use build tools, SSH and Ansible to deploy Java microservices
Boring, but no less excellent advice on how to deploy Java microservices ... Actually, exactly the way system administrators deployed any Java server program in companies over the past 20 years. This is the mix:- your favorite build tool (Maven, Gradle)
- good old SSH/SCP to copy .jars to servers
- Bash scripts to manage deployment scripts and servers
- or even better: some Ansible scripts.
How to use Docker to deploy Java microservices
Let's get back to the sweet pangs of choice. A couple of years ago, Docker entered the scene, and with it, containerization. If you've never worked with it, here's a brief description for end users and developers:- The container (simplified) is similar to the good old virtual machine, but “easier”. If you don't understand what "easier" means in this context, you are welcome to study this answer on Stackoverflow .
- The container guarantees its own portability. That is, it works anywhere. Sounds familiar, doesn't it?
- merge your jar into a docker image
- commit this image to a private docker registry
- pull and run this image on your target platform
- or copy the Docker image directly to your prod system and run it.
How to use Docker Swarm or Kubernetes to deploy Java microservices
Let's say you decide to try out Docker. Every time you deploy a Java microservice, you create a Docker image that bundles your .jar file. Let's say you have a couple of these Java microservices, and you want to deploy these services on multiple machines (in a cluster). The question arises: how to manage this cluster? Run Docker containers, health check, deploy updates, scale the system (brrr)? Two possible answers to this question are Docker Swarm and Kubernetes. A detailed description of both options would make this already long guide too long, but we thought it was important to mention that both options ultimately rely on you writing YAML files (see the stories about Yaml indentation) .) to manage your cluster. If you want to know how it feels in practice, just search Twitter for a similar query. So the deployment process for your Java microservices now looks something like this:- Configuring and managing Docker Swarm/Kubernetes
- All Docker steps (see above)
- Write and execute YAML
until your eyes bleeduntil everything works.
How to Test Java Microservices
Suppose you decide to implement microservices in production. How now to test the integration of n-microservices during development? How can I see if the complete workflow is running, not just individual parts of it? In practice, one of three methods can be applied:- With a bit of work (if you're using frameworks like Spring Boot), you can bundle all your microservices into a single startup class and load all the microservices with a single Wrapper.java class - depending on whether you have enough memory on your machine to start them all. your microservices.
- You can copy Docker Swarm or Kubernetes settings locally.
- Just don't run integration tests locally anymore. Instead, deploy a dedicated DEV/TEST environment. This is what quite a few commands actually do, succumbing to the pain of local microservice setups.
GO TO FULL VERSION