zerorest v0.11.2
ZeroREST
Build microservices with ZeroMQ.
Provides familiar express-style API as an abstraction of 0mq request-reply sockets for building REST-like microservices.
Installation
You will need ZeroMQ installed: Get ZeroMQ
npm install zerorestBenchmarks
ZeroREST
- 50k concurrent requests
- 3859.51 requests/sec
- 0.2591 ms/request
Raw Node.js HTTP
- 10k concurrent requests
- 1247.66 requests/sec
- 0.8015 ms/request
See for yourself...
In the repo, you will find some convenient shell scripts for running benchmarks on your local machine.
Setup
git clone git@github.com:chuej/zerorest
cd zerorestZeroREST
./bin/benchmark-zerorestOR
docker-compose run --rm service ./bin/benchmark-zerorestHTTP
./bin/benchmark-httpOR
docker-compose run --rm service ./bin/benchmark-httpQuickstart
Service:
var ZR;
ZR = require('zerorest');
var users, zms;
zms = new ZR("tcp://0.0.0.0:5555");
users = zms.router("/users");
users.route("/findById", function(req, res, next) {
return res.json({
user: {
id: req.params.id
}
});
});
zms.start()Client:
var Client, client;
Client = require('zerorest').Client;
client = new Client("tcp://0.0.0.0:5555");
client.on('start', function() {
var opts;
opts = {
params: {
id: '4321'
}
};
client.request('/users/findById', opts, function(err, resp) {
return console.log(resp);
});
});Examples
Examples are located in the github repo:
git clone git://github.com/chuej/zerorest.git
cd zerorest
npm installDocker / Docker Compose
The following command will start the example service, then the client.
docker-compose upsh scripts
Service:
./bin/example-serviceClient:
./bin/example-clientUsage
var ZR, startService;
ZR = require('zerorest');
startService = function() {
var templates, users, zms;
conf = {
broker: {
concurrency: 5, // number of concurrent router sockets
hearbeat: 2500,
lbmode: 'rr' // load-balance mode: round-robin (rr) or random (rand)
},
worker: {
concurrency: 5, // number of concurrent dealer sockets per route
reconnect: 1000,
heartbeat: 2500
},
noFork: false, // by default, zerorest will fork processes for brokers/workers
url: "tcp://0.0.0.0:5555"
};
zms = new ZR("tcp://0.0.0.0:5555");
// or
zms = new ZR(conf);
zms.use(function(req, res, next) {
// middleware
return next(null);
});
// array of middleware
zms.use([function(req,res,next){ return next(null); }]);
conf = {
reconnect: 1000,
heartbeat: 2500,
path: "/users"
};
users = zms.router("/users");
// or
users = zms.router(conf);
users.use(function(req, res, next) {
// users specific middleware
return next(null);
});
users.use(function(err, req, res, next) {
// users specific error handler
// calling next w/ error will continue on to service error handlers
return next(err);
});
users.route("/findById", function(req, res, next) {
return res.json({
user: {
id: req.params.id
}
});
});
users.route("/update", function(req, res, next) {
res.setStatus(201);
return res.json({
user: {
id: req.params.id
link: {
path: '/findById'
params: {
id: req.params.id
}
}
}
});
});
templates = zms.router("/templates");
templates.route("/html", function(req, res, next) {
return res.send("<html></html>");
});
zms.use(function(err, req, res, next) {
// handler for errs generated in service
//calling next w/ err will trigger default res.error
return res.send("ERROR");
});
zms.on('error', function(err) {
// handle uncaught service/router errors
});
return zms.start();
};
startService();var Client, client;
Client = require('zerorest').Client;
client = new Client("tcp://0.0.0.0:5555");
client.on('start', function() {
var opts, resp, stream;
opts = {
params: {
id: '1234'
},
body: {
data: {
hello: "world"
}
},
headers: {
method: 'PATCH'
},
copts: {
timeout: 100000
}
};
client.request('/users/update', opts, function(err, resp) {
// resp is json
return console.log(resp);
});
opts = {
params: {
id: '4321'
}
};
client.request('/templates/html', opts, function(err, resp) {
// resp is text
return console.log(resp);
});
resp = '';
stream = client.request('/users/findById', opts);
stream.on('data', function(data) {
return resp += data;
});
return stream.on('end', function() {
// resp is stringified json
return console.log(resp);
});
});Tests
Running the test suite is as simple as cloning the repo and running npm test.
git clone git://github.com/chuej/zerorest.git
cd zerorest
npm install
npm testResources
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago