Skip to content

sorrymeika/sonorpc

Repository files navigation

sonorpc

nodejs rpc

简介

sonorpc是一个轻量级、高性能的node rpc框架,由Registry(注册中心)、Provider(服务提供者)、Consumer(服务消费者)组成,提供远程方法调用、负载均衡、服务自动注册和发现功能。

如何使用

npm install sonorpc

Provider服务提供者

创建服务

app/config.js

exports.mysql = {
    host: '',
    user: '',
    password: '',
    database: '',
};

app/service/demo.js

const { Service } = requie("sonorpc");

class DemoService extends Service {
    async sayHello(arg1, arg2) {
        const result = await this.app.mysql.query('select * from user');
        return {
            success: true,
            data: result
        };
    }
}

启动Provider服务

scripts/start.js

require('sonorpc').startProvider();

Registry注册中心

scripts/registry.js

const { startRegistry } = require('sonorpc');

startRegistry({
    port: 3006
});

Consumer服务消费者

创建消费者

consumer.js

const { registerConsumer } = require('sonorpc');

const consumer = registerConsumer({
    // 服务提供者名称
    providerName: 'user',
    registry: {
        port: 3006
    }
});

module.exports = consumer;

调用服务

service/DemoService.js

const consumer = require('../consumer');

class DemoService {
    testMe(...args) {
        return consumer.invoke('demo.testMe', args);
    }
}

About

nodejs rpc by sunlu

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published