- 基于js的node-zookeeper-dubbo 和egg实现远程连接服务
const nzd = require('node-zookeeper-dubbo'); const opt={ application:{name:'dubbo-consumer'},//前端充当dubbo服务的消费者 registry:'****:2181', dubboVer:'****', dependencies:{ //此处的接口也可以分离出去 直接引入service接口 Foo:{ interface:'cn.wangsr.api.demoService',//后端接口 version:'1.0.0', timeout:5000, methodSignature: {//后端方法区 getUserInfo : (Id) => [ {'$class': 'java.lang.Long', '$': id} ],//参数类型按照java后台参数类型全类名 } } } } opt.java = require('js-to-java') const Dubbo=new nzd(opt); const Service = require('egg').Service; class TestDubboService extends Service{ async getUserInfo(Id){//此处暴露接口 用于controller层调用 return Dubbo.Foo.getUserInfo(10020)//为了测试直接在此处带参 } } module.exports = TestDubboService;
- controller中调用:
async main() { const { ctx } = this; const result = await this.ctx.service.index.getUserInfo();//此处省略参数 因为在前面已经提前输入 this.ctx.body =result ; }