dropwizard introduction

38
Dropwizard - 微微微微微微微 - anthonychen

Upload: anthony-chen

Post on 29-Nov-2014

368 views

Category:

Technology


2 download

DESCRIPTION

Dropwizard introduction presentation slide for TWJUG Sep. event.

TRANSCRIPT

Page 1: Dropwizard Introduction

Dropwizard-微服務架構框架 -

anthonychen

Page 2: Dropwizard Introduction

Once Upon a Time

Hibernate

Spring

JPA

SpringMVC

Tomcat

Jackson

Freemarkeror

Thymeleaf

Logback

Page 3: Dropwizard Introduction

Dropwizard

Full Stack Framework with microservice architecture

Page 4: Dropwizard Introduction

Drop...wizard?

Page 5: Dropwizard Introduction
Page 6: Dropwizard Introduction

Jackson

JDBI

mustache

Full Stack, RESTful and Open Source

Page 7: Dropwizard Introduction

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)

Page 8: Dropwizard Introduction

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

Page 9: Dropwizard Introduction

pid 1234 8GB heap

Monolithic Micro Services

pid 5678 2GB heap

pid 5978 2GB heap

pid 1234 2GB heap

pid 9527 2GB heap

Page 10: Dropwizard Introduction

Scalability

Page 11: Dropwizard Introduction

Project Management

Page 12: Dropwizard Introduction

PROXY

Extensibility

Page 13: Dropwizard Introduction

Microservice Frameworks

SleepyA RESTful framework for

Go

Phlytya microframework using ZF2 components.

Page 14: Dropwizard Introduction

Why DropWizard?

Page 16: Dropwizard Introduction

Dropwizard - Pros

Productivity – Do one thing at a time

Do your best with what you have

Simple & Lightweight

Easy Test, Deployment and

Management

Performance

Page 17: Dropwizard Introduction

Dropwizard - Pros

Productivity – Do one thing at a time

Do your best with what you have

Simple & Lightweight

Easy Test, Deployment and

Management

Performance

Page 18: Dropwizard Introduction

http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=json&f=311c-1hq8-0-0

Page 19: Dropwizard Introduction

http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=db&f=311c-1hq8-0-0

Page 20: Dropwizard Introduction

http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=query&f=311c-1hq8-0-0

Page 21: Dropwizard Introduction

Dropwizard - Cons

Frequently and Large-Scale Changes

Nano-Service Architecture

Complexity at Infrastructure Level

No Application Server

Put More Effort on Monitoring and

Metrics

Page 22: Dropwizard Introduction

How to Start DropWizard?

Page 23: Dropwizard Introduction

Overview

Resource

Application Bundle

Configuration

Representation

Environment

Views

Page 24: Dropwizard Introduction

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

Page 25: Dropwizard Introduction

public class BlogConfiguration extends Configuration {

@Valid

@NotNull

@JsonProperty("database")

private DataSourceFactory database = new DataSourceFactory();

public DataSourceFactory getDatabase() {

return database;

}}

Configuration Class

Page 26: Dropwizard Introduction

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)

Page 27: Dropwizard Introduction

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

Page 28: Dropwizard Introduction

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

Page 29: Dropwizard Introduction

    @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

Page 30: Dropwizard Introduction

View Layer

Freemarker - http://freemarker.org

Mustache - http://mustache.github.io

Page 31: Dropwizard Introduction

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

Page 32: Dropwizard Introduction

Demo-A Sample Blog

Page 33: Dropwizard Introduction

Build - A Fat JAR

An easy maintainable, single deployable

artifact

maven-shade or maven-assembly-

plugin

Page 34: Dropwizard Introduction

Run

Command line:

java -jar blog-sample-0.0.1-SNAPSHOT.jar server blog.yml

exec-maven-plugin

Page 35: Dropwizard Introduction

More DropWizard?

Page 36: Dropwizard Introduction

Metric

Page 37: Dropwizard Introduction

AngularJS & DropWizard

Page 38: Dropwizard Introduction

Spring & DropWizard