Dropwizard Introduction

Preview:

DESCRIPTION

Dropwizard introduction presentation slide for TWJUG Sep. event.

Citation preview

Dropwizard-微服務架構框架 -

anthonychen

Once Upon a Time

Hibernate

Spring

JPA

SpringMVC

Tomcat

Jackson

Freemarkeror

Thymeleaf

Logback

Dropwizard

Full Stack Framework with microservice architecture

Drop...wizard?

Jackson

JDBI

mustache

Full Stack, RESTful and Open Source

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-ganglialdropwizard-views-

freemarker

dropwizard-forms dropwizard-metrics-graphite dropwizard-views-mustache

dropwizard-hibernate dropwizard-metrics dropwizard-views

Dropwizard Modules (v0.7.1)

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

pid 1234 8GB heap

Monolithic Micro Services

pid 5678 2GB heap

pid 5978 2GB heap

pid 1234 2GB heap

pid 9527 2GB heap

Scalability

Project Management

PROXY

Extensibility

Microservice Frameworks

SleepyA RESTful framework for

Go

Phlytya microframework using ZF2 components.

Why DropWizard?

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

Metrics

How to Start DropWizard?

Overview

Resource

Application Bundle

Configuration

Representation

Environment

Views

Project Orginizationcom.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

public class BlogConfiguration extends Configuration {

@Valid

@NotNull

@JsonProperty("database")

private DataSourceFactory database = new DataSourceFactory();

public DataSourceFactory getDatabase() {

return database;

}}

Configuration Class

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"

Configuration File (YAML)

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;

}}

Representation Class

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 - Bundle

    @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);

}}

Application Class – Environment

View Layer

Freemarker - http://freemarker.org

Mustache - http://mustache.github.io

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());

       }

   }

}

Health Check

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

Recommended