Dropwizard 
-微服務架構框架- 
anthonychen
Once Upon a Time 
Hibernate 
Tomcat 
Spring 
JPA 
Spring 
MVC 
Jackson 
Freemarker 
or 
Thymeleaf 
Logback
Dropwizard 
Full Stack Framework 
with microservice architecture
Drop...wizard?
Jackson 
mustache 
JDBI 
Full Stack, RESTful and Open Source
Dropwizard Modules (v0.7.1) 
dropwizard-assets dropwizard-jackson dropwizard-migrations 
dropwizard-auth dropwizard-jdbi dropwizard-servlets 
dropwizard-client dropwizard-jersey dropwizard-spdy 
dropwizard-configuration dropwizard-jetty dropwizard-testing 
dropwizard-core dropwizard-lifecycle dropwizard-util 
dropwizard-db dropwizard-logging dropwizard-validation 
dropwizard-example dropwizard-metrics-ganglial dropwizard-views-freemarker 
dropwizard-forms dropwizard-metrics-graphite dropwizard-views-mustache 
dropwizard-hibernate dropwizard-metrics dropwizard-views
Microservice 
Architecture 
The microservice architectural 
style is an approach to 
developing a single application as 
a suite of small services, each 
running in its own process and 
communicating with lightweight 
mechanisms, often an HTTP 
resource API. These services are 
built around business capabilities 
and independently deployable by 
fully automated deployment 
machinery. 
- Martin Fowler
Monolithic Micro Services 
pid 1234 8GB heap 
pid 5678 
2GB heap 
pid 5978 
2GB heap 
pid 1234 
2GB heap 
pid 9527 
2GB heap
Scalability
Project Management
P 
R 
O 
X 
Y 
Extensibility
Phlyty 
a microframework using ZF2 components. 
SleepyA RESTful framework for Go 
Microservice Frameworks
Why 
DropWizard?
http://www.oracle.com/us/technologies/java/gimmejava/usage-terms/index.html
Dropwizard - Pros 
Productivity – Do one thing at a time 
Do your best with what you have 
Simple & Lightweight 
Easy Test, Deployment and Management 
Performance
Dropwizard - Pros 
Productivity – Do one thing at a time 
Do your best with what you have 
Simple & Lightweight 
Easy Test, Deployment and Management 
Performance
http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=json&f=311c-1hq8-0-0
http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=db&f=311c-1hq8-0-0
http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=query&f=311c-1hq8-0-0
Dropwizard - Cons 
Frequently and Large-Scale Changes 
Nano-Service Architecture 
Complexity at Infrastructure Level 
No Application Server 
Put More Effort on Monitoring and
How to Start 
DropWizard?
Overview 
Resource 
Application Bundle 
Configuration 
Representation 
Environment 
Views
Project Orginization 
com.example.myapplication: 
 api: Representations. 
 cli: Commands 
 client: Client implementation for your application 
 core: Domain implementation 
 jdbi: Database access classes 
 health: Health Checks 
 resources: Resources 
 MyApplication: The application class 
 MyApplicationConfiguration: configuration class
Configuration Class 
public class BlogConfiguration extends Configuration { 
@Valid 
@NotNull 
@JsonProperty("database") 
private DataSourceFactory database = new DataSourceFactory(); 
public DataSourceFactory getDatabase() { 
return database; 
}}
Configuration File 
(YAML) 
server: 
type: simple 
applicationContextPath: /application 
adminContextPath: /admin 
database: 
driverClass: com.mysql.jdbc.Driver 
user: anthonychen 
password: anthonychen 
url: jdbc:mysql://localhost:3306/blog 
validationQuery: "/* MyApplication Health Check */ SELECT 1"
Representation Class 
public class Notification { 
private String text; 
public Notification(String text) { 
this.text = text; 
} 
@JsonProperty 
public String getText() { 
return text; 
} 
@JsonProperty 
public void setText(String text) { 
this.text = text; 
}}
Application Class - Bundle 
public class BlogApplication extends Application<BlogConfiguration> { 
public static void main(String[] args) throws Exception { 
new BlogApplication().run(args); 
} 
@Override 
public void initialize(Bootstrap<BlogConfiguration> bootstrap) { 
bootstrap.addBundle(hibernateBundle); 
bootstrap.addBundle(new ViewBundle()); 
bootstrap.addBundle(new AssetsBundle("/assets/js", "/js", null, "js")); 
}}
Application Class – Environment 
@Override 
public void run(BlogConfiguration configuration, Environment environment) throws Exception { 
// Crete DAOs 
final ArticleDAO articleDAO = new ArticleDAO(hibernateBundle.getSessionFactory()); 
final UserDAO userDAO = new UserDAO(hibernateBundle.getSessionFactory()); 
// Create healthchecks 
final SessionFactoryHealthCheck dbHealthCheck = new SessionFactoryHealthCheck( 
hibernateBundle.getSessionFactory(), configuration.getDatabase().getValidationQuery() 
); 
// Register resources, filters and healthchecks 
environment.jersey().register(new BlogResource(configuration.getSiteName(), articleDAO)); 
environment.jersey().register(new ArticleResource(articleDAO, userDAO)); 
environment.healthChecks().register("databaseHealthcheck", dbHealthCheck); 
}}
View Layer 
Freemarker - http://freemarker.org 
Mustache - http://mustache.github.io
Health Check 
public class DatabaseHealthCheck extends HealthCheck { 
private final Database database; 
public DatabaseHealthCheck(Database database) { 
this.database = database; 
} 
@Override 
protected Result check() throws Exception { 
if (database.isConnected()) { 
return Result.healthy(); 
} else { 
return Result.unhealthy("Cannot connect to " + database.getUrl()); 
} 
} 
}
Demo- 
A Sample Blog
Build - A Fat JAR 
An easy maintainable, single deployable 
artifact 
maven-shade or maven-assembly-plugin
Run 
Command line: 
java -jar blog-sample-0.0.1-SNAPSHOT.jar server blog.yml 
exec-maven-plugin
More 
DropWizard?
Metric
AngularJS & 
DropWizard
Spring & 
DropWizard

Dropwizard Introduction

  • 1.
  • 2.
    Once Upon aTime Hibernate Tomcat Spring JPA Spring MVC Jackson Freemarker or Thymeleaf Logback
  • 3.
    Dropwizard Full StackFramework with microservice architecture
  • 4.
  • 6.
    Jackson mustache JDBI Full Stack, RESTful and Open Source
  • 7.
    Dropwizard Modules (v0.7.1) dropwizard-assets dropwizard-jackson dropwizard-migrations dropwizard-auth dropwizard-jdbi dropwizard-servlets dropwizard-client dropwizard-jersey dropwizard-spdy dropwizard-configuration dropwizard-jetty dropwizard-testing dropwizard-core dropwizard-lifecycle dropwizard-util dropwizard-db dropwizard-logging dropwizard-validation dropwizard-example dropwizard-metrics-ganglial dropwizard-views-freemarker dropwizard-forms dropwizard-metrics-graphite dropwizard-views-mustache dropwizard-hibernate dropwizard-metrics dropwizard-views
  • 8.
    Microservice Architecture Themicroservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. - Martin Fowler
  • 9.
    Monolithic Micro Services pid 1234 8GB heap pid 5678 2GB heap pid 5978 2GB heap pid 1234 2GB heap pid 9527 2GB heap
  • 10.
  • 11.
  • 12.
    P R O X Y Extensibility
  • 13.
    Phlyty a microframeworkusing ZF2 components. SleepyA RESTful framework for Go Microservice Frameworks
  • 14.
  • 15.
  • 16.
    Dropwizard - Pros Productivity – Do one thing at a time Do your best with what you have Simple & Lightweight Easy Test, Deployment and Management Performance
  • 17.
    Dropwizard - Pros Productivity – Do one thing at a time Do your best with what you have Simple & Lightweight Easy Test, Deployment and Management Performance
  • 18.
  • 19.
  • 20.
  • 21.
    Dropwizard - Cons Frequently and Large-Scale Changes Nano-Service Architecture Complexity at Infrastructure Level No Application Server Put More Effort on Monitoring and
  • 22.
    How to Start DropWizard?
  • 23.
    Overview Resource ApplicationBundle Configuration Representation Environment Views
  • 24.
    Project Orginization com.example.myapplication:  api: Representations.  cli: Commands  client: Client implementation for your application  core: Domain implementation  jdbi: Database access classes  health: Health Checks  resources: Resources  MyApplication: The application class  MyApplicationConfiguration: configuration class
  • 25.
    Configuration Class publicclass BlogConfiguration extends Configuration { @Valid @NotNull @JsonProperty("database") private DataSourceFactory database = new DataSourceFactory(); public DataSourceFactory getDatabase() { return database; }}
  • 26.
    Configuration File (YAML) server: type: simple applicationContextPath: /application adminContextPath: /admin database: driverClass: com.mysql.jdbc.Driver user: anthonychen password: anthonychen url: jdbc:mysql://localhost:3306/blog validationQuery: "/* MyApplication Health Check */ SELECT 1"
  • 27.
    Representation Class publicclass Notification { private String text; public Notification(String text) { this.text = text; } @JsonProperty public String getText() { return text; } @JsonProperty public void setText(String text) { this.text = text; }}
  • 28.
    Application Class -Bundle public class BlogApplication extends Application<BlogConfiguration> { public static void main(String[] args) throws Exception { new BlogApplication().run(args); } @Override public void initialize(Bootstrap<BlogConfiguration> bootstrap) { bootstrap.addBundle(hibernateBundle); bootstrap.addBundle(new ViewBundle()); bootstrap.addBundle(new AssetsBundle("/assets/js", "/js", null, "js")); }}
  • 29.
    Application Class –Environment @Override public void run(BlogConfiguration configuration, Environment environment) throws Exception { // Crete DAOs final ArticleDAO articleDAO = new ArticleDAO(hibernateBundle.getSessionFactory()); final UserDAO userDAO = new UserDAO(hibernateBundle.getSessionFactory()); // Create healthchecks final SessionFactoryHealthCheck dbHealthCheck = new SessionFactoryHealthCheck( hibernateBundle.getSessionFactory(), configuration.getDatabase().getValidationQuery() ); // Register resources, filters and healthchecks environment.jersey().register(new BlogResource(configuration.getSiteName(), articleDAO)); environment.jersey().register(new ArticleResource(articleDAO, userDAO)); environment.healthChecks().register("databaseHealthcheck", dbHealthCheck); }}
  • 30.
    View Layer Freemarker- http://freemarker.org Mustache - http://mustache.github.io
  • 31.
    Health Check publicclass DatabaseHealthCheck extends HealthCheck { private final Database database; public DatabaseHealthCheck(Database database) { this.database = database; } @Override protected Result check() throws Exception { if (database.isConnected()) { return Result.healthy(); } else { return Result.unhealthy("Cannot connect to " + database.getUrl()); } } }
  • 32.
  • 33.
    Build - AFat JAR An easy maintainable, single deployable artifact maven-shade or maven-assembly-plugin
  • 34.
    Run Command line: java -jar blog-sample-0.0.1-SNAPSHOT.jar server blog.yml exec-maven-plugin
  • 35.
  • 36.
  • 37.
  • 38.

Editor's Notes

  • #17 單一應用程式可以拆解為數個小型服務,以符合各自的需求,也容易了解、修改與維護 與舊系統整合,老酒裝新瓶,無須大幅翻新 可以針對負擔較重的服務特別擴充其資源 可以只佈署個別服務,而不用每次均佈署整個應用程式 每個服務無須使用相同的程式語言 --> 殺雞焉用牛刀 每個服務都可以有自己的小開發團隊  強迫程式設計師面對佈署與了解服務整合需求 收斂程式語言與框架的多樣性
  • #18 單一應用程式可以拆解為數個小型服務,以符合各自的需求,也容易了解、修改與維護 與舊系統整合,老酒裝新瓶,無須大幅翻新 可以針對負擔較重的服務特別擴充其資源 可以只佈署個別服務,而不用每次均佈署整個應用程式 每個服務無須使用相同的程式語言 --> 殺雞焉用牛刀 每個服務都可以有自己的小開發團隊  強迫程式設計師面對佈署與了解服務整合需求 收斂程式語言與框架的多樣性
  • #22 避免變成奈米服務架構 nano-service architecture 服務間變得更鬆散,依賴性降低,程式庫的版本會更難管裡 (如果要管理的話) 程式語言平台環境可能變得很複雜
  • #37 避免變成奈米服務架構 nano-service architecture 服務間變得更鬆散,依賴性降低,程式庫的版本會更難管裡 (如果要管理的話) 程式語言平台環境可能變得很複雜