Date post: | 21-Oct-2014 |
Category: |
Technology |
View: | 14,940 times |
Download: | 0 times |
1
Dropwizard & Spring The perfect Java REST stack
Jacek Furmankiewicz
Architect @ PROS, Inc.
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!
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
4
Dropwizard
What is it and why is it so exciting?
Dropwizard
4
5
Dropwizard
Created by Coda Hale @ Yammer
http://dropwizard.io
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)
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)
8
Dropwizard
Anatomy of a Dropwizard application
Jetty JerseyYOUR CODE(business logic, DAO, Spring, JPA, etc.)
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
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
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
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
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.
14
Dropwizard and Spring
Dropwizard and Spring DI
Adding dependency injection
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
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
17
Dropwizard and Spring
Dropwizard and Spring Security
Adding enterprise-grade security
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
19
Dropwizard
Dropwizard user comments
Words from people who use it every day
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
21
Dropwizard
Q&A
PROSInterested in Big Data and real-time
challenges?We're hiring!
http://www.pros.com/company/working-pros/
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