基于 Node.js 的 RPC 客户端 dubbo2.js

码农软件 · 软件分类 · RPC/XMLRPC项目 · 2019-04-11 06:12:56

软件介绍

dubbo2.js —— nodejs 使用原生的 dubbo (dubbo head + hessian body) 协议打通了 dubbo 的 rpc 方法调用。

功能特性

  • 支持将 zookeeper 作为注册中心

  • 使用 TCP Dubbo 原生协议(Dubbo Header + Hessian Body)

  • Socket 池 (ServerAgent -> SocketPool -> SocketWorker)

  • 直接支持 Dubbo (const Dubbo = DirectlyDubbo({..}))

  • 中间件,易于扩展

  • 追踪

  • 支持 Dubbox

示例代码

import {Dubbo, java, TDubboCallResult} from 'dubbo2.js';
//定义dubbo方法类型接口
//方便代码自动提示
interface IDemoService {
  sayHello(name: string): TDubboCallResult<string>;
  echo(): TDubboCallResult<string>;
  test(): TDubboCallResult<void>;
  getUserInfo(): TDubboCallResult<{
    status: string;
    info: {id: number; name: string};
  }>;
}
//创建dubbo对象
const dubbo = new Dubbo({
  application: {name: 'node-dubbo'},
  //zookeeper address
  register: 'localhost:2181',
  dubboVersion: '2.0.0',
  interfaces: ['com.alibaba.dubbo.demo.DemoService'],
});
//代理本地对象->dubbo对象
const demoService = dubbo.proxyService<IDemoService>({
  dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
  version: '1.0.0',
  methods: {
    sayHello(name) {
      //仅仅做参数hessian化转换
      return [java.String(name)];
    },
    echo() {},
    test() {},
    getUserInfo() {
      //仅仅做参数hessian化转换
      return [
        java.combine('com.alibaba.dubbo.demo.UserRequest', {
          id: 1,
          name: 'nodejs',
          email: 'node@qianmi.com',
        }),
      ];
    },
  },
});
//main method
(async () => {
  const result1 = await demoService.sayHello('node');
  //print {err: null, res:'hello node from dubbo service'}
  const res = await demoService.echo();
  //print {err: null, res: 'pang'}
  const res = await demoService.getUserInfo();
  //status: 'ok', info: { id: '1', name: 'test' }
})();

本文地址:https://www.codercto.com/soft/d/3317.html

Once Upon an Algorithm

Once Upon an Algorithm

Martin Erwig / MIT Press / 2017-9-8 / GBP 22.95

How Hansel and Gretel, Sherlock Holmes, the movie Groundhog Day, Harry Potter, and other familiar stories illustrate the concepts of computing. Picture a computer scientist, staring at a screen and......一起来看看 《Once Upon an Algorithm》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具