2013年8月3日星期六
Using Node.js + Mongodb build projects based on Cloud Foundry
This article will introduce VMware's cloud computing platform - Cloud Foundry, this article will use Node.js + MongoDB build projects based on Cloud Foundry.
essential knowledge:
Node.js and NPM command line use.
Node.js syntax.
MongoDB syntax.
Mongoose syntax.
environment to build:
Windows 7
Node.js + Ruby + vmc (Cloud Foundry CIL command-line tool)
user level and degree of difficulty:
All Users
moderate
sample files:
http://pan.baidu.com/share/link? shareid = 30772 & uk = 2298510329
will learn through this article:
grasp Node.js, Ruby, DevKit, vmc (Cloud Foundry CIL command-line tool) environment configuration.
tunnel connection Cloud Foundry using a remote database (MongoDB) approach.
Node.js configuration:
Download: http://nodejs.org/download/
configuration:
According to the actual situation download the corresponding node.js installation package. (Select the version of Node.js as possible with Cloud Foundry environment consistent)
due to the use msi program, so simply double-click the installation package can be downloaded.
reboot the system.
verification:
typing in cmd: node-v / npm-v if there is a corresponding version number, the installation was successful.
Ruby configuration:
Cloud Foundry's operation is based on vmc (CIL command line tool), is required before installing vmc Ruby environment.
Because of the need to use Devkit, so here is a Ruby version: 1.9.2. (1.9.3 there is a problem in the DevKit)
Download: https://rubyforge.org/frs/?group_id=167 (Please download the 1.9.2-p290 version)
configuration:
extract to any folder, such as: {drive}: \ DevTools \ ruby-1.9.2-p290.
add a Windows environment variable:
RUBY_HOME = {letter}: \ DevTools \ ruby-1.9.2-p290
Path + =% RUBY_HOME% \ bin (must be configured in the system environment variables, rather than the user environment variables)
reboot the system.
verification:
typing in cmd: ruby-v / gem-v If the version number, the installation was successful.
vmc installation:
installation:
Using gem install vmc, type in cmd gem install vmc. (No need to reboot)
Verification:
typing in cmd vmc-v, if the version number, the installation was successful.
vmc use:
establish the connection:
vmc target api.cloudfoundry.com (designated Cloud Foundry API address)
Create an account:
through the website creation: In http://www.cloudfoundry.com/ established accounts.
By vmc established in cmd type: vmc add-user [- email, - passwd].
visit:
type in cmd: vmc login (when prompted enter the registered email address and password)
establishment app (Node.js):
established in the local folder, such as: {drive}: \ nodejs \ local \ testcf
locate testcf root directory, type in cmd: npm install express
new folder in testcf index.js file and type the following:
var express = require ('express');
var app = express ();
app.get ('/', function (req, res) {
res.send ('Hello from Cloud Foundry');
});
app.listen (process.env.VMC_APP_PORT | | 3000);
console.log ("Server start up!");
this test:
locate testcf root directory, type in cmd: node index.js
Console appears: "Server start up!" after typing in the browser: http://loaclhost:3000, such as displaying "Hello from Cloud Foundry", the instructions to run successfully.
Upload App to Cloud Foundry:
locate testcf root directory, type in cmd: vmc push - runtime = node08, generates the following contents:
Would you like to deploy from the current directory? [Yn]: y
Application Name: ks-test-cf
Detected a Node.js Application, is this correct? [Yn]: y
Application Deployed URL [ks-test-cf.cloudfoundry.com]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [64M]: 128
How many instances? [1]: 1
Bind existing services to 'ks-test-cf'? [yN]: n
Create services to bind to 'ks-test-cf'? [yN]: n
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (22K): OK
Push Status: OK
Staging Application 'ks-test-cf': OK
Starting Application 'ks-test-cf': OK
Note: The above is relatively simple English translation skipped. Since the current project does not use a database, so
not binding (Binding) and establishing (Create) any Service.
In a browser, type: http://ks-test-cf.cloudfoundry.com/, where there is "Hello from Cloud Foundry", the instructions to run successfully. (With the same effect as running locally)
MongoDB configuration:
Download:
http://www.mongodb.org/downloads
configuration:
According to the actual situation download the corresponding MongoDB archive.
Note: Due to Cloud Foundry corresponding MongoDB version is 2.0, so to avoid the local test environment with Cloud Foundry inconsistency, try to choose the appropriate version.
extract to any folder, such as: {drive}: \ DevTools \ mongodb-2.0.7.
add a Windows environment variable:
MONGODB_HOME = {letter}: \ DevTools \ mongodb-2.0.7
Path + =% MONGODB_HOME% \ bin
reboot the system.
Verification:
type in cmd: mongo - version such as version number appears, the installation was successful.
Use:
locally to create a folder, such as: {drive}: \ mongodb \ testdb.
type in cmd: mongod - dbpath {letter}: \ mongodb \ testdb, you can connect to the local database.
Note: You can use MongoVUE other client operating MongoDB.
Mongoose configuration:
Mongoose is a third party Noe.js modules can be more convenient operation MongoDB.
Download:
type in cmd: npm install mongoose
Verification: (as similar to the picture below, the installation is successful)
Note:
mongoose at the time of installation relies on mongodb (Node.js modules)
If mongodb module is not installed, then, mongoose will automatically install, as shown above.
standalone installation mongodb, type in cmd: npm install mongodb (Note npm version must be greater than 1.1.19, there would be an error)
Node.js connection MongoDB: (Localhost locally)
Mongoose introduction:
var db = require ('mongoose');
define an Object Structure: (In order to fit with Cloud Foundry)
mongo = {
'hostname': 'localhost',
'port': 27017,
'username':'',
'password':'',
'db': testdb
}
Note: hostname, port and db these three attributes. (Should correspond to your development environment)
generate mongoose URL string when connecting mongodb:
var generate_mongo_url = function (mongo) {
return 'mongodb :/ /' + mongo.hostname + ':' + mongo.port + '/' + mongo.db ;
}
connect to the database (MongoDB):
db.connect (mongourl);
Create Schame and Model:
var Schema = db.Schema,
ObjectId = Schema.ObjectId;
var testSchema = new Schema ({
host: String,
dbs: String,
time: Date
});
binding model:
var TestModule = db.model ('test-cloudfoundry', testSchema);
Note: test-cloudfoundry is the name of the Collection (Collection equivalent to the SQL Table concept)
save data to the Collection:
var test = new TestModule ();
test.host = mongo.hostname;
test.dbs = mongo.db;
test.time = new Date ();
test.save (function (err) {
if (! err) {
console.log ('save complete')
}
else {
console.log ('save error =' + err)
}
});
Note: save method is equivalent to the SQL Insert statement, but also defines the callback function Mongoose
for use.
run:
Use mongod - dbpath way to connect to the local MongoDB.
positioned to test-cf root directory, type in cmd: node index.js.
in your browser to access http://localhost:3000/, the following appears in cmd, then call MongoDB success.
mongodb url: mongodb :/ / localhost: 2701
/ testdbServer start at http://localhost:3000
save complete
Note:
mongo can not define variable of type Object, just to meet with Cloud Foundry was so treated.
Mongoose unique Schame mechanism corresponds to Table data structure.
Connect Cloud Foundry remote database:
Installation:
gem install eventmachine - pre
gem install caldecott (such as abnormal or are mistakes, you need to install DevKit, otherwise skip this section)
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instruction
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
DevKit installation:
Download: http://rubyinstaller.org/downloads/
configuration:
extract to any folder, such as {letter}: \ DevTools \ devkit-4.5.2
positioned to devkit-4.5.2 root directory, type in cmd: ruby dk.rb init
properly installed, will generate config.yml file, edit the file and type the directory where the ruby format: - {letter} :/ DevTools/ruby-1.9.2-p290 (Note Henggang, slash)
ruby dk.rb review (check to add support for Ruby DevKit list is wrong, you can skip)
ruby dk.rb install
Verification:
gem install caldecott
without any error message appears, the installation was successful.
Note:
DevKit version: DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe
DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe corresponding Ruby version :1.9.2-p290
Note: The above has been verified through a matching version, the other version is not verified.
Node.js connection MongoDB: (Cloud Foundry remotely)
New MongoDB Service:
vmc create-service mongodb testdb
Binding Service to App (ks-test-cf):
vmc bind-service testdb ks-test-cf
Note: testdb is just created MongoDB; ks-test-cf is previously uploaded (push) the App Name.
connect to local MongoDB for Node.js transformation:
modify mongo variables:
if (process.env.VCAP_SERVICES) {
var env = JSON.parse (process.env.VCAP_SERVICES);
mongo = env ['mongodb-2.0'] [0] ['credentials'];
}
else {
mongo = {
'hostname': 'localhost',
'port': 27017,
'username':'',
'password':'',
'db': 'testdb'
}
}
Note: Increasing the logic to determine the current environment, process.env.VCAP_SERVICES is Node.js
provides system variable (JSON), preservation of the current landing Cloud Foundry App
(ks-test-cf) the necessary information.
modify generate_mongo_url Method:
var generate_mongo_url = function (obj) {
if (process.env.VCAP_SERVICES) {
return 'mongodb :/ /' + obj.username + ':' + obj.password + '@' + obj.hostname ; + ':' + obj.port + '/' + obj.db;
}
else {
return 'mongodb :/ /' + obj.hostname + ':' + obj.port + '/' + obj.db ;
}
}
Note: Add the current environment for the Cloud Foundry, the resulting string mongodb logic,
url relatively local connections, increasing the username, password.
Updated:
locate testcf root directory, type in cmd: vmc update ks-test-cf, where there is the following, and then the update was successful:
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (26K): OK
Push Status: OK
Stopping Application 'ks-test-cf': OK
Staging Application 'ks-test-cf': OK
Starting Application 'ks-test-cf': OK
run:
In a browser, type: http://ks-test-cf.cloudfoundry.com/
type in cmd: vmc logs ks-test-cf (See ks-test-cf background printing information)
Server start up!
mongodb url: mongodb :/ XXXXXXXXXXX: XXXXXXXXXXX@172.30.48.68: 25176/db
Server start at http://172.30.50.21:12265
save complete
remote connection Cloud Foundry database (testdb):
type in cmd: vmc tunnel testdb, there will be the following:
Binding Service [testdb]: OK
Stopping Application 'caldecott': OK
Staging Application 'caldecott': OK
Starting Application 'caldecott': OK
Getting tunnel connection info: OK
Service connection info:
username: XXXXXXXXXXX
password: XXXXXXXXXXX
name: db
url: mongodb :/ / XXXXXXXXXXX: XXXXXXXXXXX@172.30.48.68: 25176/db
Starting tunnel to testdb on port 10000.
1: none
2: mongo
3: mongodump
4: mongorestore
Which client would you like to start
Note: where username and password is Cloud Foundry system-generated, has been hidden in here
go. Select one, you can use a local client to connect, as previously mentioned MongoVUE.
MongoVUE Connection:
Note: Service, Port accordance with the above filling; Username, Password, Database (s)
accordance with Cloud Foundry given written content.
Note:
Note env ['mongodb-2.0'] [0] ['credentials'] in the 'mongodb-2.0' means: MongoDB version number.
when using vmc tunnel testdb, the need to "Connect Cloud Foundry remote databases" section for configuration.
These changes give only the key code, the remaining code see attachment.
Summary:
vmc requires Ruby environment.
Connect Cloud Foundry remote database requires the Ruby eventmachine, caldecott these two packages.
install caldecott need DevKit support.
By increasing the Cloud Foundry system variables: process.env.VCAP_SERVICES to carry out the judgment of the current environment.
------ Solution ------------------------------------ --------
good, preaching Tuition FAQ effect. .
------ Solution ---------------------------------------- ----
This is very good, oh
------ Solution ---------------------------- ----------------
article will use Node.js + MongoDB Building a Cloud
------ Solution ------ --------------------------------------
landlord encouragement, praise Share
------ Solution ------------------------------------------- -
this project based on Cloud Foundry exactly what to do with the
------ Solution ------------------------ --------------------
Oh, I did not expect, this article has been updated here, but did not tell me, but also that it has not been turned ~
original address of this article: http://tk-zone.cn/post/2012-09-13 / cloud-foundry
message to everyone in here ~
------ For reference only --------------------------- ------------
ah. Landlord information useful.
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
little understand
------ For reference only --------------------------- ------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
good, very good! ! ! ! !
------ For reference only -------------------------------------- -
10000000 a top
------ For reference only ------------------------------- --------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------------
top ten with me. . . .
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
do not know, but seemingly good
------ For reference only ---------------------- -----------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
very long ah, the proposed landlord entire pdf document for everyone to download.
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
country has not done a decent cloud
------ For reference only --------------------- ------------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
landlord v5, very helpful to me, look at the first
------ For reference only ------------------ ---------------------
------ For reference only ---------------------------------- -----
incompatible encoding regexp match (GBK regexp with UTF-8), how to solve this error?
------ For reference only -------------------------------------- -
http://topic.csdn.net/u/20120913/17/fac34745-28d4-4b5b-b301-2e6425b2e350.html?seed=932620697&r=79722298 # r_79722298
------ For reference only ---------------------------------------
http://un . zhubajie.com / r /? u = 7314963 & l = http://task.zhubajie.com/post/
------ For reference only ------------ ---------------------------
------ For reference only ---------------------------------- -----
although do not understand, feel good
------ For reference only ----------------------- ----------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
so complicated, thank you landlord
------ For reference only ------------------------- --------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
good oh. . .
------ For reference only -------------------------------------- -
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
perform this one after vmc logs ks-test-cf, cloud-foundry gives an error:
cannot find module 'mongoose'
but why?
------ For reference only ------------------------------ ---------
your environment is not installed mongoose this module?
------ For reference only -------------------------------------- -
ah, PDF Address: http://pan.baidu.com/share/link?shareid=79642 & ; uk = 2298510329
there are problems, you can always here or my microblogging http://weibo.com/23784148 to contact me :)
订阅:
博文评论 (Atom)
没有评论:
发表评论