0.3.16 • Published 4 years ago

rams v0.3.16

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Rams

A light and easy-to-use microservices framework for Node.js. Rams loads a directory as a microservice, so that the client can calls remote functions in it with Object Style like service1.do.say.hi('Hello world!') or Message Style like call('service1:/do/say/hi', 'Hello world!') and gets the results immediately.

Rams is based on RabbitMQ and Redis. If you just need Message Style only, try the lite version Rams Call.

Server Environment

1. Install Docker (Docker CE recommended)

2. Install RabbitMQ in Docker
1) Install: docker pull rabbitmq:management
2) Start: docker run --restart=always --name rabbitmq -d -p 5672:5672 -p 15672:15672 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 --name redis -d -p 6379:6379 -it redis redis-server

Install

npm install rams --save

Test

Download this repo

git clone https://github.com/hiowenluke/rams.git
cd rams
npm install

Test

npm test

TRY IT!

To run this demo, download this repo first if not yet (see above).

1. Run microservices

1) Open a new tab in terminal, then:

node ./example/service1
# Service s1 is running...

2) Open a new tab in terminal, then:

node ./example/service2
# Service s2 is running...

2. Run client

1) Open a new tab in terminal, then:

node ./example/client-with-object-style

# Microservices #1
# Microservices #2
# { msg: 'Hi, I\'m owen, 100 years old.' }

Press Ctrl + C twice to quit this demo.

2) Open a new tab in terminal, then:

node ./example/client-with-message-style

# Microservices #1
# Microservices #2
# { msg: 'Hi, I\'m owen, 100 years old.' }

Press Ctrl + C twice to quit this demo.

Usage

1. Create functions in directory "./src" in server project, such as below:

module.exports = async (name, age) => {
    return {msg: `Hi, I'm ${name}, ${age} years old.`};
};
module.exports = async () => {
    return `Microservices #1`;
};

2. Load the directory "./src" as a microservice named "s1" in index.js.

// "s1"
//      The name of this microservice.
//      It can be omitted if you have only one microservice.

// "./src"
//      The root folder name of business files such as "src/say/hi.js".
//      It can be omitted or replaced with other names such as "./biz", "./src", etc.
//      It should be started with ".".

require('rams').initServer('s1', './src');

3. Client: call the remote functions with Object Style

const services = require('rams').initServices();

const main = async () => {

    // Get services by names 's1', 's2', ..., etc.
    // If the names are omitted, all services will be returned
    const {s1} = await services('s1');

    let result;
    result = await s1.about();
    console.log(result); // "Microservices #1"

    result = await s1.say.hi('owen', 100);
    console.log(result); // {msg: 'Hi, I\'m owen, 100 years old.'}
};

main();

4. Client: call the remote functions with Message Style:

const call = require('rams').initClient();

const main = async () => {
    let result;

    result = await call('s1:/about');
    console.log(result); // "Microservices #1"

    result = await call('s1:/say/hi', 'owen', 100);
    console.log(result); // {msg: 'Hi, I\'m owen, 100 years old.'}
};

main();

Options

const options = {

    // Specify the rabbitMQ options.
    // It can be omitted if the host is "localhost".
    rabbitMQ: {
        host: 'localhost',      // RabbitMQ host
    },
    
    // Specify the Redis options.
    // It can be omitted if it is the default options (like below) of Redis.
    redis: {
        host: 'localhost',      // Redis host
        // port: '6379',           // Redis port
        // password: 'auth',       // Redis password
        // db: 0,                  // Redis database number
        // family: 4,              // 4 (IPv4) or 6 (IPv6)
    },
};

For initializing server:

require('rams').initServer('s1', './src', options);

For initializing client with Object Style:

require('rams').initServices(options);

For initializing client with Message Style:

require('rams').initClient(options);

Example

See files in directory example to learn more.

License

MIT

Copyright (c) 2019, Owen Luke

0.3.16

4 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.8

4 years ago

0.1.6

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago