Gathering detailed insights and metrics for egg-rpc-for-apache-dubbo
Gathering detailed insights and metrics for egg-rpc-for-apache-dubbo
Gathering detailed insights and metrics for egg-rpc-for-apache-dubbo
Gathering detailed insights and metrics for egg-rpc-for-apache-dubbo
npm install egg-rpc-for-apache-dubbo
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (97.95%)
Shell (2.05%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
40 Stars
19 Commits
2 Forks
33 Watchers
1 Branches
24 Contributors
Updated on Sep 10, 2024
Latest Version
1.0.0
Package Id
egg-rpc-for-apache-dubbo@1.0.0
Unpacked Size
30.36 kB
Size
9.45 kB
File Count
7
NPM Version
6.9.0
Node Version
8.4.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Dubbo RPC plugin for Egg.js
1$ npm i egg-rpc-for-apache-dubbo --save
enable egg-rpc-for-apache-dubbo plugin in ${app_root}/config/plugin.js:
1// {app_root}/config/plugin.js 2exports.dubboRpc = { 3 enable: true, 4 package: 'egg-rpc-for-apache-dubbo', 5};
1// @example 2exports.rpc = { 3 registry: { 4 address: '127.0.0.1:2181', // configure your real zk address 5 }, 6 client: { 7 responseTimeout: 3000, 8 }, 9 server: { 10 namespace: 'org.eggjs.rpc.test', 11 port: 12200, 12 maxIdleTime: 90 * 1000, 13 codecType: 'hessian2', 14 selfPublish: true, 15 version: '1.0.0', 16 group: 'DUBBO', 17 autoServe: true, 18 }, 19};
all configuations is under rpc
property
address:(required)
the zookeeper addressresponseTimeout:(optional)
number of milliseconds to wait for a response, if timeout will get an exception, the default value is 3000(ms)namespace:(required)
the default namespace to publish all servicesport:(optional)
the port which RPC server listening on, the default value is 12200maxIdleTime:(optional)
maximum idle time (in milliseconds) for a connectioncodecType:(optional)
the serialization type, default value is hessian2selfPublish:(optional)
if set to true (default), every worker process will listen on different portsversion:(optional)
the service version, default value is 1.0.0group:(optional)
the service group, default value is DUBBOautoServe:(optional)
if set to true (default), will launce Dubbo RPC server automaticallyFirst, 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
1'use strict'; 2 3module.exports = { 4 group: 'HSF', 5 version: '1.0.0', 6 services: [{ 7 appName: 'dubbo', 8 api: { 9 UserService: { 10 interfaceName: 'org.eggjs.dubbo.UserService', 11 }, 12 }, 13 dependency: [{ 14 groupId: 'eggjs', 15 artifactId: 'dubbo-demo-api', 16 version: '1.0-SNAPSHOT', 17 }], 18 }], 19};
details as follows:
version:(optional)
service version, the global configgroup:(optional)
service grouperrorAsNull:(optional)
if set true, we are returning null instead of throwing an exception while error appearsservices:(required)
RPC services configuation
appName:(required)
the name of RPC providerapi:(required)
API details
interfaceName:(required)
interface nameversion:(optional)
service version, it will overwrite the global onegroup:(optional)
service group, it will overwrite the global onedependency:(required)
like Maven pom config
groupId:(required)
uniquely identifies your project across all projectsartifactId:(required)
the name of the jar without versionversion:(required)
the jar versionRun 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
1$ npm i egg-rpc-generator --save-dev
add rpc command into scripts of package.json
1{ 2 "scripts": { 3 "rpc": "egg-rpc-generator" 4 }, 5}
execute the rpc command
1$ npm run rpc
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
1'use strict'; 2 3const Controller = require('egg').Controller; 4 5class HomeController extends Controller { 6 async index() { 7 const { ctx } = this; 8 const result = await ctx.proxy.userService.echoUser({ 9 id: 123456, 10 name: 'gxcsoccer', 11 address: 'Space C', 12 salary: 100000000, 13 }); 14 ctx.body = result; 15 } 16} 17 18module.exports = HomeController;
you can use app.mockProxy
to mock the RPC interface
1'use strict'; 2 3const mm = require('egg-mock'); 4const assert = require('assert'); 5 6describe('test/mock.test.js', () => { 7 let app; 8 before(async function() { 9 app = mm.app({ 10 baseDir: 'apps/mock', 11 }); 12 await app.ready(); 13 }); 14 afterEach(mm.restore); 15 after(async function() { 16 await app.close(); 17 }); 18 19 it('should app.mockProxy ok', async function() { 20 app.mockProxy('DemoService', 'sayHello', async function(name) { 21 return 'hello ' + name + ' from mock'; 22 }); 23 24 const ctx = app.createAnonymousContext(); 25 const res = await ctx.proxy.demoService.sayHello('gxcsoccer'); 26 assert(res === 'hello gxcsoccer from mock'); 27 }); 28});
As above, you can call remote service as a local method.
create a JAR file that contains the API interface
Put your implementation code under ${app_root}/app/rpc
folder
1// ${app_root}/app/rpc/UserService.js 2exports.echoUser = async function(user) { 3 return user; 4}; 5 6exports.interfaceName = 'org.eggjs.dubbo.UserService'; 7exports.version = '1.0.0'; 8exports.group = 'DUBBO';
1'use strict'; 2 3const mm = require('egg-mock'); 4 5describe('test/index.test.js', () => { 6 let app; 7 before(async function() { 8 app = mm.app({ 9 baseDir: 'apps/rpcserver', 10 }); 11 await app.ready(); 12 }); 13 after(async function() { 14 await app.close(); 15 }); 16 17 it('should invoke HelloService', done => { 18 app.rpcRequest('org.eggjs.dubbo.UserService') 19 .invoke('echoUser') 20 .send([{ 21 id: 123456, 22 name: 'gxcsoccer', 23 address: 'Space C', 24 salary: 100000000, 25 }]) 26 .expect({ 27 id: 123456, 28 name: 'gxcsoccer', 29 address: 'Space C', 30 salary: 100000000, 31 }, done); 32 }); 33});
For more details of app.rpcRequest, you can refer to this acticle
Please open an issue here.
No vulnerabilities found.
No security vulnerabilities found.