Top Banner
#JCConf Dropwizard - 微微微微微微微 - anthonychen
56

Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Jun 30, 2015

Download

Software

Anthony Chen

JCConf Taiwan 2014 議程簡報:Dropwizard Restful 微服務 (microservice) 初探
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

#JCConf

Dropwizard- 微服務架構框架 -

anthonychen

Page 2: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

#JCConf

About anthonychen

Github: https://github.com/anthonych

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

Twitter: https://

twitter.com/anthonychen

Page 3: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

#JCConf

Agenda

• Dropwizard Introduction

• Microservice Architecture

• Why Dropwizard

• How to Dropwizard

• More Dropwizard

Page 4: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

有一天…

Page 5: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

於是 ...

Page 6: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

本來我以為是 ...

Page 7: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

結果其實是 ...

Page 8: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

其實我需要的只是 ...

Page 9: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

發現 Dropwizard!

Page 10: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

2011.12 Opened by Yammer

Page 11: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Drop...wizard?

http://gunshowcomic.com/316

Page 12: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

mustache

Dropwizard 包括 ...

Microservice Framework

Page 13: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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 14: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 15: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

pid 1234 8GB heap

Monolithic Micro Services

pid 5978 2GB heap

pid 1234 2GB heap

pid 5678 2GB heap

pid 9527 2GB heap

Architecture

Page 16: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Monolithic Micro Services

Scalability

Page 17: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

PROXY

Extensibility

Page 18: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Project Management

Page 19: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Microservice vs. SOA...

VS.

Page 20: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

SleepyA RESTful framework

for Go

Microservice Frameworks

Page 21: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Why DropWizard?

Page 23: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

開發效率

Page 24: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

容易佈署

Single JAR

Page 25: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Self-Contained

Page 26: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 27: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 28: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 29: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

How to DropWizard?

Page 30: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

自 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

Page 31: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

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

project-application: 應用程式實作

模組架構

Page 32: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

com.example.myapplication:

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

Dropwizard 專案結構

Page 33: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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)

Page 34: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

public class BlogConfiguration extends Configuration {

@Valid

@NotNull

@JsonProperty("database")

private DataSourceFactory database = new

DataSourceFactory();

public DataSourceFactory getDatabase() {

return database;

}}

Configuration 類別

Page 35: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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 36: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

ViewBundle

AssetsBundle

HibernateBundle,

ScanningHibernateBundle

MigrationsBundle

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

Bundle 類別

Page 37: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 38: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 39: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

可使用 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

Page 40: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Demo- A simple restful service -

Page 41: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Freemarker (http://freemarker.org)

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

另類作法: AssetsBundle + JavaScript MVC

View Layer

Page 42: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Demo- A simple web application -

Page 43: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

More DropWizard?

Page 45: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

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

使用 Yeoman + Angular-Dropwizard generator

Dropwizard + AngularJS

Page 46: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

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

Page 47: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

grunt server

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

Ex. mvn exec:exec –pl demo-service

mvn compile

編譯並執行 Service

執行 AngularJS 網頁應用程式

Page 48: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

最後 ...

Page 49: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Dropwizard 適合 ...

Page 51: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Dropwizard 不是 ...

Page 52: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Dropwizard 也許只是 ...

Page 53: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

一不小心 ...

Page 54: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

別忘了他還是 ...

Page 55: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

也許有一天 ...

Page 56: Dropwizard Restful 微服務 (microservice) 初探 - JCConf TW 2014

Thank You!