Getting started with NetflixOSS Governator

NetflixOSS Governator is a set of Google Guice extensions to create REST services using Jersey.

Using Governator we can easily configure servers like Jetty and Tomcat in order to build microservices. We also can use set of guice modules to integrated with Archaius and Eureka-Client.

Governator is not opinionated, it's similar to Spring Boot in comparison. However, Governator is configured to work with Guice and not Spring framework.

Governator it's cool because you can define pretty much everything using java code and annotations in a declarative fashion. All code is configured in Guice so we can take benefit of Ioc and Dependency injection and end up creating solution more testable by nature.


Governator Features:
  • Classpath scanning
  • Automatic binding
  • Lifecycle management
  • Configuration of field mapping
  • Field validation
  • Parallelized object warmup

Creating a Simple REST Service with Governator 1.x

First of all, we need to define our dependencies on build.gradle. So let's defined the dependencies.

build.gradle

 

Now we can do the java code -- which is pretty easy. 

JerseyMain.java

So here we have the following. We have a simple REST resource called SimpleResource which will take HTTP request on "/" address. Them we just have to configure Governator / Guice bindings and that's it.

We need to add the ShutdownHookModule in order to have a shutdown port for our server. I'm also overriding the JettyModule in order to change the server port to 9090. Them we need to add the Governator support for Jersey with GovernatorJerseySupportModule. Where we configure the PREFIX of all REST resource, in this case, will be "/*". We also are binding all REST resource with getResourceConfig. That's it we have a working service with we can run with $ ./gradle run or just run as the main class in eclipse for instance.

You can download the code source from my Github here.

Cheers,
Diego Pacheco

Popular posts from this blog

Kafka Streams with Java 15

Rust and Java Interoperability

HMAC in Java