dropwizard and groovy

101
Groovy with Dropwizard for high performance and reliable web services Tomas Lin @tomaslin

Upload: tomaslin

Post on 10-May-2015

8.297 views

Category:

Technology


0 download

DESCRIPTION

Building REST services with Groovy and Dropwizard for GGX 2013

TRANSCRIPT

Page 1: Dropwizard and Groovy

Groovy with Dropwizard

for high performance and reliable web

services

Tomas Lin @tomaslin

Page 2: Dropwizard and Groovy

!

• Quick tour of Dropwizard functionality written with Groovy & friends instead of pure Java.

• Showcase some of the “DevOps” friendly features in Dropwizard for easy setup, deployment and monitoring.

Goals for this talk

Page 3: Dropwizard and Groovy

This is not a talk about REST Services

• Everything I tell you about REST will probably be a lie.

• Think JSON services over http.

Page 4: Dropwizard and Groovy

REST Services

https://github.com/nebhale/spring-one-2013/tree/master/rest-ful-api-design

http://www.slideshare.net/stormpath/rest-jsonapisBeautiful REST + JSON APIs - Les Hazlewood

REST-ful API Design - Ben Hale

Page 5: Dropwizard and Groovy

whoami• Senior Software Engineer at Netflix

• Grails developer since 2008

Page 6: Dropwizard and Groovy

What is Dropwizard?

Page 7: Dropwizard and Groovy

http://gunshowcomic.com/316

Page 8: Dropwizard and Groovy

http://www.dropwizard.io

Page 9: Dropwizard and Groovy

Dropwizard stack• Takes mature, well known Java frameworks

and glues them together.

• Jetty for HTTP

• Jersey for REST

• Jackson for JSON

• Metrics for metrics

• Etc. ( Logback, Hibernate, Liquibase, etc )

Page 10: Dropwizard and Groovy

Additional Integrations• Scala

• Views ( Freemaker / Mustache )

• Liquibase migrations

• Authentication ( http and oAuth2 )

• Dropwizard Spring - https://github.com/SindicatoSource/dropwizard-spring

Page 11: Dropwizard and Groovy

Who uses Dropwizard?

Carl Quinn : Dropwizard + Netflix OSS tools http://tekconf.com/conferences/codemash-2014/how-we-built-a-cloud-platform-at-riot-games-u

Fault Tolerant Job Scheduler https://github.com/airbnb/chronos

Fraud Detection & Gift Card Services

Page 12: Dropwizard and Groovy

Dropwizard + Groovy?

• Bloom Health - Insurance carriers data exchange. Spring Batch, Dropwizard and Groovy. (25x faster than previous system)

• Sky Find and Watch - Powers the remote record / watch now functionality

• UnderwriteMe - Watch Marcin’s talk! http://skillsmatter.com/podcast/home/modern-groovy-enterprise-stack

Page 13: Dropwizard and Groovy

• Editorial Expansion - Time inc. subsidiary in Mexico Gourmet Awards iPhone app backend.

Dropwizard + Groovy?

Page 14: Dropwizard and Groovy

Why should I try Dropwizard?

Page 15: Dropwizard and Groovy

Enable Service Oriented Architecture

Page 16: Dropwizard and Groovy
Page 17: Dropwizard and Groovy
Page 18: Dropwizard and Groovy
Page 19: Dropwizard and Groovy
Page 20: Dropwizard and Groovy
Page 21: Dropwizard and Groovy

Advantages• Scale up only some parts of your

application.

• Isolate services based depending on their security profiles.

• Fault tolerance.

• Cloud friendly!

Page 22: Dropwizard and Groovy

vs.—-

+

Page 23: Dropwizard and Groovy

vs.—-

+

Page 24: Dropwizard and Groovy

Dropwizard with Grails at Sky Find and Watch

Page 25: Dropwizard and Groovy

Performance

http://www.techempower.com/benchmarks/

Dropwizard is a very high performance, low latency framework.

Page 26: Dropwizard and Groovy
Page 27: Dropwizard and Groovy
Page 28: Dropwizard and Groovy

Other niceties• Testable. Every part of the system can be

tested during development.

• Deployment friendly - easy to configure, deploy and monitor.

• Pure Java.

Page 29: Dropwizard and Groovy

Getting Started with Dropwizard

and Groovy

Page 30: Dropwizard and Groovy

Lazybones Starter Template

• By Kyle Boon from Bloom Health

• Available via Lazybones template tool by Peter Ledbrook

• You can get Lazybones via gvm

Page 31: Dropwizard and Groovy

Lazybones Starter Template

• Gradle build

• Spock tests

• Hibernate persistance layer

• Fat Jars via Shadow ( like Maven’s Shade )

• CodeNarc for clean code

• Cobertura for test coverage

Page 32: Dropwizard and Groovy
Page 33: Dropwizard and Groovy

Tasks• Test the application ./gradlew test

• Build a Jar file for deploy ./gradlew shadow

• Drop a database ./gradlew dropAll

• Setup database ./gradlew migrate

• You can add your own like deployToCloud

Page 34: Dropwizard and Groovy

Parts of a Dropwizard Application

Page 35: Dropwizard and Groovy

Banner

src/main/resources/banner.txt

Page 36: Dropwizard and Groovy
Page 37: Dropwizard and Groovy

Services• Like an Application in Grails

• Central place where all the other building blocks are connected.

Page 38: Dropwizard and Groovy
Page 39: Dropwizard and Groovy
Page 40: Dropwizard and Groovy

Services• The Dropwizard approach to services lets

us see one place where all our other components are bound and glued together.

• Relationships between other parts must be explicitly declared. There is no magic linking.

Page 41: Dropwizard and Groovy

Configurations • Each service has a configuration that is

passed into the run method.

• Dropwizard has default configurations for clients, logging and Jetty that can be overwritten

Page 42: Dropwizard and Groovy

ConfigurationsSame jar file, configuration is externalized.

!

java -jar application.jar server test.yml java -jar application.jar server stage.yml java -jar application.jar server prod.yml

Page 43: Dropwizard and Groovy

gradleplugins/src/main/groovy/com/tomaslin/gradleplugins/ GradlePluginsConfiguration.groovy

Configurations

Page 44: Dropwizard and Groovy

Configurations

Page 45: Dropwizard and Groovy

Configurations

Page 46: Dropwizard and Groovy

Configurations add to .yml file

Page 47: Dropwizard and Groovy

Configuration• Mapped by Jackson. Uses Hibernate Validator.

• @Max / @Min

• @NotNull

• @Size(min=0, max=50)

• @Pattern(regex=, flag=)

• @Future / @Past

• @NotEmpty

• @CreditCardNumber

Page 48: Dropwizard and Groovy

http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#validator-defineconstraints-builtin

Page 49: Dropwizard and Groovy

Nesting Configurations

Page 50: Dropwizard and Groovy
Page 51: Dropwizard and Groovy

vs. Java version

Page 52: Dropwizard and Groovy

Configurations • Configurations are vital because they allow

us to make sure we got all the details for our service right.

• One config file instead of merged conflict from many sources eliminates confusion and makes it easy to automate / swap out.

Page 53: Dropwizard and Groovy

Resources • Represent a set of service endpoints

• Like Controllers in Grails

• They are just Jersey Resources: https://jersey.java.net/nonav/documentation/latest/user-guide.html#jaxrs-resources

Page 54: Dropwizard and Groovy

Resource

Page 55: Dropwizard and Groovy

Resources• @GET, @POST, @PUT, @DELETE,

@HEAD, @OPTIONS, and even @HttpMethod

• @Path("/{user}/notifications") @PathParam("user") LongParam userId, @QueryParam("count") @DefaultValue("20") IntParam count

Page 56: Dropwizard and Groovy

Resources

Page 57: Dropwizard and Groovy

Resources• Tested not as Unit mocks, but as Jersey

components with a real Jersey server.

• Similar to the FakeServer in Grails Rest Client

• Dropwizard comes with a ResourceTest that works with Mockito / JUnit

• I wrote a Spock equivalent available at http://fbflex.wordpress.com/2013/01/30/dropwizard-with-spock-and-groov/

Page 58: Dropwizard and Groovy
Page 59: Dropwizard and Groovy

Resources

Page 60: Dropwizard and Groovy

In your IDE

Page 61: Dropwizard and Groovy
Page 62: Dropwizard and Groovy

Representations • Lets you formally describe the data flowing

in and out of your REST API.

• Data Transfer Objects that can be validated.

• It gives you a way to easily map them into your database objects either directly or via a DAO.

• Recommended approach is to share representations between servers and clients.

Page 63: Dropwizard and Groovy
Page 64: Dropwizard and Groovy
Page 65: Dropwizard and Groovy
Page 66: Dropwizard and Groovy
Page 67: Dropwizard and Groovy
Page 68: Dropwizard and Groovy
Page 69: Dropwizard and Groovy
Page 70: Dropwizard and Groovy

Representations• The use of representations ensure the

contract between our REST endpoints and clients that consume this endpoint.

• You could also just skip this and go straight to your other endpoints or JSON friendly database.

Page 71: Dropwizard and Groovy

Caching

Page 72: Dropwizard and Groovy

Metrics• Uses Coda Hale’s own Metrics Library

• Integrates with JMX, http, Ganglia and Graphite

Page 73: Dropwizard and Groovy

Metrics@Timed - duration and rate of events @Metered - rate of events @ExceptionMetered - rate of exceptions

Page 74: Dropwizard and Groovy

Metrics

Page 75: Dropwizard and Groovy

Kim Betti Dropwizard

Dashboard with Vert.x

https://github.com/kimble/

dropwizard-dashboard

Page 76: Dropwizard and Groovy

Metrics• They are first class citizens within

Dropwizard.

• Very flexible. You can define custom metrics, set Gauges, etc.

• It’s about communicating the actual state of your server back to the mothership so it can be coordinated.

Page 77: Dropwizard and Groovy

Logging• Dropwizard has a pretty robust default

logging configuration.

Page 78: Dropwizard and Groovy

Logging• We can use the @Slf4j annotation in Groovy

Page 79: Dropwizard and Groovy

Logging

Page 80: Dropwizard and Groovy

Logging• I am lazy.

• Don’t realize logs are needed until it is too late.

• Logs are time machines to past malfunctions, making them easy to set up allows us to use them.

Page 81: Dropwizard and Groovy

Client Support

• Dropwizard has available both the Apache HttpClient and the Jersey Client.

• Jersey Client can use same deserialization available to the server

Page 82: Dropwizard and Groovy
Page 83: Dropwizard and Groovy

We can now use the client within the Resource to fetch http data from other sources

Page 84: Dropwizard and Groovy

Clients• Returns a fully wrapped object that emits

metrics. This allows us to monitor how they are used and identify bottlenecks or bugs.

Page 85: Dropwizard and Groovy

Managed Objects• Classes that have a start and end phase

attached to the service.

• Often have their own configurations.

• Easy way to encapsulate integrations into other services / systems.

Page 86: Dropwizard and Groovy
Page 87: Dropwizard and Groovy
Page 88: Dropwizard and Groovy

Health Checks• Runtime checks that the service is

operating correctly

• If an exception is thrown, this is shown on the health monitoring screen

• Also throws a 500 error code on the health check page for tools

Page 89: Dropwizard and Groovy
Page 90: Dropwizard and Groovy
Page 91: Dropwizard and Groovy
Page 92: Dropwizard and Groovy
Page 93: Dropwizard and Groovy

Health Checks• Quick diagnostics on running instance.

• Better to kill a sick service than to keep it running and potentially corrupting your data.

• Also helps when you are deploying and building out your infrastructure.

Page 94: Dropwizard and Groovy

Other Features• Tasks - Like Grails Scripts

• Commands - Jobs that can be invoked via an URL. Lots of power since you’re using the same stack as your runtime service.

• Others - Filters, Bundles, etc.

http://www.dropwizard.io/manual/

Page 95: Dropwizard and Groovy

Deployment

Make Jars, not Wars

One jar file from CI to deploy

Page 96: Dropwizard and Groovy

Deploying Dropwizard java $JAVA_OPTS -Ddw.http.port=$PORT

-Ddw.http.adminPort=$PORT -jar application.jar server prod.yml

Page 97: Dropwizard and Groovy

Grails Equivalents• Dropwizard Plugin by Burt Beckwith

http://grails.org/plugin/dropwizard

• Health Control Plugin by Kim Betti http://grails.org/plugin/health-control

• Validate Config Plugin by Andy Miller http://grails.org/plugin/validate-config

• Metrics Plugin by Jeff Ellishttp://grails.org/plugin/yammer-metrics

Page 98: Dropwizard and Groovy

Using Groovy• Less verbose objects. Can use map

constructors.

• Nice annotations for logging.

• Gradle / Spock / Betamax / Lazybones.

• Not sure if @CompileStatic has any changes that are significant - 6%[ http://kyleboon.org/blog/2013/09/26/does-compile-static-make-your-website-faster/ ]

Page 99: Dropwizard and Groovy

Development• A lot faster since there is very little magic

and user interface to test.

• Excellent support in IntelliJ for Gradle project and tasks.

Page 100: Dropwizard and Groovy

Final Thoughts• Think about the deployed application.

• Grails + Dropwizard. Not Grails or Dropwizard.

• This is just mini-scale. What happens when you have 100 services/servers? 1,000? 1,000,000? ( Come work at Netflix! )

Page 101: Dropwizard and Groovy

Thank you

• Email : [email protected]

• Twitter : @tomaslin

• Slides will be posted on my blog:

• http://bit.ly/tomaslin