rams-pro v0.2.4
Rams Pro (RabbitMQ-Microservices Pro)
A light and easy-to-use microservices framework for Node.js, call remote functions like service1.do.say.hi('Hello world!') or call('service1:/do/say/hi', 'Hello world!') and get the results immediately.
Rams Pro is based on RabbitMQ and Redis. If you just need call('service1:/do/say/hi', 'Hello world!') only, try Rams.
- Why Enterprises Are Embracing Microservices and Node.js
- Microservices With Node.js: Scalable, Superior, and Secure Apps
Prepare
1. Install Docker (Docker CE recommended)
2. Install RabbitMQ in Docker
1) Install: docker pull rabbitmq:management
2) Start: docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq:management
3) Login to RabbitMQ management web page: http://localhost:15672/
(guest/guest)
3. Install Redis in Docker
1) Install: docker pull redis
2) Start: docker run --restart=always -it --name redis -d -p 6379:6379 redis redis-server --requirepass ramspro123
TL;DR
After prepared, run example.
Installation
See below section "Quick Start" for more details.
npm install rams-pro --save
Test:
git clone https://github.com/hiowenluke/rams-pro.git
cd rams-pro
npm install
npm test
Quick Start
Create a new directory for rams-pro-demo first:
mkdir ./rams-pro-demo && cd ./rams-pro-demo
1. Create Microservices #1
Open a new tab in terminal, initialize a new directory for service1
# relative to path rams-pro-demo
mkdir ./service1 && cd ./service1 && npm init -y
Install Rams Pro in service1
npm install rams-pro --save
Create index.js
const rams = require('rams-pro');
const options = {
rabbitMQ: {
host: 'localhost', // it can be omitted if it is 'localhost'
},
redis: {
host: 'localhost', // it can be omitted if it is 'localhost'
// same as in "docker run ... --requirepass ramspro123" above
password: 'ramspro123',
}
};
// Arguments:
// "s1"
// The name of this microservice.
// It can be omitted if you have only one microservice.
// "./lib"
// The root folder name of business files such as "lib/say/hi.js".
// It can be omitted or replaced with other names such as "./biz", "./src", etc.
// It should be started with ".".
// options
// The options of rabbitMQ and redis
rams.initServer('s1', './lib', options);
Create lib/say/hi.js
module.exports = async (name, age) => {
return {
msg: `Hi, I'm ${name}, ${age} years old.`,
name,
age,
};
};
Create lib/about.js
module.exports = async () => {
return `Microservices #1`;
};
Run
node index.js
# Service s1 is running...
2. Create Microservices #2
Open a new tab in terminal, initialize a new directory for service2
# relative to path service1
mkdir ../service2 && cd ../service2 && npm init -y
Install Rams Pro in service2
npm install rams-pro --save
Create index.js
const rams = require('rams-pro');
const options = {
rabbitMQ: {
host: 'localhost',
},
redis: {
host: 'localhost',
password: 'ramspro123',
}
};
rams.initServer('s2', './lib', options);
Create lib/about.js
module.exports = async () => {
return `Microservices #2`;
};
Run
node index.js
# Service s2 is running...
3. Call Microservices
Open a new tab in terminal, initialize a new directory for client
# relative to path service2
mkdir ../client && cd ../client && npm init -y
Install Rams Pro in client
npm install rams-pro --save
Create lib/ramsServices.js
const rams = require('rams-pro');
const options = {
rabbitMQ: {
host: 'localhost',
},
redis: {
host: 'localhost',
password: 'ramspro123',
}
};
const services = rams.initServices(options);
module.exports = services;
Create index.js
const services = require('./lib/ramsServices');
const main = async () => {
// Get services by names 's1', 's2'.
// If the names are omitted, all services will be returned
const {s1, s2} = await services('s1', 's2');
let result;
result = await s1.about();
console.log(result); // "Microservices #1"
result = await s2.about();
console.log(result); // "Microservices #2"
result = await s1.say.hi('owen', 100);
console.log(result); // {msg: 'Hi, I\'m owen, 100 years old.', name: 'owen', age: 100}
};
main();
Run
node index.js
# Microservices #1
# Microservices #2
# { msg: 'Hi, I\'m owen, 100 years old.', name: 'owen', age: 100 }
Example
1. Run example:
Download this repo first (if not yet):
git clone https://github.com/hiowenluke/rams-pro.git
cd rams-pro
npm install
1) Open a new tab in terminal for example service1, then do the following:
# relative to path rams-pro
cd ./example/service1
node index.js
# Service s1 is running...
2) Open a new tab in terminal for example service2, then do the following:
# relative to path service1
cd ../../example/service2
node index.js
# Service s2 is running...
3) Open a new tab in terminal for example client-pro, then do the following:
# relative to path service2
cd ../../example/client-pro
node index.js
# Microservices #1
# Microservices #2
# { msg: 'Hi, I\'m owen, 100 years old.', name: 'owen', age: 100 }
2. See files in directory example to learn more (same as Quick Start).
Use Rams Pro as Rams
License
Copyright (c) 2019, Owen Luke