micromq
Microservice framework based on native Node.js HTTP module and AMQP protocol with express middleware & built-in prometheus. 🔬 🐇
Install
$ npm i micromq
Tests
$ RABBIT_URL=amqp://localhost node tests/apps/users.js &$ RABBIT_URL=amqp://localhost node tests/apps/balances.js &$ RABBIT_URL=amqp://localhost PORT=3000 node tests/apps/gateway.js &$ PORT=3000 npm test
Examples
There are some simple examples.
API
Gateway
.constructor(options)
options
<Object>
This method creates gateway.
const Gateway = ; const gateway = microservices: 'users' 'orders' rabbit: url: 'amqp://localhost:5672' requests: timeout: 5000 ;
.action(name, handler)
This method creates RPC action.
const Gateway = ;const Users = ; const gateway = ... ; gatewayaction'increase_balance' async { if !metaamount || Number resstatus400; res; return; await Users; res;}; gateway;
const MicroMQ = ; const app = ... ; app; appstart;
.on(name, handler)
This method creates application event (read more).
const MicroMQ = ; const app = ... ; // register eventapp; app; app; appstart;
.emit(name, ...args)
name
<string> Event name...args
<...any> - Arguments
This method emits application event.
.enablePrometheus(endpoint, credentials)
This method enables prometheus monitoring.
const Gateway = ; const gateway = ... ; // start prometheus with default /metrics endpoint & without credentialsgateway; // start prometheus with /users/metrics endpoint & without credentialsgateway; // start prometheus with /users/metrics endpoint & credentialsgateway; // start prometheus with default /metrics endpoint & credentialsgateway; gateway;
.use(...middlewares)
...middlewares
<...function> Middlewares
This method adds middlewares.
.all(path, ...middlewares)
This method creates endpoint for all HTTP-methods.
.options(path, ...middlewares),
.get(path, ...middlewares),
.post(path, ...middlewares),
.put(path, ...middlewares),
.patch(path, ...middlewares),
.delete(path, ...middlewares)
This method creates endpoint with needed method.
.middleware()
- returns: <function>
This method returns middleware for express.
const express = ;const bodyParser = ;const Gateway = ;const users = ; const app = ;const gateway = ... ; app; // apply middlewareapp; // monolith routerapp; // delegate requests to the microserviceapp;
.listen(port)
port
<number> Port for listen.
This method creates HTTP-server and starts listen needed port.
MicroMQ
.constructor(options)
options
<Object>
This method creates microservice.
const MicroMQ = ; const app = microservice: 'users' rabbit: url: 'amqp://localhost:5672' ;
.action(name, handler)
This method creates RPC action.
const MicroMQ = ;const Users = ; const app = ... ; appaction'new_deposit' async { await Users; res;}; appstart;
.enablePrometheus(endpoint, credentials)
This method enables prometheus monitoring.
const MicroMQ = ; const app = ... ; // start prometheus with default /metrics endpoint & without credentialsapp; // start prometheus with /users/metrics endpoint & without credentialsapp; // start prometheus with /users/metrics endpoint & credentialsapp; // start prometheus with default /metrics endpoint & credentialsapp; appstart;
.use(...middlewares)
...middlewares
<...function> Middlewares
This method adds middlewares.
.all(path, ...middlewares)
This method creates endpoint for all HTTP-methods.
.options(path, ...middlewares),
.get(path, ...middlewares),
.post(path, ...middlewares),
.put(path, ...middlewares),
.patch(path, ...middlewares),
.delete(path, ...middlewares)
This method creates endpoint with needed method.
.ask(name, query)
name
<string> Microservice for askquery
<Object>- returns: <Promise<Object>> { status: <number>, response: <any> }
This method asks other microservice.
const MicroMQ = ; const app = ... microservices: 'balances'; app; appstart;
.listen(port)
port
<number> Port for listen.
This method creates HTTP-server and starts listen needed port. The usual use case for this method is run tests.
.start()
This method starts microservice.