Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

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

7 221 vues

Publié le

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

Publié dans : Logiciels
  • Soyez le premier à commenter

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

  1. 1. #JCConf Dropwizard -微服務架構框架- anthonychen
  2. 2. #JCConf About anthonychen Github: https://github.com/anthonych Blog: http://blog.anthonychen.idv.tw Twitter: https://twitter.com/anthonychen
  3. 3. #JCConf Agenda • Dropwizard Introduction • Microservice Architecture • Why Dropwizard • How to Dropwizard • More Dropwizard
  4. 4. 有一天…
  5. 5. 於是...
  6. 6. 本來我以為是...
  7. 7. 結果其實是...
  8. 8. 其實我需要的只是...
  9. 9. 發現 Dropwizard!
  10. 10. 2011.12 Opened by Yammer
  11. 11. Drop...wizard? http://gunshowcomic.com/316
  12. 12. mustache Dropwizard 包括... Microservice Framework
  13. 13. 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-ganglial dropwizard-views-freemarker dropwizard-forms dropwizard-metrics-graphite dropwizard-views-mustache dropwizard-hibernate dropwizard-metrics dropwizard-views Dropwizard Modules (v0.7.1)
  14. 14. 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.html http://martinfowler.com/articles/microservices.html
  15. 15. pid 1234 8GB heap Monolithic Micro Services pid 5978 2GB heap pid 1234 2GB heap pid 5678 2GB heap pid 9527 2GB heap Architecture
  16. 16. Monolithic Micro Services Scalability
  17. 17. P R O X Y Extensibility
  18. 18. Project Management
  19. 19. Microservice vs. SOA... VS.
  20. 20. SleepyA RESTful framework for Go Microservice Frameworks
  21. 21. Why DropWizard?
  22. 22. http://www.oracle.com/us/technologies/java/gimmejava/usage-terms/index.html
  23. 23. 開發效率
  24. 24. 容易佈署 Single JAR
  25. 25. Self-Contained
  26. 26. http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=json&f=311c-1hq8-0-0
  27. 27. http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=db&f=311c-1hq8-0-0
  28. 28. http://www.techempower.com/benchmarks/#section=data-r9&hw=i7&test=query&f=311c-1hq8-0-0
  29. 29. How to DropWizard?
  30. 30. 自 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
  31. 31. project-api: API 格式類別,通常是 JSON project-client: API 的 HTTP客戶端程式 project-application: 應用程式實作 模組架構
  32. 32. com.example.myapplication: api: API 格式類別實作 cli: Command 類別 client: HTTP 客戶端實作 core: Domain 類別實作 jdbi: JDBI 資料庫存取相關實作 health: Health Check 類別實作 resources: Resource 類別實作 Dropwizard 專案結構
  33. 33. 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)
  34. 34. public class BlogConfiguration extends Configuration { @Valid @NotNull @JsonProperty("database") private DataSourceFactory database = new DataSourceFactory(); public DataSourceFactory getDatabase() { return database; }} Configuration 類別
  35. 35. 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 類別
  36. 36. ViewBundle AssetsBundle HibernateBundle, ScanningHibernateBundle MigrationsBundle 客製自己的Bundle – 實作ConfiguredBundle 介面 Bundle 類別
  37. 37. 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( )
  38. 38. @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( )
  39. 39. 可使用 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
  40. 40. Demo - A simple restful service -
  41. 41. Freemarker (http://freemarker.org) Mustache (http://mustache.github.io) 另類作法:AssetsBundle + JavaScript MVC View Layer
  42. 42. Demo - A simple web application -
  43. 43. More DropWizard?
  44. 44. Fallwizard (https://github.com/Berico-Technologies/Fallwizard) dropwizard-springbundle: (https://github.com/anthonych/Dropwizard-SpringBundle) Dropwizard + Spring
  45. 45. https://github.com/rayokota/generator-angular-dropwizard 手工製作 AngularJS 程式 (使用 AssetsBundle) 使用 Yeoman + Angular-Dropwizard generator 或 Dropwizard + AngularJS
  46. 46. 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 等類別)
  47. 47. grunt server mvn exec:exec –pl [your_app-name]-service Ex. mvn exec:exec –pl demo-service mvn compile 編譯並執行 Service 執行 AngularJS 網頁應用程式
  48. 48. 最後...
  49. 49. Dropwizard 適合...
  50. 50. http://www.flickr.com/photos/psd/13109673843 by Paul Downey 大型應用也可以...
  51. 51. Dropwizard 不是...
  52. 52. Dropwizard 也許只是...
  53. 53. 一不小心...
  54. 54. 別忘了他還是...
  55. 55. 也許有一天...
  56. 56. Thank You!

×