Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Starting Dropwizard Microservices

DZone's Guide to

Starting Dropwizard Microservices

I read about Dropwizard when I started using Spring Boot, but decided to stick with Spring as I am more familiar with it. When I read Building Microservices by Sam Newman, I thought I should look at Dropwizard again and learn more about it.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

I read about Dropwizard when I started using Spring Boot, but decided to stick with Spring as I am more familiar with Spring. Then I read the book Building Microservices by Sam Newman, and he recommended Dropwizard for microservices development. So I thought I should look at Dropwizard again, and learn more about it.

What Is Dropwizard?

Dropwizard provided the inspiration for spring boot. It is not a framework, but a collection of best-of-breed libraries and provides:

...out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more...

At its core are:

It also includes metrics, Logback, and slf4jLiquidbaseFreemarker and Mustache; and Joda Time.

The final product is a single big JAR that can be run standalone.

Sample Project

The build system for Dropwizard is Maven. I don't have strong feelings on this, and maven is fine for me.

The basic .pom has just a Dropwizard dependency:

<dependency>
   <groupId>io.dropwizard</groupId>
   <artifactId>dropwizard-core</artifactId>
   <version>0.9.2</version>
</dependency>


It also uses the shade plugin to generate the big JAR:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <configuration>
      <createDependencyReducedPom>true</createDependencyReducedPom>
      <filters>
         <filter>
            <artifact>*:*</artifact>
            <excludes>
               <exclude>META-INF/*.SF</exclude>
               <exclude>META-INF/*.DSA</exclude>
               <exclude>META-INF/*.RSA</exclude>
            </excludes>
         </filter>
      </filters>
      <transformers>
         <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.glenware.dropwizard.TodaysDateApplication</mainClass>
         </transformer>
      </transformers>
   </configuration>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
      </execution>
   </executions>
</plugin>


The core of the application is the "Application" class:

public class TodaysDateApplication extends Application<TodaysDateConfiguration> {

   public static void main(String[] args) throws Exception {
      new TodaysDateApplication().run(args);
   }

   @Override
   public String getName() {
      return "TodaysDate-microservice";
   }

   @Override
   public void initialize(Bootstrap<TodaysDateConfiguration> bootstrap) {
   }

   @Override
   public void run(TodaysDateConfiguration configuration, Environment environment) throws Exception {
      environment.jersey().register(new TodaysDateResource());
      environment.HealthCheck().register("TodaysDateHC", new TodaysDateHealthCheck());
   }

}


The key points are:

  • The main method starts an instance of the Jersey server
  • The run method then sets up two parameters
  • REST server: TodaysDateResource
  • HealthCheck class, TodaysDateHealthCheck (this isn't necessary but you do get a warning on startup if you don't set one)

The configuration class itself is just a placeholder:

public class TodaysDateConfiguration extends Configuration {
}


We can add our own configuration here to accept parameters from the .yml config file.

The HealthCheck class is also quite basic, and just increments a count on the number of times the service is called:

public class TodaysDateHealthCheck extends HealthCheck {

   @Override
   protected Result check() throws Exception {
      return Result.healthy();
   }

}


In a real example, we could use this to monitor key resources and feed it back based on whether external connections or services are active.

Finally, we get to the core of the application:

@Path("/todaysdate")
@Produces(MediaType.APPLICATION_JSON)
public class TodaysDateResource {

   @GET
   public String todaysDate() {
      return "{\"todaysdate\": " + new DateTime().toString("dd/MM/yyyy") + "}";
   }

}


This is simply a Jersey REST application:

  • Serves on path /todaysdate, and returns a JSON message like - { "todaysdate" : "12/04/2016" }  

There is one additional configuration file—config.yml:

logging:
  level: INFO

server:
  applicationConnectors:
  - type: http 
    port: 8080
  adminConnectors:
  - type: http
    port: 8001


YML is an extension of JSON, and is designed to be human readable. I hadn't used it before, but it's just another configuration format and easy to understand. I added some additional information to let you change server ports.

Putting it all together

mvn clean package
java -jar target/dropwizard-dates-microservice-0.0.1-SNAPSHOT.jar server src/main/resources/server-config.yml


You can then access the service through your browser at:

http://localhost:8080/todaysdate

The output is the date in UK format:

{"todaysdate": 12/04/2016 }


The next feature is HealthCheck:

http://localhost:8081/healthcheck?pretty=true

{
   "TodaysDateHC" : 
   {
      "healthy" : true
   },
   "deadlocks" : 
   {
      "healthy" : true
   }
}

Conclusion

It's too soon to draw meaningful conclusions about Dropwizard, beyond concluding that a basic HelloWorld style microservice is fairly easy to implement. That said it, has a number of features I like: single JAR, standalone server, and the HealthCheck functionality. The next step will be to implement a more complete microservice example, and comparison against Spring Boot.

Source Code

dropwizard-microservices

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
java ,microservices ,dropwizard ,spring boot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}