Skip to content
Permalink
Browse files
sync latest code
  • Loading branch information
hufeng committed Apr 13, 2018
1 parent 6e1ebec commit 304ba8e1475488688c2a5c21bf2e94450c6e77ea
Show file tree
Hide file tree
Showing 158 changed files with 4,374 additions and 4,322 deletions.
@@ -8,6 +8,8 @@ pids
*.pid
*.seed

target

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

@@ -27,4 +29,15 @@ node_modules

# Users Environment Variables
.lock-wscript
lib
lib

.DS_Store
node_modules
lib
temp
logs
*.log
*.iml
target
es6
es7
@@ -0,0 +1,12 @@
build-dubbo: clean-dubbo
tsc --project ./packages/dubbo/tsconfig.json
tsc --project ./packages/dubbo/tsconfig-es6.json
@echo "compile duboo successfully 👌\n"

clean-dubbo:
rm -rf ./packages/dubbo/es6
rm -rf ./packages/dubbo/es7
@echo "clean dubbo successfully 👌\n"

test-dubbo:
./node_modules/.bin/jest --testPathPattern packages/dubbo/src/__tests__ --verbose --watch
142 README.md
@@ -1,13 +1,31 @@
## dubbo
## dubbo2.js

nodejs 使用原生的 dubbo 协议打通了 dubbo 的 rpc 方法调用 .
![love dubbo]()

感谢阿里的 hessian.js 和 js-to-java 两大模块
多年期盼,一朝梦圆!

感谢 js-to-java,hessian.js 两大核心模块

我们终于用 nodejs 使用原生的 dubbo (dubbo head + hessian body) 协议打通了 dubbo 的 rpc 方法调用 .

## Features

1. Support zookeeper as register center

2. TCP Dubbo Native protocal (Dubbo Header + Hessian Body)

3. Socket Pool (SocketAgent -> SocketPool -> SocketWorker)

4. Support Directly Dubbo (const Dubbo = DirectlyDubbo({..}))

5. middleware

6. tracing

## Getting Started

```shell
npm install dubbo2.js
yarn add dubbo2.js
```

## How to Usage?
@@ -32,7 +50,7 @@ interface IDemoService {
//创建dubbo对象
const dubbo = new Dubbo({
application: {name: 'dubbo-directly-test'},
application: {name: 'node-dubbo'},
//zookeeper address
register: 'localhost:2181',
dubboVersion: '2.0.0',
@@ -42,7 +60,7 @@ const dubbo = new Dubbo({
//代理本地对象->dubbo对象
const demoService = dubbo.proxyService<IDemoService>({
dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
version: '0.0.0',
version: '1.0.0',
methods: {
sayHello(name) {
//仅仅做参数hessian化转换
@@ -89,5 +107,113 @@ yarn run test
DEBUG=dubbo* yarn run test
```

![log](https://raw.githubusercontent.com/hufeng/dubbo.js/master/screenshot/1.jpg)
![log](https://raw.githubusercontent.com/hufeng/dubbo.js/master/screenshot/2.jpg)
![dubbo-flow](http://git.dev.qianmi.com/QMFE/dubbo2.js/raw/master/resources/dubbo2-flow.png)

## API

创建 Dubbo 对象

```javascript
const dubbo = new Dubbo({
dubboVersion //当前dubbo的版本 (string类型); 必传
application //记录应用的名称,zookeeper的调用时候写入consumer 类型:({name: string};) 可选
enableHeartBeat //是否启用心跳机制 默认true 可选 类型 boolean
dubboInvokeTimeout //设置dubbo调用超时时间默认10s 可选 类型number
dubboSocketPool //设置dubbo创建socket的pool大小,默认4 可选 类型number
register //设置zookeeper注册中心地址 必填 类型string
zkRoot //zk的默认根路径,默认/dubbo 类型string
interfaces //设置zk监听的接口名称 类型 Array<string> 必填
});
// Or
const dubbo = Dubbo.from({
dubboVersion //当前dubbo的版本 (string类型); 必传
application //记录应用的名称,zookeeper的调用时候写入consumer 类型:({name: string};) 可选
enableHeartBeat //是否启用心跳机制 默认true 可选 类型 boolean
dubboInvokeTimeout //设置dubbo调用超时时间默认10s 可选 类型number
dubboSocketPool //设置dubbo创建socket的pool大小,默认4 可选 类型number
register //设置zookeeper注册中心地址 必填 类型string
zkRoot //zk的默认根路径,默认/dubbo 类型string
interfaces //设置zk监听的接口名称 类型 Array<string> 必填
})
//dubbo的代理服务
const demoSerivce = Dubbo.proxService({
//代理的服务接口 - string 必传
dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
//服务接口的版本 - string 必传
version: '1.0.0',
//超时时间 number 可选
timeout: 10
//所属组 string 可选
group: 'qianmi',
//接口内的方法 - Array<Function> 必传
methods: {
//method name
xx(params) {
return [
params
]
}
},
})
```

## middleware

通过对调用链路的抽象使用和 koa 相同的 middleware 机制,方便自定义拦截器,比如 logger,

```js
//cost-time middleware
dubbo.use(async (ctx, next) => {
const startTime = Date.now();
await next();
const endTime = Date.now();
console.log(endTime - startTime);
});
```

## Performance

```text
loadtest -c 100 -n 10000 -k http://localhost:3000/hello
[Mon Feb 12 2018 17:30:28 GMT+0800 (CST)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Target URL: http://localhost:3000/hello
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Max requests: 10000
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Concurrency level: 100
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Agent: keepalive
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Completed requests: 10000
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Total errors: 0
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Total time: 2.3191059540000003 s
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Requests per second: 4312
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Mean latency: 22.9 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO Percentage of the requests served within a certain time
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO 50% 20 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO 90% 31 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO 95% 38 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO 99% 73 ms
[Mon Feb 12 2018 17:30:30 GMT+0800 (CST)] INFO 100% 116 ms (longest request)
```

## FAQ

```javascript
import {Dubbo} from 'dubbo2.js';
```

默认导入的 dubbo2.js 是按照 es2017 进行编译的,支持 node7.10 以上。

如果更低的 node 版本,可以使用

```javascript
import {Dubbo} from 'dubbo2.js/es6';
```

## 怎么参与开发?

1.欢迎 pr

2.欢迎 issue
@@ -0,0 +1,30 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'egg-ci',
'egg-bin',
'egg-mock',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
'./dist',
],
};

@@ -0,0 +1 @@
coverage
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
@@ -0,0 +1,12 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
@@ -0,0 +1,10 @@
sudo: false
language: node_js
node_js:
- '8'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
@@ -0,0 +1,33 @@
# hello

a simple a simple web app invoke dubbo service

## QuickStart

<!-- add docs here for user -->

see [egg docs][egg] for more detail.

### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### Deploy

```bash
$ npm start
$ npm stop
```

### npm scripts

- Use `npm run lint` to check code style.
- Use `npm test` to run unit test.
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.


[egg]: https://eggjs.org
@@ -0,0 +1,39 @@
# hello

a simple a simple web app invoke dubbo service

## 快速入门

<!-- 在此次添加使用文档 -->

如需进一步了解,参见 [egg 文档][egg]。

### 本地开发

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### 部署

```bash
$ npm start
$ npm stop
```

### 单元测试

- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。
- 断言库非常推荐使用 [power-assert]。
- 具体参见 [egg 文档 - 单元测试](https://eggjs.org/zh-cn/core/unittest)。

### 内置指令

- 使用 `npm run lint` 来做代码风格检查。
- 使用 `npm test` 来执行单元测试。
- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod) 。


[egg]: https://eggjs.org
@@ -0,0 +1,34 @@
const {Dubbo, java} = require('dubbo2.js');

const dubbo = new Dubbo({
application: {name: 'dubbo-node-consumer'},
register: 'localhost:2181',
dubboVersion: '2.0.0',
interfaces: ['com.alibaba.dubbo.demo.DemoService'],
});

const demoService = dubbo.proxyService({
dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
version: '1.0.0',
methods: {
sayHello(name) {
return [java.String(name)];
},

echo() {},

test() {},

getUserInfo() {
return [
java.combine('com.alibaba.dubbo.demo.UserRequest', {
id: 1,
name: 'nodejs',
email: 'node@qianmi.com',
}),
];
},
},
});

module.exports = demoService;

0 comments on commit 304ba8e

Please sign in to comment.