Social Media

Tag Archives for " dropwizard "

Dropwizard Microservices

I had read about Dropwizard when I started using Spring Boot, but decided to stick with Spring as I was more familiar with Spring. Then I read Building Microservices book, by Sam Reilly, and he recommended it 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 a collection of what it considers 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 is –

It also includes metrics, Logback and slf4j, Liquidbase, Freemarker and Mustache and Joda Time

The final product is a single big jar

Sample Project

dropwizard recommends maven so I’ll stick with that. Not much to our basic pom – just a dropwizard dependency –

[sourcecode lang=”xml”] <dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.9.2</version>
</dependency>
[/sourcecode]

We then use the shade plugin to create our big jar –
[sourcecode lang=”xml”] <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>
[/sourcecode]

The core of the application is the “Application” class –
[sourcecode lang=”java”] 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.healthChecks().register("TodaysDateHC", new TodaysDateHealthCheck());
}
}
[/sourcecode]

The key points are –

  • The main method starts an instance of the jetty 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 –

[sourcecode lang=”java”] public class TodaysDateConfiguration extends Configuration {
}
[/sourcecode]

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 –

[sourcecode lang=”java”] public class TodaysDateHealthCheck extends HealthCheck {
@Override
protected Result check() throws Exception {
return Result.healthy();
}
}
[/sourcecode]

Finally we get to the core of the application –

[sourcecode lang=”java”] @Path("/todaysdate")
@Produces(MediaType.APPLICATION_JSON)
public class TodaysDateResource {
@GET
public String todaysDate() {
return "{\"todaysdate\": " + new DateTime().toString("dd/MM/yyyy") + "}";
}
}
[/sourcecode]

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 –

[sourcecode] logging:
level: INFO

server:
applicationConnectors:
– type: http
port: 8080
adminConnectors:
– type: http
port: 8001
[/sourcecode]

The obvious question is what is, and why YML. Im told its human-friendly configuration, and to be honest it doesnt bother me whether config is xml, properties or whatever the framework is using as long as I can get the information I need. In this example I’ve included some config to allow you to change 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

[sourcecode lang=”json”]{"todaysdate": 12/04/2016 }[/sourcecode]

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

[sourcecode lang=”json”] {
"TodaysDateHC" : {
"healthy" : true
},
"deadlocks" : {
"healthy" : true
}
}[/sourcecode]

Conclusions

This example barely scrapes the surface of dropwizard, but half the battle of coding is to get a HelloWorld application to compile. Its also too soon to provide a comparison between Spring Boot and dropwizard just yet.

Source Code

dropwizard-microservices