alipay/sofa-bolt-node

Name: sofa-bolt-node

Owner: Alipay

Description: The Node.js implementation of the SOFABolt protocol

Created: 2018-05-22 08:18:06.0

Updated: 2018-05-24 08:07:12.0

Pushed: 2018-05-23 13:53:20.0

Homepage:

Size: 53

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

sofa-bolt-node

Bolt ?? Nodejs ????

NPM version build status Test coverage David deps Known Vulnerabilities npm download

????

SOFABoltNode ? SOFABolt ? Nodejs ??????? Bolt ?????????? RPC ????????? Java ??????????????????????????????????????????????? RPC ??????

??Bolt ???????

Bolt ?????????????????????????????

V1 ??

est command protocol for v1
  1     2           4           6           8          10           12          14         16
--+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
to| type| cmdcode   |ver2 |   requestId           |codec|        timeout        |  classLen |
--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
derLen  | contentLen            |                             ... ...                       |
--------+-----------+-----------+                                                                                               +
            className + header  + content  bytes                                            |
                                                                                            +
                            ... ...                                                         |
--------------------------------------------------------------------------------------------+

onse command protocol for v1
  1     2     3     4           6           8          10           12          14         16
--+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
to| type| cmdcode   |ver2 |   requestId           |codec|respstatus |  classLen |headerLen  |
--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
ntentLen            |                  ... ...                                              |
--------------------+                                                                       +
                       header  + content  bytes                                             |
                                                                                            +
                            ... ...                                                         |
--------------------------------------------------------------------------------------------+

V2 ??

est command protocol for v2
  1     2           4           6           8          10     11     12          14         16
--+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+
to| ver1|type | cmdcode   |ver2 |   requestId           |codec|switch|   timeout             |
--------+-----------+-----------+-----------+-----------+------------+-----------+-----------+
ssLen   |headerLen  |contentLen             |           ...                                  |
--------+-----------+-----------+-----------+                                                +
            className + header  + content  bytes                                             |
                                                                                             +
                            ... ...                                  | CRC32(optional)       |
---------------------------------------------------------------------------------------------+

onse command protocol for v2
  1     2     3     4           6           8          10     11    12          14          16
--+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+
to| ver1| type| cmdcode   |ver2 |   requestId           |codec|switch|respstatus |  classLen |
--------+-----------+-----------+-----------+-----------+------------+-----------+-----------+
derLen  | contentLen            |                      ...                                   |
--------------------------------+                                                            +
            className + header  + content  bytes                                             |
                                                                                             +
                            ... ...                                  | CRC32(optional)       |
---------------------------------------------------------------------------------------------+

V2 ?? V1 ??????????

  1. ?????????ver1?
  2. ??????????? CRC32 ??????????

???????

??????
?? RPC ????

?????

 strict';

t net = require('net');
t pump = require('pump');
t protocol = require('sofa-bolt-node');

t options = {
ntReqs: new Map(),

t socket = net.connect(12200, '127.0.0.1');
t encoder = protocol.encoder(options);
t decoder = protocol.decoder(options);

et.once('connect', () => {
nsole.log('connected');

et.once('close', () => {
nsole.log('close');

et.once('error', err => {
nsole.log(err);


? API
(encoder, socket, decoder, err => {
nsole.log(err);


? response / heartbeat_acl
der.on('response', res => {
nsole.log(res);

der.on('heartbeat_ack', res => {
nsole.log(res);


? RPC ??
der.writeRequest(1, {
gs: [{
$class: 'java.lang.String',
$: 'peter',
,
rverSignature: 'com.alipay.sofa.rpc.quickstart.HelloService:1.0',
thodName: 'sayHello',
meout: 3000,


????
der.writeHeartbeat(2, { clientUrl: 'xxx' });

?????

 strict';

t net = require('net');
t pump = require('pump');
t protocol = require('sofa-bolt-node');

t server = net.createServer(socket => {
nst options = {
sentReqs: new Map(),

nst encoder = protocol.encoder(options);
nst decoder = protocol.decoder(options);
mp(encoder, socket, decoder, err => {
console.log(err);
;

coder.on('request', req => {
console.log(req);
encoder.writeResponse(req, {
  isError: false,
  appResponse: {
    $class: 'java.lang.String',
    $: `hello ${req.data.args[0]} !`,
  },
});
;
coder.on('heartbeat', hb => {
console.log(hb);
encoder.writeHeartbeatAck(hb);
;


er.listen(12200);
?????????

??????????? protobuf????????????????????????????? hessian ?????????????????????????????????????? pb ? demo

?? *.proto ??????

ax = "proto3";

age com.alipay.sofa.rpc.test;

?
on java_multiple_files = false;

ice ProtoService {
c echoObj (EchoRequest) returns (EchoResponse) {}


age EchoRequest {
ring name = 1;
oup group = 2;


age EchoResponse {
t32 code = 1;
ring message = 2;


 Group {
= 0;
= 1;

????? protobuf

 strict';

t net = require('net');
t path = require('path');
t pump = require('pump');
t protocol = require('sofa-bolt-node');
t protobuf = require('antpb');

? *.proto ???????? proto
t protoPath = path.join(__dirname, 'proto');
t proto = protobuf.loadAll(protoPath);

 proto ?????? encoder/decoder
t sentReqs = new Map();
t encoder = protocol.encoder({ sentReqs, proto });
t decoder = protocol.decoder({ sentReqs, proto });

t socket = net.connect(12200, '127.0.0.1');
et.once('connect', () => {
nsole.log('connected');

et.once('close', () => {
nsole.log('close');

et.once('error', err => {
nsole.log(err);

(encoder, socket, decoder, err => {
nsole.log(err);


??????? protobuf
der.codecType = 'protobuf';

t req = {
rverSignature: 'com.alipay.sofa.rpc.test.ProtoService:1.0',
thodName: 'echoObj',
gs: [{
name: 'peter',
group: 'B',
,
meout: 3000,


der.on('response', res => {
nsole.log(res.data.appResponse);


????????
Reqs.set(1, { req });
der.writeRequest(1, req);

????? protobuf

 strict';

t net = require('net');
t path = require('path');
t pump = require('pump');
t protocol = require('sofa-bolt-node');
t protobuf = require('antpb');

t protoPath = path.join(__dirname, 'proto');
t proto = protobuf.loadAll(protoPath);

t server = net.createServer(socket => {
nst options = {
sentReqs: new Map(),
proto,

nst encoder = protocol.encoder(options);
nst decoder = protocol.decoder(options);
mp(encoder, socket, decoder, err => {
console.log(err);
;

coder.on('request', req => {
const reqData = req.data.args[0].toObject({ enums: String });;
encoder.writeResponse(req, {
  isError: false,
  appResponse: {
    code: 200,
    message: 'hello ' + reqData.name + ', you are in ' + reqData.group,
  },
});
;

coder.on('heartbeat', hb => {
console.log(hb);
encoder.writeHeartbeatAck(hb);
;


er.listen(12200);
CRC32 ??

RPC ??????????????????????????????????????????????????????? Bolt ??????????????????????????????? 4 ? bytes ????????? CRC32 ???????????????????? CRC32 ??????????????????????????

?????????????????????????????????????????? crc32 ??

 strict';

t net = require('net');
t pump = require('pump');
t protocol = require('sofa-bolt-node');

t options = {
ntReqs: new Map(),


t socket = net.connect(12200, '127.0.0.1');
t encoder = protocol.encoder(options);
t decoder = protocol.decoder(options);
(encoder, socket, decoder);

???? crc ??
der.protocolType = 'bolt2'; // v2 ??????? crc ??
der.boltVersion = 2;
der.crcEnable = true;

?
der.writeRequest(1, {
gs: [{
$class: 'java.lang.String',
$: 'peter',
,
rverSignature: 'com.alipay.sofa.rpc.quickstart.HelloService:1.0',
thodName: 'sayHello',
meout: 3000,

??????
????
ProtocolEncoder ??
????????

????????????????????????? Encoder ? Decoder ????????? Nodejs ???Stream????

------+  pipe  +---------+  pipe  +---------+    response
coder |  --->  | Socket  |  --->  | Decoder |    ...
------+        +---------+        +---------+
                  |  ^
                  |  |
                  |  |
                  v  |
------+  pipe  +---------+  pipe  +---------+    request
coder |  --->  | Socket  |  --->  | Decoder |    ...
------+        +---------+        +---------+

????????????????? Encoder/Decoder ???????????? API??????????????????????dubbo??????????????

??????

??????????????????????????????????Bug?????

??????????????????????

??????

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.