第8回cloudfoundry輪読会発表資料(node.js on cloud foundry)

19
© 2011 VMware Inc. All rights reserved 2012/7/26 ヴイエムウェア株式会社 池俊彦 Node.js on Cloud Foundry NPMによるNode.jsモジュール管のサポートについて オリジナルコンテンツ:Cloud Foundry Supports Node.js Modules with NPM http://blog.cloudfoundry.com/2012/05/24/cloud-foundry-supports-node-js-modules-with-npm/

Upload: toshihiko-ikeda

Post on 12-Nov-2014

1.088 views

Category:

Documents


7 download

DESCRIPTION

 

TRANSCRIPT

  • 1. Node.js on Cloud Foundry NPMNode.js Cloud Foundry Supports Node.js Modules with NPMhttp://blog.cloudfoundry.com/2012/05/24/cloud-foundry-supports-node-js-modules-with-npm/ 2012/7/26 2011 VMware Inc. All rights reserved
  • 2. Toshihiko Ikeda! [email protected] / @tikeda123! 12 years in enterprise middleware(java application server)! /SOA! c/c++/Java/Ruby/JavaScript! Spring Framework(core)/rails/grails?!2
  • 3. Node.js 1. Google ChromeJavaScriptV8 JavaScript 2. I/O Chrome TCP/HTTP/ Async Event Other JS V8 DNS I/O Support C/C++ libcv Modules C++ lib C/C++ lib C/C++ lib C/C++ libcv C/C++ Environment JS Env Node.js Under-the-hood3
  • 4. I/O http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/4
  • 5. Multi-threaded server - Threads are spawned for every connection User1 i/o request T1 DB User2 i/o request T2 Blocking I/O User3Refreshes 2 times T3 T4 T5 T6 T7 T8 FS User4refreshes 3 times T9 Multi threaded server T Thread Because every I/o is blocking, server spawns a thread per connection* to support multiple requests 5
  • 6. Non-blocking & Evented I/O (Node.js server) JS C/C++ T1 User1 i/o request V8 DB T1 Libio T1 POSIX V8 Event loop V8 Async User2 i/o request (Libev) Threads T1 delegate i/o to Non-blocking t2 I/O V8 libeio t1 User3Refreshes 2 times Single t4 thread t3 serves T1 t6 all users V8 t5 User4refreshes 3 times T1 t7 FS V8 i/o result returned T1 T1 2 EL after x time V8 T1 V8 T1 JS Thread running V8 V8 your code (Single threaded) Node.js t1 POSIX threads doing t2 async I/O (multi-threaded) t3 Everything except your (JS) code is runs in parallel (by libio) 6
  • 7. Multi-threaded blocking server (Apache) VS Event-driven, non-blocking server (Nginx) performance Reqs/sec v/s concurrent connections At ~4000 concurrent connections, - Nginx can serve ~9000 reqs/sec - Apache can serve ~3000 reqs/sec Ref: http://blog.webfaction.com/a-little-holiday-present 7
  • 8. NPMNode.js NPM(Node Package Manager) Node.jsRubyRVMnode npm install package.json ./node_modules npm list npm shrinkwrap npm- shrinkwrap.json8
  • 9. NPMNode.js NPM(node package manager)Node vmc push app-name Installing dependencies. Node version 0.6.8 Failed getting the requested package: [email protected] package.json./ node_modules npm install package.json npm-shrinkwrap.jsoncf vmc pushnpm-shrinkwrap.json npm shrinkwrap 9
  • 10. NPMNode.js cloudfoundry.jsonignoreNodeModules true node_modules bcrypt,bers,nowjsNative cloudfoundry.json cloudfoundry.json { "ignoreNodeModules" : true } vmc push vmc push runtime=node06(Node.js $vmc push runtime=node06 Would you like to deploy from the current directory? [Yn]: Y Application Name: ike-node Detected a Node.js Application, is this correct? [Yn]: Y Application Deployed URL [ike-node.cloudfoundry.com]: Creating Application: OK Uploading Application: Checking for available resources: OK Processing resources: OK Packing application: OK Uploading (15K): OK Push Status: OK Staging Application ike-node: OK Starting Application ike-node: OK 10
  • 11. 1. JavaScriptV8 JavaScript2. I/O 3. Cloudfoundry.comNPMNode NPM4. NPMbcrypt,bers,nowjsNative 11
  • 12. NPMNode.js cf.comnpm-shrinkwrap.json gitURL{ "name": "node-dependencies-example", "version": "0.1.0", "dependencies": { "express": { "version": 3.0.0rc1", "dependencies": { "connect": { "version": "1.8.7",} NG{ "name": "node-dependencies-example", "version": "0.1.0", "dependencies": { "express": { "version": 3.0.0rc1", from: git://github.com/visionmedia/express.git, "dependencies": { "connect": { "version": "1.8.7",}13
  • 13. PythonPerlJavaScript8 TIOBE Software2012314
  • 14. node.js + express 1.express $express -t ejs examplehello create : examplehello create : examplehello/views/index.ejs dont forget to install dependencies: $ cd examplehello && npm install 2.express $cd examplehello $ls app.js package.json public routes views app.js:expressJavaScript package.json:express public:Web uiCSS,JavaScript routes: view:15
  • 15. Cloud Foundry16
  • 16. node.js + express3.npm installnode$npm [email protected] node_modules/express [email protected] [email protected] [email protected] [email protected] ([email protected])17
  • 17. NPMNode.js app.jsvar app = require("express").createServer();var bcrypt = require("bcrypt");app.get("/", function(req, res) {var salt = bcrypt.genSaltSync(10);var hash = bcrypt.hashSync("B4c0//", salt);if (bcrypt.compareSync("B4c0//", hash))res.send("World is safe!");elseres.send("World is in trouble!");});var port = process.env.VCAP_APP_PORT || 3000;app.listen(port); package.json{ "name" : "node-dependencies-example", "version" : "0.1.0", "dependencies" : { "express" : "2.5.x", "bcrypt" : "0.5.x" }}18
  • 18. NPMNode.js npm-shrinkwrap.json{ "name": "node-dependencies-example", "version": "0.1.0", "dependencies": { "express": { "version": "2.5.9", "dependencies": { "connect": { "version": "1.8.7", "dependencies": { "formidable": { "version": "1.0.9" } } }, "mime": { "version": "1.2.4" }, "qs": { "version": "0.4.2" }, "mkdirp": { "version": "0.3.0" } } }, "bcrypt": { "version": "0.5.0" } }} 19