Dropwizard and Groovy

9,521
-1

Published on

Building REST services with Groovy and Dropwizard for GGX 2013

Published in: Technology

Dropwizard and Groovy

  1. 1. Groovy with Dropwizard for high performance and reliable web services Tomas Lin @tomaslin
  2. 2. ! Goals for this talk • 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.
  3. 3. This is not a talk about REST Services • Everything I tell you about REST will probably be a lie. • Think JSON services over http.
  4. 4. REST Services REST-ful API Design - Ben Hale https://github.com/nebhale/spring-one-2013/tree/master/ rest-ful-api-design Beautiful REST + JSON APIs - Les Hazlewood http://www.slideshare.net/stormpath/rest-jsonapis
  5. 5. whoami • Senior Software Engineer at Netflix • Grails developer since 2008
  6. 6. What is Dropwizard?
  7. 7. http://gunshowcomic.com/316
  8. 8. http://www.dropwizard.io
  9. 9. 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 )
  10. 10. Additional Integrations • Scala • Views ( Freemaker / Mustache ) • Liquibase migrations • Authentication ( http and oAuth2 ) • Dropwizard Spring - https://github.com/ SindicatoSource/dropwizard-spring
  11. 11. Who uses Dropwizard? Fraud Detection & Gift Card Services Fault Tolerant Job Scheduler https://github.com/airbnb/chronos Carl Quinn : Dropwizard + Netflix OSS tools http://tekconf.com/conferences/ codemash-2014/how-we-built-acloud-platform-at-riot-games-u
  12. 12. 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/moderngroovy-enterprise-stack
  13. 13. Dropwizard + Groovy? • Editorial Expansion - Time inc. subsidiary in Mexico 
 Gourmet Awards iPhone app backend.
  14. 14. Why should I try Dropwizard?
  15. 15. Enable Service Oriented Architecture
  16. 16. Advantages • Scale up only some parts of your application. • Isolate services based depending on their security profiles. • Fault tolerance. • Cloud friendly!
  17. 17. vs. —- +
  18. 18. vs. —- +
  19. 19. Dropwizard with Grails at Sky Find and Watch
  20. 20. Performance Dropwizard is a very high performance, low latency framework. http://www.techempower.com/benchmarks/
  21. 21. Other niceties • Testable. Every part of the system can be tested during development. • Deployment friendly - easy to configure, deploy and monitor. • Pure Java.
  22. 22. Getting Started with Dropwizard and Groovy
  23. 23. Lazybones Starter Template • By Kyle Boon from Bloom Health • Available via Lazybones template tool by Peter Ledbrook • You can get Lazybones via gvm
  24. 24. 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
  25. 25. 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
  26. 26. Parts of a Dropwizard Application
  27. 27. Banner src/main/resources/banner.txt
  28. 28. Services • Like an Application in Grails • Central place where all the other building blocks are connected.
  29. 29. 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.
  30. 30. 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
  31. 31. Configurations Same 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
  32. 32. Configurations gradleplugins/src/main/groovy/com/tomaslin/gradleplugins/ GradlePluginsConfiguration.groovy
  33. 33. Configurations
  34. 34. Configurations
  35. 35. Configurations add to .yml file
  36. 36. Configuration • Mapped by Jackson. Uses Hibernate Validator. • @Max / @Min • @NotNull • @Size(min=0, max=50) • @Pattern(regex=, flag=) • @Future / @Past • @NotEmpty • @CreditCardNumber
  37. 37. http://docs.jboss.org/hibernate/validator/4.2/reference/enUS/html_single/#validator-defineconstraints-builtin
  38. 38. Nesting Configurations
  39. 39. vs. Java version
  40. 40. 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.
  41. 41. 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
  42. 42. Resource
  43. 43. Resources • @GET, @POST, @PUT, @DELETE, @HEAD, @OPTIONS, and even @HttpMethod • @Path("/{user}/notifications")
 @PathParam("user") LongParam userId, @QueryParam("count") @DefaultValue("20") IntParam count
  44. 44. Resources
  45. 45. 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/dropwizardwith-spock-and-groov/
  46. 46. Resources
  47. 47. In your IDE
  48. 48. 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.
  49. 49. 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.
  50. 50. Caching
  51. 51. Metrics • Uses Coda Hale’s own Metrics Library • Integrates with JMX, http, Ganglia and Graphite
  52. 52. Metrics @Timed - duration and rate of events @Metered - rate of events @ExceptionMetered - rate of exceptions
  53. 53. Metrics
  54. 54. Kim Betti Dropwizard Dashboard with Vert.x https://github.com/ kimble/ dropwizarddashboard
  55. 55. 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.
  56. 56. Logging • Dropwizard has a pretty robust default logging configuration.
  57. 57. Logging • We can use the @Slf4j annotation in Groovy
  58. 58. Logging
  59. 59. 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.
  60. 60. Client Support • Dropwizard has available both the Apache HttpClient and the Jersey Client. • Jersey Client can use same deserialization available to the server
  61. 61. We can now use the client within the Resource to fetch http data from other sources
  62. 62. Clients • Returns a fully wrapped object that emits metrics. This allows us to monitor how they are used and identify bottlenecks or bugs.
  63. 63. 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.
  64. 64. 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
  65. 65. 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.
  66. 66. 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/
  67. 67. Deployment Make Jars, not Wars One jar file from CI to deploy
  68. 68. Deploying Dropwizard java $JAVA_OPTS -Ddw.http.port=$PORT -Ddw.http.adminPort=$PORT -jar application.jar server prod.yml
  69. 69. 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 Ellis
 http://grails.org/plugin/yammer-metrics
  70. 70. 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/doescompile-static-make-your-website-faster/ ]
  71. 71. Development • A lot faster since there is very little magic and user interface to test. • Excellent support in IntelliJ for Gradle project and tasks.
  72. 72. 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! )
  73. 73. Thank you • Email : tomaslin@gmail.com • Twitter : @tomaslin • Slides will be posted on my blog: • http://bit.ly/tomaslin

×