sonorpc

Nodejs RPC Github address

Introduction to the

Sonorpc is a lightweight, high-performance Node RPC framework consisting of Registry, Provider, and Consumer that provides remote method invocation, load balancing, automatic service registration, and discovery.

why

Why did we choose to develop SonorPC instead of using Dubbo and HTTP directly?

  1. Doubbo does not provide a JS version of provider, and our main technology stack is NodeJS.
  2. HTTP (S), JSON PRC performance is not good enough, we use byte transfer and connection pooling to ensure performance.
  3. We weren’t large enough to use ZooKeeper (adding complexity to the system), so we developed our own registry to do load balancing.

How to use

npm install sonorpc

import { createProvider } from "sonorpc"
Copy the code

Provider Service Provider

Create a service

DemoService.js

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

class DemoService extends Service {
    sayHello(arg1, arg2) {
        return {
            success: true.data: ['any data']}}}Copy the code

Starting the Provider Service

scripts/start.js

const { createProvider } = requie("sonorpc");
const provider = createProvider({
    // Example logging class
    logger: console.// Listen on the port
    port: 3005./ / service class
    serviceClasses: [DemoService],
    // Registry configuration
    registry: {
        // Registry address
        host: '127.0.0.1'.// Registry port
        port: 3006}}); provider.start((a)= > {
    console.log('Service started');
});
Copy the code

Registry

scripts/registry.js

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

startRegistry({
    port: 3006
});
Copy the code

Consumer services

Create consumers

consumer.js

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

const consumer = registerConsumer({
    port: 3006
});

module.exports = consumer;
Copy the code

Call the service

DemoService.js

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

class DemoService { testMe(... args) {return new Promise((resolve, reject) = > consumer.invoke('demo.testMe', args, (err, data) => { err ? reject(err) : resolve(data); })); }}Copy the code