dropwizard restful 微服務 (microservice) 初探 - jcconf tw 2014
DESCRIPTION
JCConf Taiwan 2014 議程簡報:Dropwizard Restful 微服務 (microservice) 初探TRANSCRIPT
#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-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
http://www.oracle.com/us/technologies/java/gimmejava/usage-terms/index.html
自 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
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
Freemarker (http://freemarker.org)
Mustache (http://mustache.github.io)
另類作法: AssetsBundle + JavaScript MVC
View Layer
Fallwizard (https://github.com/Berico-Technologies/Fallwizard)
dropwizard-springbundle: (https://github.com/anthonych/Dropwizard-SpringBundle)
Dropwizard + Spring
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 網頁應用程式
http://www.flickr.com/photos/psd/13109673843 by Paul Downey
大型應用也可以 ...