We’ve been working at 4financeit for last couple of months on some open source solutions for microservices. I will be publishing some articles related to microservices and our tools and this is the first of (hopefully) many that I will write in the upcoming weeks (months?) on Too much coding blog.
This article will be an introduction to the micro-infra-spring library showing how you can quickly set up a microservice using our tools.
Introduction
Before you start it is crucial to remember that it’s not enough to just use our tools to have a microservice. You can check out my slides about microservices and issues that we have dealt with while adopting them at 4financeit.
Here you can find my video where I talk about microservice at 4finance (it’s from 19.09.2014 so it’s pretty outdated)
Also it’s worth checking out the articles of Martin Fowler about microservices, Todd Hoff’s – Microservices not a free lunch! or The Strengths and Weaknesses of Microservices by Abel Avram’s.
Is monolith bad?
No it isn’t! The most important thing to remember when starting with microservices is that it will complicate your life in terms of operations, metrics, deployment and testing. Of course it does bring plenty of benefits but if you are unsure of what to pick – monolith or microservices then my advice to use is to go the monolith way.
All the benefits of microservices like code autonomy, doing one thing well, getting ridd of pacakge dependencies can be also achieved in the monolithic code thus try to write your applications with such approaches and your life will get simpler for sure. How to achieve that? That’s complicated but here are a couple of hints that I can give you:
- try to do DDD. No, you don’t have DDD when your entities have methods. Try to use concepts of aggregate roots
- try not to make dependencies on packages from different roots. If you have two different bounded context like com.blogspot.toomuchcoding.client and com.blogspot.toomuchcoding.loan – go via tight cohesion and low coupling – emit events, call REST endpoint, send JMS messages or talk via strictly defined API. Do not reuse internals of those packages – take a look at the next point that deals with encapsulation
- take your highscool notes and read about encapsulation again. Most of us make the mistake of thinking that if we make a field private and add an accessor to it then we have encapsulation. That’s not true! I really like the example of Slawek Sobotka (article in polish) who shows an example of common approach to encapsulation:
human.getStomach().getBowls().getContent().add(new Sausage())
instead of
human.eat(new Sausauge())
- add to your IDE class generation template that you want your new classes to be package scoped by default – what should be publicly available are interfaces and really limited number of classes
- start doing what’s crucial in terms of tracking microservice requests and measuring business and technical data in your own application! Gather metrics, set up correlation ids for your messages, add service discovery if you have multiple monoliths.
I’m a hipster – I want microservices!
Clone a repo and get to work
- uses micro-infra-spring library
- is written in Groovy
- uses Spring Boot
- is built with Gradle (set up for 4finance – but that’s really easy to change)
- is JDK8 compliant
- contains an example of a business scenario
- check out the slides above to see our approach to microservices
- remove the packages com/ofg/twitter from src/main and src/test
- alter microservice.json to support your requirements
- write your code!
- you don’t have to set up anything – we’ve already done it for you
- the time required to start developing a feature is close to zero
Aren’t we duplicating Spring Cloud?
- correlation id setting (we support different libraries) which is crucial for log aggregation approaches
- metrics path prepending (accoriding to a defnied pattern)
- giving you Swagger API and Swagger UI
- allowing you to pick properties from a repository on your drive
- soon we will be able to sketch graphs of dependencies between services
- we give you Consumer Driven Contract approach
- testing our solutions on production – as a company we’re giving short term loans so this in our system where we test our open source ideas
Conclusions
What’s next
This post is part of the Java Advent Calendar and is licensed under the Creative Commons 3.0 Attribution license. If you like it, please spread the word by sharing, tweeting, FB, G+ and so on!
Author: Marcin Grzejszczak
Author of “Mockito Instant” and “Mockito Cookbook” books. OSS Contributor. Co-founder of the Warsaw Groovy User Group and Warsaw Cloud Native Meetup.
Lead of Spring Cloud Sleuth, Spring Cloud Contract and Spring Cloud Pipelines projects at Pivotal
Contributed to Groovy, Mockito, Rest-assured, Drools, Moco. Author of Uptodate Gradle plugin, Spock subjects-collaborators extension, gradle-test-profiler, JSONAssert and XMLAssert open source projects.