nodejs调用dubbo接口

童琪
2023-12-01

1. apache-dubbo-js

node调用dubbo,使用工具库:apache-dubbo-js
代码地址:https://github.com/apache/dubbo-js
npm安装

npm install apache-dubbo-js

2. 新建一个service

import { Dubbo } from 'dubbo-js';

const demoProvider = dubbo =>
  dubbo.proxyService({
  	// 包路径
    dubboInterface: 'org.apache.dubbo.demo.DemoProvider',
    // 包版本
    version: '1.0.0',
    // 包接口
    methods: {
      sayHello(name, age) {
      	// 参数转java类型
        return [java.String(name), java.Integer(name)];
      },
      getUserInfo() {
        // DTO参数
        return [
          java.combine('org.apache.dubbo.demo.DTO', {
            id: 1,
            name: 'nodejs',
            email: 'node@test.com',
          }),
        ];
      },
    },
  });

module.exports = {
  demoProvider,
};

3. 创建dubbo

import { Dubbo } from 'dubbo-js';
// 引入service
import service from './service';
const dubbo = new Dubbo({
  application: { name: 'dubbo-js' },
  // zookeeper 地址
  register: 'localhost:2181',
  service,
});

4. 调用方法

返回的是promise

dubbo.service.DemoProvider.sayHello('node', 23);

dubbo.service.DemoProvider.getUserInfo())
 类似资料: