dubbo2.js基于 Node.js 的 RPC 客户端
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' } })();
评论