Introducing Microserver : a zero config Java 8 REST server for microservices

A zero configuration, standards based, battle hardened library to run Java Rest Webservices via a standard Java main class from AOL. (free CD not included)

Microserver is an opinionated Java 8 REST microservice server from AOL.

GitHub Page

Google Group : https://groups.google.com/forum/#!forum/micro-server

Rationale

To make developing Microservices in Java 8 simple.

The code snippet below, will start a Grizzly NIO JavaEE webserver on port 8080, with a Jersey 2 REST end point on ‘simple-app/status/ping’, configured to serialise and deserialise entities with Jackson 2.5 annotations (& understand Guava Immutable collections, JDK 8 Optionals), with full Spring 4 integration, Swagger for API documentation, Codahale Metrics, an NIO REST client, and simple-React for concurrent programming. No XML configuration necessary. Microserver will autodiscover most of the configuation it needs (Rest end points, Servlets, Filters etc), for everything else we assume, sensible, common defaults which you can change easily if you like.

It is similar in concept to projects such as Dropwizard and Spring Boot (in fact you can optionally use Spring Boot and Microserver together). It differs from those projects in terms of the technologies we integrate, and because it codifies best practices learnt the hard way building high scale microservices.

To launch the same service using Spring Boot (which will introduce some very nifty auto-configuration magic based on the jars on your classpath) you can use the MicrobootApp class e.g.

A brief silent screen cast showing the creation of a Rest Microservice with sample end point.

(The video may start a bit grainy but should improve in quality after a few seconds)

Including Microserver in your projects

To include Microserver in your projects and get started include Microserver as a Maven or Gradle dependency e.g.

Microserver core Maven

<dependency>
<groupId>com.aol.microservices</groupId>
<artifactId>microserver-core</artifactId>
<version>0.5</version>
</dependency>

Microserver core Gradle

compile group: 'com.aol.microservices', name:'microserver-core', version:'0.5'

Optimising Microservices

‘Obligatory’ hipster logo

Microserver exists to make developing Java 8 microservices simple.

Microservices greatly improve developer productivity by allowing developers to focus on new / greenfield development — so long as developers are not repeatedly solving the same integration challenges.

It’s a core goal of Microserver to solve those problems for our chosen tech stack, so engineers using Microserver don’t have to.

We have focused on making the tech stack integration seamless, very testable, immutability is encouraged, facilitating best practices in concurrency / parallelism, API documentation, horizontal scalability, performance and robustness.

Testing Microservices

Doing Robert C. Martin’s clean code course was an eye opener for me, in particular seeing his approach to Test Driven Development. Rather than write tests attached to a class, during the course Robert drove the development by writing tests against the Java API. This meant, that behind the API he was able to refactor at will — none of the applications internals were being exposed to tests.

Microserver makes it possible & simple to write your fast running ‘unit’ tests against your public REST API. If you keep your Services truly ‘micro’, your tests will complete very quickly. Microserver can easily help you mock external REST end points, potentially giving you full end to end testing, with no internal mocked objects. See some example JUnit tests here.


Keeping everything immutable

Microserver includes Google’s Guava library which has a set of excellent Immutable collections. Jersey & Jackson will be automatically configured to serialise and deserialise these. NB to deserialise immutable objects Jackson requires a default constructor be provided. E.g. The entity below is fully serialisable and deserialisable in a Microserver app

That @Builder annotation?

Lombok, a compile time annotation processor that greatly reduces boiler plate in applications, is also included as a dependency. If you wish to use it in your own apps, we recommend you follow their instructions to install it to your IDE. Lombok’s @Wither helps ‘mutate’ immutable objects by creating a new instance — and @Builder makes creating even complex immutable objects a breeze. Implementing the Builder pattern recommended by Josh Block in Effective Java.

Concurrency will matter

Immutability matters because it makes it much simpler to write concurrent applications. If you are adopting a Microservices approach, concurrency matters. This is because by breaking up a monolithic single process into multiple distributed services, you are introducing a potentially wasteful I/O overhead where none existed before. We recommend you use non-blocking concurrency for I/O where scaling is required.

To help you do this, we’ve included AOL’s simple-react library as a dependency, and supply an NIO Rest client tailored to integrate with it.


Capturing application metrics

We’ve integrated Codahale metrics so you can capture application stats just by adding a choice of simple annotations.

We’ve learned the hard way, you need to know where requests come from — so we also bundle up a Servlet filter that will capture and make available incoming IP Addresses.

We also provide a way to capture data about scheduled jobs within your application — what’s running now, and what’s finished recently.

We also allow you to publish metadata about requests, and again see what’s active now and recently finished via REST end points.

Persisting and reading external data

Microservices should be small, very small. In our view that obviates the need for the complex domain models of the past. In fact increasingly we find our Microservices are utilising simple (with an emphasis on simple) key/value NoSQL stores. But some of your services will have to talk to relational dbs, so we’ve made it easy to configure Datasources, JDBC , Spring Data and Hibernate.

There are other goodies in there too, and much more planned. Why not explore our test apps sections on github!

Like what you read? Give John McClean a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.