node调用dubbo接口

node-zookeeper-dubbo

一,主要过程和必要参数

1,生成node-zookeeper-dubbo实例

  • register:zookeeper的注册地址
  • dubboVer: 后端dubbo服务用的版本
  • dependencies:自定义若干接口名

    • interface:真实的dubbo接口名
    • methodSignature:真实的dubbo接口名下的方法名以及对应参数

2,node-zookeeper-dubbo实例调用dubbo方法

1,自己创建http请求

2,http请求里调用dubbo方法(注意参数的定义格式和要传入java定义参数的model类名)

二,代码

node-dubbo.js

const ZookeeperDubbo = require('node-zookeeper-dubbo');
const app=require('express')();
var java = require('js-to-java');

const Dubbo = new ZookeeperDubbo({
  application: { name: 'provider' },
  registry: '172.29.8.100:2181',
  dubboVer: '2.8.4',
  root: 'dubbo',
  dependencies: {
    IUserApi: {
      interface: 'com.xxx.api',
      timeout: 6000,
      methodSignature: {
        getUserLists : (Obj) => [{$class: 'com.xxx.model',$:Obj}],
      }  
    }
  }
})

app.get('/foo',(req,res)=>{

  const customerObj = {
    $class: 'com.xxx.model',
    $: {
      userName: java.String('test'),
      uin: java.String('1000000000000')
    }
  };

  Dubbo.IUserApi
    .getUserLists(customerObj)
    .then(data=> {
      console.log('data---------'+data);
      res.send(data)
    })
    .catch(err=>{
      console.log('err---------'+err);
      res.send(err)
    })

})


app.listen(9090)

package.json

{
  "name": "dubbo",
  "version": "1.0.0",
  "description": "",
  "main": "node-dubbo.js",
  "dependencies": {
    "express": "^4.16.4",
    "js-to-java": "^2.6.1",
    "node-zookeeper-dubbo": "^3.0.15"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

三,运行

1,node node-dubbo.js(会提示初始化成功 Dubbo service init done)
2,浏览器运行自己创建的接口(成功会返回dubbo对应接口的方法的返回值)
http://localhost:9090/foo

四,bug

1,安装node-zookeeper-dubbo的3.0.0及以下版本会报错,初始化不成功

2,Fail to decode request due to: RpcInvocation
注释掉node-zookeeper-dubbo包中libs下的encode.js文件中以下代码(针对dubbox的东西,但后端用的是dubbo)

// if (ver.startsWith("2.8")) {
//   body.write(-1); //for dubbox 2.8.X
// }

五,git

https://github.com/omnip620/n...
https://github.com/omnip620/n...

六,待研究

dubbo-js
https://github.com/apache/dub...

你可能感兴趣的:(node.js,dubbo,dubbo-zookeeper)