dropwizard restful 微服務 (microservice) 初探 - jcconf tw 2014

56
#JCConf Dropwizard - 微微微微微微微 - anthonychen

Upload: anthony-chen

Post on 30-Jun-2015

3.578 views

Category:

Software


1 download

DESCRIPTION

JCConf Taiwan 2014 議程簡報:Dropwizard Restful 微服務 (microservice) 初探

TRANSCRIPT

#JCConf

Dropwizard- 微服務架構框架 -

anthonychen

#JCConf

About anthonychen

Github: https://github.com/anthonych

Blog: http://blog.anthonychen.idv.tw

Twitter: https://

twitter.com/anthonychen

#JCConf

Agenda

• Dropwizard Introduction

• Microservice Architecture

• Why Dropwizard

• How to Dropwizard

• More Dropwizard

有一天…

於是 ...

本來我以為是 ...

結果其實是 ...

其實我需要的只是 ...

發現 Dropwizard!

2011.12 Opened by Yammer

Drop...wizard?

http://gunshowcomic.com/316

mustache

Dropwizard 包括 ...

Microservice Framework

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

http://martinfowler.com/articles/microservices.htmlhttp://martinfowler.com/articles/microservices.html

pid 1234 8GB heap

Monolithic Micro Services

pid 5978 2GB heap

pid 1234 2GB heap

pid 5678 2GB heap

pid 9527 2GB heap

Architecture

Monolithic Micro Services

Scalability

PROXY

Extensibility

Project Management

Microservice vs. SOA...

VS.

SleepyA RESTful framework

for Go

Microservice Frameworks

Why DropWizard?

開發效率

容易佈署

Single JAR

Self-Contained

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

How to DropWizard?

自 Dropwizard 0.7 版開始, Service 類別改名為 Application

開發流程

Configuration• YAML 設定檔• Configuration 類別

Domain• JDBI, Hibernate• POJO 類別• 可以是 hibernate entity

Resource• Jersey controller 類別• RESTful API 方法

Health Check• 自訂 Healcheck 類別• Freemarker, Mustache

Application• 程式啟動點• 設定 Resource, Healcheck...

Configuration Domain/DAOs Resource

Healthcheck/View Application

Build & Run

project-api: API 格式類別,通常是 JSON

project-client: API 的 HTTP 客戶端程式

project-application: 應用程式實作

模組架構

com.example.myapplication:

api: API 格式類別實作cli: Command 類別client: HTTP 客戶端實作core: Domain 類別實作 jdbi: JDBI 資料庫存取相關實作health: Health Check 類別實作 resources: Resource 類別實作

Dropwizard 專案結構

server:

type:simple

applicationContextPath:/application

adminContextPath:/admin

database:

driverClass:com.mysql.jdbc.Driver

user:anthonychen

password:anthonychen

url:jdbc:mysql://localhost:3306/demo

Configuration Reference: http://dropwizard.io/manual/configuration.html

Configuration 檔案 (YAML)

public class BlogConfiguration extends Configuration {

@Valid

@NotNull

@JsonProperty("database")

private DataSourceFactory database = new

DataSourceFactory();

public DataSourceFactory getDatabase() {

return database;

}}

Configuration 類別

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 類別

ViewBundle

AssetsBundle

HibernateBundle,

ScanningHibernateBundle

MigrationsBundle

客製自己的 Bundle – 實作 ConfiguredBundle 介面

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

}}

註冊 Bundles

Application 類別 – initialize( )

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

}}

註冊 Resources, HealthChecks...

Application 類別 – run( )

可使用 maven-shade 或 maven-assembly-plugin包成 Fat JAR 檔案

命令列執行

java -jar [your_jar_file] server [your_yaml_file]

Ex. java –jar demo-0.0.1-SNAPSHOT.jar server demo.yaml

瀏覽器開啟 http://localhost:8080 即可開始使用應用程式

Build & Run

Demo- A simple restful service -

Freemarker (http://freemarker.org)

Mustache (http://mustache.github.io)

另類作法: AssetsBundle + JavaScript MVC

View Layer

Demo- A simple web application -

More DropWizard?

https://github.com/rayokota/generator-angular-dropwizard

手工製作 AngularJS 程式 ( 使用 AssetsBundle)

使用 Yeoman + Angular-Dropwizard generator

Dropwizard + AngularJS

Yeoman + Angular-Dropwizard Generator

yo angular-dropwizard

命令列建立 Dropwizard 專案初步結構

yo angular-dropwizard:entity [your_entity_class_name]

Ex. yo angular-dropwizard:entity User

建立 Entity 類別 ( 自動產生 DAO 、 Resource 等類別 )

grunt server

mvn exec:exec –pl [your_app-name]-service

Ex. mvn exec:exec –pl demo-service

mvn compile

編譯並執行 Service

執行 AngularJS 網頁應用程式

最後 ...

Dropwizard 適合 ...

Dropwizard 不是 ...

Dropwizard 也許只是 ...

一不小心 ...

別忘了他還是 ...

也許有一天 ...

Thank You!