npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-dubbo-rpc

v1.2.1

Published

dubbo rpc plugin for egg

Downloads

237

Readme

egg-rpc-for-apache-dubbo

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

Dubbo RPC plugin for Egg.js

Install

$ npm i egg-rpc-for-apache-dubbo --save

Usage

1. Enable the Plugin

enable egg-rpc-for-apache-dubbo plugin in ${app_root}/config/plugin.js:

// {app_root}/config/plugin.js
exports.dubboRpc = {
  enable: true,
  package: 'egg-rpc-for-apache-dubbo',
};

2. Configuration

// @example
exports.rpc = {
  registry: {
    address: '127.0.0.1:2181', // configure your real zk address
  },
  client: {
    responseTimeout: 3000,
  },
  server: {
    namespace: 'org.eggjs.rpc.test',
    port: 12200,
    maxIdleTime: 90 * 1000,
    codecType: 'hessian2',
    selfPublish: true,
    version: '1.0.0',
    group: 'DUBBO',
    autoServe: true,
  },
};

all configuations is under rpc property

  • registry (we use zookeeper as service registry in dubbo)
    • address:(required) the zookeeper address
  • client
    • responseTimeout:(optional) number of milliseconds to wait for a response, if timeout will get an exception, the default value is 3000(ms)
  • server
    • namespace:(required) the default namespace to publish all services
    • port:(optional) the port which RPC server listening on, the default value is 12200
    • maxIdleTime:(optional) maximum idle time (in milliseconds) for a connection
    • codecType:(optional) the serialization type, default value is hessian2
    • selfPublish:(optional) if set to true (default), every worker process will listen on different ports
    • version:(optional) the service version, default value is 1.0.0
    • group:(optional) the service group, default value is DUBBO
    • autoServe:(optional) if set to true (default), will launce Dubbo RPC server automatically

3. Call Dubbo Services as Consumer

Configure the Interface in proxy.js

First, you need to put the JAR file (which contains the API interfaces) into {app_root}/assembly folder.

And then you need to config $app_root/config/proxy.js, which 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: org.eggjs.dubbo.UserService provided by dubbo application

'use strict';

module.exports = {
  group: 'HSF',
  version: '1.0.0',
  services: [{
    appName: 'dubbo',
    api: {
      UserService: {
        interfaceName: 'org.eggjs.dubbo.UserService',
      },
    },
    dependency: [{
      groupId: 'eggjs',
      artifactId: 'dubbo-demo-api',
      version: '1.0-SNAPSHOT',
    }],
  }],
};

details as follows:

  • version:(optional) service version, the global config
  • group:(optional) service group
  • errorAsNull:(optional) if set true, we are returning null instead of throwing an exception while error appears
  • services:(required) RPC services configuation
    • appName:(required) the name of RPC provider
    • api:(required) API details
      • interfaceName:(required) interface name
      • version:(optional) service version, it will overwrite the global one
      • group:(optional) service group, it will overwrite the global one
    • dependency:(required) like Maven pom config
      • groupId:(required) uniquely identifies your project across all projects
      • artifactId:(required) the name of the jar without version
      • version:(required) the jar version

Generate the Proxy

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

install egg-rpc-generator

$ npm i egg-rpc-generator --save-dev

add rpc command into scripts of package.json

{
  "scripts": {
    "rpc": "egg-rpc-generator"
  },
}

execute the rpc command

$ npm run rpc

Call Dubbo Service

You can call the Dubbo 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 UserService, and proxyName using lower camelcase, so it's ctx.proxy.userService

'use strict';

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

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const result = await ctx.proxy.userService.echoUser({
      id: 123456,
      name: 'gxcsoccer',
      address: 'Space C',
      salary: 100000000,
    });
    ctx.body = result;
  }
}

module.exports = HomeController;

Unittest of RPC Client in Egg.js

you can use app.mockProxy to mock the RPC interface

'use strict';

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

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

  it('should app.mockProxy ok', async function() {
    app.mockProxy('DemoService', 'sayHello', async function(name) {
      return 'hello ' + name + ' from mock';
    });

    const ctx = app.createAnonymousContext();
    const res = await ctx.proxy.demoService.sayHello('gxcsoccer');
    assert(res === 'hello gxcsoccer from mock');
  });
});

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

4. Expose Dubbo Services as Provider

Define the RPC Interface

create a JAR file that contains the API interface

Implemenation the RPC Interface

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

// ${app_root}/app/rpc/UserService.js
exports.echoUser = async function(user) {
  return user;
};

exports.interfaceName = 'org.eggjs.dubbo.UserService';
exports.version = '1.0.0';
exports.group = 'DUBBO';

Unittest of your RPC Server in Egg.js

'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('org.eggjs.dubbo.UserService')
      .invoke('echoUser')
      .send([{
        id: 123456,
        name: 'gxcsoccer',
        address: 'Space C',
        salary: 100000000,
      }])
      .expect({
        id: 123456,
        name: 'gxcsoccer',
        address: 'Space C',
        salary: 100000000,
      }, done);
  });
});

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

Reference List

Questions & Suggestions

Please open an issue here.

License

Apache License V2