node調用dubbo接口

node-zookeeper-dubbo

一,主要過程和必要參數

1,生成node-zookeeper-dubbo實例

  • register:zookeeper的註冊地址
  • dubboVer: 後端dubbo服務用的版本
  • dependencies:自定義若干接口名java

    • interface:真實的dubbo接口名
    • methodSignature:真實的dubbo接口名下的方法名以及對應參數

2,node-zookeeper-dubbo實例調用dubbo方法

1,本身建立http請求node

2,http請求裏調用dubbo方法(注意參數的定義格式和要傳入java定義參數的model類名)git

二,代碼

node-dubbo.jsgithub

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.jsonexpress

{
  "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/fooapache

四,bug

1,安裝node-zookeeper-dubbo的3.0.0及如下版本會報錯,初始化不成功json

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...api

六,待研究

dubbo-js
https://github.com/apache/dub...瀏覽器

相關文章
相關標籤/搜索