dropwizard spring - the perfect java rest server stack

22
1 Dropwizard & Spring The perfect Java REST stack Jacek Furmankiewicz Architect @ PROS, Inc.

Post on 21-Oct-2014

14.942 views

Category:

Technology


0 download

DESCRIPTION

An introduction to Dropwizard, as well as examples of how to integrate it with Spring DI and Spring Security. Prepared for a presentation at the Houston Java User Group. Code samples at : https://github.com/jacek99/dropwizard-spring-di-security-onejar-example

TRANSCRIPT

Page 1: Dropwizard Spring - the perfect Java REST server stack

1

Dropwizard & Spring The perfect Java REST stack

Jacek Furmankiewicz

Architect @ PROS, Inc.

Page 2: Dropwizard Spring - the perfect Java REST server stack

2

Welcome to Houston TechFest

Please turn off all electronic devices or set them to vibrate.If you must take a phone call, please do so in the lobby so

as not to disturb others.Thanks to our Diamond Sponsors:

Thank you for being a part of the 7th Annual Houston TechFest!

Page 3: Dropwizard Spring - the perfect Java REST server stack

3

InformationSpeaker presentation slides will be available at www.houstontechfest.org within a week

Don’t forget to complete the Bingo card to be eligible for door prizes

Page 4: Dropwizard Spring - the perfect Java REST server stack

4

Dropwizard

What is it and why is it so exciting?

Dropwizard

4

Page 5: Dropwizard Spring - the perfect Java REST server stack

5

Dropwizard

Created by Coda Hale @ Yammer

http://dropwizard.io

Page 6: Dropwizard Spring - the perfect Java REST server stack

6

Dropwizard

Best-of-breed Java libraries integrated into one embedded application package

● Embedded Jetty (no WAR, no external servlet container)

● JAX-RS (Jersey) for REST APIs

● JSON (Jackson)

● Logging (Logback and SLF4J)

● Hibernate Validators

● Metrics (application SLA and performance)

Page 7: Dropwizard Spring - the perfect Java REST server stack

7

Dropwizard

Embedded Jetty● Restart your code in seconds. Run your code from the IDE.

● No WAR to recompile (ever!)

● No WAR to redeploy (ever!)

● No servlet container (Tomcat, Websphere, Weblogic, etc) to install

● Debug from your IDE (you have a main()), no need to attach to a separate process

● No need to share heap and GC issues with other apps running in the same servlet container

● Total process isolation (one misbehaving WAR (e.g. OutOfMemoryException) cannot bring down the entire server process)

Page 8: Dropwizard Spring - the perfect Java REST server stack

8

Dropwizard

Anatomy of a Dropwizard application

Jetty JerseyYOUR CODE(business logic, DAO, Spring, JPA, etc.)

Page 9: Dropwizard Spring - the perfect Java REST server stack

9

Dropwizard

Multiple apps on the same server

Jetty Jersey APP 1

pid 1843

Jetty Jersey APP 2

pid 1407

Jetty Jersey APP 3

pid 1976

Own JVM4 GB heap

Own JVM4 GB heap

Own JVM4 GB heap

Page 10: Dropwizard Spring - the perfect Java REST server stack

10

Dropwizard

Operations-friendly and ready for deployment

● Opens 2 HTTP ports: one for public APIs (i.e. your REST services), one for admin APIs (e.g. run GC, refresh internal caches, etc)

● Admin port can be closed off on the firewall and inaccessible to outside world

● Health Check APIs to allow easy monitoring from external tools like Nagios

● @Timed annotation on any single REST API allows to track its SLA using Metrics library

Page 11: Dropwizard Spring - the perfect Java REST server stack

11

Dropwizard

Ease of deployment● Dropwizard apps can be easily compiled into a single JAR with all dependencies

(e.g. using Maven Shade or Gradle Shadow plugins)

● Your entire app consists of two files:

YAML config fileJAR application file

● Trivial to run from command line:

java -server myapp.jar server myapp.yml

Page 12: Dropwizard Spring - the perfect Java REST server stack

12

Dropwizard

Ease of deployment – part 2

● Can be wrapped in an RPM or DEB to install on Linux clusters

● Can be registered as a Linux daemon, e.g.

sudo start myapp

sudo stop myapp

Page 13: Dropwizard Spring - the perfect Java REST server stack

13

Dropwizard

Embedding HTML resources

● Dropwizard provides the concept of AssetBundle, which allows to bundle HTML/JS/CSS assets directly in the app (via your src/main/resources folder)

● Perfect for creating a modern HTML5 Javascript UI (Backbone.js, Ember.js, Angular.js, etc) powered by rock-solid super fast Java REST APIs – the best of both worlds

● In 2013, you should not be using any Java (or .Net or Python or Ruby) server-side web frameworks.

HTML5 and Javascript have won and are the future of web development.

Java is best at providing REST APIs to power them with top reliability and performance.

Page 14: Dropwizard Spring - the perfect Java REST server stack

14

Dropwizard and Spring

Dropwizard and Spring DI

Adding dependency injection

Page 15: Dropwizard Spring - the perfect Java REST server stack

15

Dropwizard and Spring

Integrating Spring DI● Start up parent Spring context (AnnotationConfigWebApplicationContext)

● Inject Dropwizard Configuration object into it (so that it is available as a Spring bean)

● Start a child context (with the parent in it), also of type AnnotationConfigWebApplicationContext

● Extract all the Spring beans that Dropwizard cares about from the context and register them within Dropwizard:

– JAX-RS @Path resources and @Provider classes

– Dropwizard Task and HealthCheck objects

● Link Spring with the embedded Jetty instance, so that it behaves just as if it were running in a regular servlet container

Page 16: Dropwizard Spring - the perfect Java REST server stack

16

Dropwizard and Spring

Integrating Spring DI – part 2

Sample app with a full example:

https://github.com/jacek99/dropwizard-spring-di-security-onejar-example

In particular this class has all the low-level details:

MyAppService.java

Page 17: Dropwizard Spring - the perfect Java REST server stack

17

Dropwizard and Spring

Dropwizard and Spring Security

Adding enterprise-grade security

Page 18: Dropwizard Spring - the perfect Java REST server stack

18

Dropwizard and Spring Security

● @ImportResource Spring Security XML config file into your root Spring @Configuration class

● Activate Spring Security filter

● Provides both HTTP and method level (@Secured) support

● Present in github sample app covered previously

Page 19: Dropwizard Spring - the perfect Java REST server stack

19

Dropwizard

Dropwizard user comments

Words from people who use it every day

Page 20: Dropwizard Spring - the perfect Java REST server stack

20

Dropwizard

● Dropwizard is a godsend for all those of us who just need to get stuff DONE.

● Dropwizard changed the rules of the game completely. We have dramatically changed the way of working and increased the speed of whole development process. Dropwizard is being considered as a Restful Application Framework but this is totally unfair and limiting its potential. We have just added a few classes without forking it to operate Spring Framework and it is working wonderfully. That is to say, Dropwizard can be also considered as a "embedded application development framework" and we are using in this way

Page 21: Dropwizard Spring - the perfect Java REST server stack

21

Dropwizard

Q&A

PROSInterested in Big Data and real-time

challenges?We're hiring!

http://www.pros.com/company/working-pros/

Page 22: Dropwizard Spring - the perfect Java REST server stack

22

Please Leave Feedback During Q&AIf you leave session feedback and provide contact information, you will be qualified for a prize

Scan the QR code to the right or go to bit.ly/htf130514