All Projects → eggjs → Egg Sofa Rpc

eggjs / Egg Sofa Rpc

Licence: mit
SOFARPC plugin for egg

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Egg Sofa Rpc

Egg Oauth2 Server
🌟 OAuth2 server plugin for egg.js based on node-oauth2-server
Stars: ✭ 174 (+145.07%)
Mutual labels:  eggjs, egg-plugin
Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (-29.58%)
Mutual labels:  eggjs, egg-plugin
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+164.79%)
Mutual labels:  eggjs, egg-plugin
Egg Router Plus
The missing router feature for eggjs
Stars: ✭ 117 (+64.79%)
Mutual labels:  eggjs, egg-plugin
Egg Cancan
cancancan like authorization plugin for Egg.js
Stars: ✭ 47 (-33.8%)
Mutual labels:  eggjs, egg-plugin
Egg Mp
EggJS插件:微信公众平台基本服务
Stars: ✭ 153 (+115.49%)
Mutual labels:  eggjs, egg-plugin
egg-sentry
Sentry Plugin For Egg.js
Stars: ✭ 18 (-74.65%)
Mutual labels:  egg-plugin, eggjs
Sofa Bolt Node
The Node.js implementation of the SOFABolt protocol
Stars: ✭ 142 (+100%)
Mutual labels:  eggjs, sofastack
egg-logger-sls
Logger transport for aliyun sls.
Stars: ✭ 14 (-80.28%)
Mutual labels:  egg-plugin, eggjs
egg-parameters
Merge all parameters (ctx.params, ctx.request.query, ctx.request.body) into ctx.params like Rails application.
Stars: ✭ 24 (-66.2%)
Mutual labels:  egg-plugin, eggjs
egg-rbac
Role Based Access Control for eggjs
Stars: ✭ 32 (-54.93%)
Mutual labels:  egg-plugin, eggjs
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+594.37%)
Mutual labels:  eggjs, egg-plugin
egg-cache
💾 Cache plugin for eggjs
Stars: ✭ 60 (-15.49%)
Mutual labels:  egg-plugin, eggjs
Egg Graphql
Stars: ✭ 345 (+385.92%)
Mutual labels:  eggjs, egg-plugin
Egg Mongo Native
MongoDB egg.js plugin using native driver.
Stars: ✭ 69 (-2.82%)
Mutual labels:  eggjs, egg-plugin
Weapp Vue Eggjs Shop Demo
商城、商店批发或零售,pc管理端 + 微信小程序 + 后端服务
Stars: ✭ 604 (+750.7%)
Mutual labels:  eggjs
Sofa Ark
SOFAArk is a light-weight,java based classloader isolation framework.
Stars: ✭ 990 (+1294.37%)
Mutual labels:  sofastack
Egg Sequelize
Sequelize for Egg.js
Stars: ✭ 540 (+660.56%)
Mutual labels:  egg-plugin
Egg Restfulapi
🏅 基于Egg.js 2.0 & {mongoose,jwt}RESTful API 模板,用于快速集成开发RESTful前后端分离的服务端。
Stars: ✭ 524 (+638.03%)
Mutual labels:  egg-plugin
Egg View Ejs
egg view plugin for ejs.
Stars: ✭ 54 (-23.94%)
Mutual labels:  egg-plugin

egg-sofa-rpc

NPM version build status Test coverage David deps Known Vulnerabilities npm download

SOFARPC plugin for egg framework

Install

$ npm i egg-sofa-rpc --save

Usage

Enable the plugin

Change ${app_root}/config/plugin.js to enable SOFARPC plugin:

exports.sofaRpc = {
  enable: true,
  package: 'egg-sofa-rpc',
};

RPC Service Discovery

By default, We use zookeeper for service discovery. Therefore you need to configure the zk address:

// ${app_root}/config/config.${env}.js
config.rpc = {
  registry: {
    address: '127.0.0.1:2181', // configure your real zk address
  },
};

We plan to provide more implementations of service discovery. And also you can implement it by yourself, you can follow this article

RPC Client

By using the plugin, you can call rpc services provided by other system.

1. Get the RPC Interface Definition

We use protobuf interface definition lanaguge to describe the RPC service. So you need to get the *.proto files and put them into ${app_root}/proto folder

2. Global RPC Client Configuration

Configure RPC Client information in ${app_root}/config/config.{env}.js:

// ${app_root}config/config.${env}.js
exports.rpc = {
  client: {
    responseTimeout: 3000,
  },
};
  • responseTimeout(optional): RPC timeout in milliseconds, default value is 3000

3. Configure the Interface in proxy.js

${app_root}/config/proxy.js is a very important config file for rpc client, you should configure the services you needed, then executing the egg-rpc-generator tool to generate the proxy files.

Let's see a simple example of proxy.js. It declare a interface named: com.alipay.sofa.rpc.test.ProtoService provided by sofarpc application

'use strict';

module.exports = {
  services: [{
    appName: 'sofarpc',
    api: {
      ProtoService: 'com.alipay.sofa.rpc.test.ProtoService',
    },
  }],
};

Refer this acticle for more details

4. Generate the Proxy

Run egg-rpc-generator to generate the proxy files. After running success, it will generate a file: ProtoService.js under ${app_root}/app/proxy

$ egg-rpc-generator

[EggRpcGenerator] framework: /egg-rpc-demo/node_modules/egg, baseDir: /egg-rpc-demo
[ProtoRPCPlugin] found "com.alipay.sofa.rpc.test.ProtoService" in proto file
[ProtoRPCPlugin] save all proto info into "/egg-rpc-demo/run/proto.json"

5. Call the Service

You can call the RPC service by using ctx.proxy.proxyName. The proxyName is key value of api object you configure in proxy.js. In our example, it's ProtoService, and proxyName using lower camelcase, so it's ctx.proxy.protoService

'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const res = await ctx.proxy.protoService.echoObj({
      name: 'gxcsoccer',
	    group: 'A',
    });
    ctx.body = res;
  }
}

module.exports = HomeController;

As above, you can call remote service as a local method.

RPC Server

By using the plugin, you also can publish your own RPC service to other system.

1. Define the RPC Interface

Writing the *.proto files, and put them into ${app_root}/proto folder

# ProtoService.proto
syntax = "proto3";

package com.alipay.sofa.rpc.protobuf;
option java_multiple_files = true; // 可选
option java_outer_classname = "ProtoServiceModels"; // 可选

service ProtoService {
    rpc echoObj (EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
    string name = 1;
    Group group = 2;
}

message EchoResponse {
    int32 code = 1;
    string message = 2;
}

enum Group {
    A = 0;
    B = 1;
}

2. Global RPC Server Configuration

Configure RPC Server information in ${app_root}/config/config.{env}.js:

module.exports = {
	rpc: {
    server: {
      namespace: 'com.nodejs.rpc',
    },
	},
},
  • namespace(required): the default namespace of all rpc service
  • selfPublish(optional): whether every node process publish service independent, default is true
  • port(optional): the TCP port will be listen on
  • maxIdleTime(optional): server will disconnect the socket if idle for long time
  • responseTimeout(optional): Number of milliseconds to wait for a response to begin arriving back from the remote system after sending a request
  • codecType(optional): recommended serialization method,default is protobuf

3. Implemenation the RPC Interface

Put your implementation code under ${app_root}/app/rpc folder

// ${app_root}/app/rpc/ProtoService.js
exports.echoObj = async function(req) {
  return {
    code: 200,
    message: 'hello ' + req.name + ', you are in ' + req.group,
  };
};

4. RPC Unittest in Eggjs

'use strict';

const mm = require('egg-mock');

describe('test/index.test.js', () => {
  let app;
  before(async function() {
    app = mm.app({
      baseDir: 'apps/rpcserver',
    });
    await app.ready();
  });
  after(async function() {
    await app.close();
  });

  it('should invoke HelloService', done => {
    app.rpcRequest('com.alipay.nodejs.HelloService')
      .invoke('hello')
      .send([ 'gxcsoccer' ])
      .expect('hello gxcsoccer', done);
  });
});

For more details of app.rpcRequest, you can refer to this acticle

Docs

How to Contribute

Please let us know how can we help. Do check out issues for bug reports or suggestions first.

To become a contributor, please follow our contributing guide.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].