Webservices with Dropwizard and Groovy

E655ab825a4bd2f23b90c85d57c3ee01?s=47 Kyle Boon
February 12, 2013

Webservices with Dropwizard and Groovy

These are the slides from my talk at the Groovy Users of Minnesota group. I gave this talk on Feb 12th 2013 and it describes how we use dropwizard at Bloomhealth.

E655ab825a4bd2f23b90c85d57c3ee01?s=128

Kyle Boon

February 12, 2013
Tweet

Transcript

  1. 3.

    WHAT IS DROPWIZARD Dropwizard is a heavily opinionated framework for

    building web services on the JVM. It is mostly glue around mature java libraries like Jetty, Jersey, Jackson and Guava. “Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality HTTP+JSON web service in the shortest time possible. ”
  2. 4.

    @coda As I've said before, the only reason Dropwizard exists

    at all is to provide opinions on what a service should be. It uses fat JARs because I think they work better. It embeds Jetty because I think that works better. It uses Jackson because I think that works better. It uses Jersey because I think that works better. It has a single YAML configuration file because I think that works better. It wraps Logback because I think that works better. WHO CREATED IT?
  3. 6.

    THE STACK AT BLOOMHEALTH Groovy for programming Grails for web

    applications Dropwizard for JSON web services Gradle for builds Swagger for Service Discovery Spock for testing Gatling for Performace/Load Testing Redis for Caching RabbitMQ for messaging
  4. 7.

    HOW DOES IT WORK? Deployed as a fat jar and

    starts jetty. No need for a container. Start the server from the command line by running: p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) t h r o w s E x c e p t i o n { n e w C o n t a c t s S e r v i c e ( ) . r u n ( a r g s ) } j a v a - j a r c o n t a c t _ d r o p w i z a r d / b u i l d / l i b s / c o n t a c t _ d r o p w i z a r d - s h a d o w - 0 . 1 . 0 - S N A P S H O T . j a r s e r v e r s t a r t d e v _ c o n f i g . y m l
  5. 8.

    THE SERVICE Services are a collection of bundles, commands, healthchecks,

    tasks and resources. The service class defines all of the abilities of your application. @ O v e r r i d e p u b l i c v o i d i n i t i a l i z e ( B o o t s t r a p b o o t s t r a p ) { b o o t s t r a p . n a m e = ' c o n f i g u r a t i o n _ s e r v i c e ' b o o t s t r a p . a d d B u n d l e m i g r a t i o n s B u n d l e b o o t s t r a p . a d d B u n d l e h i b e r n a t e B u n d l e b o o t s t r a p . a d d B u n d l e ( n e w A s s e t s B u n d l e ( ' / s w a g g e r - u i - 1 . 1 . 0 / ' , ' / s w a g g e r ' ) ) b o o t s t r a p . a d d C o m m a n d ( n e w m i g r a t i o n s C o m m a n d ( ) ) } @ O v e r r i d e p u b l i c v o i d r u n ( C o n t a c t s C o n f i g u r a t i o n c o n f i g u r a t i o n , E n v i r o n m e n t e n v i r o n m e n t ) t h r o w s C l a s s N o t F o u n d E x c e p t i o n { C o n t a c t D A O c o n t a c t D A O = n e w C o n t a c t D A O ( h i b e r n a t e B u n d l e . s e s s i o n F a c t o r y ) e n v i r o n m e n t . a d d R e s o u r c e ( n e w C o n t a c t R e s o u r c e ( c o n t a c t D A O ) ) }
  6. 9.

    THE RESOURCE Resources model what is exposed via your RESTful

    API. Dropwizard uses Jersey for this so these classes are mostly jersey annotations. @ P a t h ( ' / c o n t a c t s ' ) @ P r o d u c e s ( M e d i a T y p e . A P P L I C A T I O N _ J S O N ) c l a s s C o n t a c t R e s o u r c e { p r i v a t e f i n a l C o n t a c t D A O c o n t a c t D A O p u b l i c C o n t a c t R e s o u r c e ( C o n t a c t D A O c o n t a c t D A O ) { t h i s . c o n t a c t D A O = c o n t a c t D A O } @ T i m e d ( n a m e = ' c r e a t e C o n t a c t ' ) @ P O S T @ U n i t O f W o r k p u b l i c C o n t a c t c r e a t e C o n t a c t ( @ V a l i d C o n t a c t c o n t a c t ) { r e t u r n c o n t a c t D A O . s a v e O r U p d a t e ( c o n t a c t ) } }
  7. 10.

    THE REPRESENTATION Your POGOs will be turned into JSON via

    Jackson. Hibernate Validator lets you specify validation rules. @ T o S t r i n g @ E q u a l s A n d H a s h C o d e c l a s s C o n t a c t { @ J s o n P r o p e r t y L o n g i d @ N o t N u l l @ N o t E m p t y @ J s o n P r o p e r t y S t r i n g f i r s t N a m e @ N o t N u l l @ N o t E m p t y @ J s o n P r o p e r t y S t r i n g l a s t N a m e @ T r a n s i e n t @ J s o n I g n o r e
  8. 11.

    METRICS Yammer Metrics is built in and provides metrics over

    an administration port (8081). Resources can be annotated with @ T i m e d or @ M e t e r e d , and @ E x c e p t i o n M e t e r e d . Dropwizard augments Jersey to automatically record runtime information about your resource methods.
  9. 12.

    HEALTH CHECKS Health Checks are a method to make sure

    the infrastructure your service depends on are all running. They are accessible on the administration port. p u b l i c c l a s s M y S Q L H e a l t h C h e c k e x t e n d s H e a l t h C h e c k { p r i v a t e f i n a l S e s s i o n F a c t o r y s e s s i o n F a c t o r y p u b l i c M y S Q L H e a l t h C h e c k ( S e s s i o n F a c t o r y s e s s i o n F a c t o r y ) { s u p e r ( " M y S Q L " ) t h i s . s e s s i o n F a c t o r y = s e s s i o n F a c t o r y } @ O v e r r i d e p r o t e c t e d c o m . y a m m e r . m e t r i c s . c o r e . H e a l t h C h e c k . R e s u l t c h e c k ( ) { i f ( ! s e s s i o n F a c t o r y . c l o s e d ) { r e t u r n h e a l t h y ( ) } e l s e { r e t u r n u n h e a l t h y ( ' S e s s i o n F a c t o r y i s C l o s e d ! ' ) } } }
  10. 13.
  11. 14.

    BUNDLES Bundles are reusable blocks of behaviour designed to be

    reused across services. Assets, Hibernate and Liquibase are all implemented as Dropwizard Bundles.
  12. 15.

    COMMANDS Commands add options to the command line interface of

    your service. For example the server starts based on the 'server' command. Migrations run based on the 'db migrate' command. You might add your own command for running functional tests or seeding the database.
  13. 16.

    TASKS Tasks are run time actions available over the administration

    port. Dropwizard ships with a garbage collection task. You might want to right a task to clean a cache by key.
  14. 19.

    REFERENCES Dropwizard User Guide Dropwizard User Group https://github.com/codahale/dropwizard Presentation about

    Dropwizard @ Yammer Presentation about Dropwizard @ Simple Coda Hale and Metrics Coda Hale and the Programming Ape
  15. 20.

    CODE WE'VE LOOKED AT Example Contact Application Swagger Enhanced Groovy

    Doc swagger-jaxrs-doclet dropwizard dashboard
  16. 21.

    THANKS A special thanks to the people who actually figured

    all this stuff out. Including but not limited to: Chad Small Sairam Rekapalli Ryley Gahagan Charlie Knudsen John Engelman