Skip to content
Permalink
Browse files
Merge pull request #251 from realbillwong/master
Fix some bugs
  • Loading branch information
hufeng committed Jan 10, 2022
2 parents dba4eea + 31a1edc commit 3d7db6f25bae3f01e088fbe2d034c053fa6b8d2a
Showing 7 changed files with 76 additions and 73 deletions.
@@ -3,14 +3,14 @@
"version": "1.0.0",
"description": "A simple demo",
"scripts": {
"debug:start": "DEBUG=dubbo* ts-node ./src/server-zookeeper.ts",
"debug:start": "cross-env DEBUG=dubbo* ts-node ./src/server-zookeeper.ts",
"start": "ts-node ./src/server-zookeeper.ts",
"dubbo-server": "ts-node ./src/dubbo/provider/server-zookeeper.ts",
"debug:dubbo-server": "DEBUG=dubbo* ts-node ./src/dubbo/provider/server-zookeeper.ts",
"debug:start-nacos": "DEBUG=dubbo* ts-node ./src/server-nacos.ts",
"debug:dubbo-server": "cross-env DEBUG=dubbo* ts-node ./src/dubbo/provider/server-zookeeper.ts",
"debug:start-nacos": "cross-env DEBUG=dubbo* ts-node ./src/server-nacos.ts",
"start-nacos": "ts-node ./src/server-nacos.ts",
"dubbo-server-nacos": "ts-node ./src/dubbo/provider/server-nacos.ts",
"debug:dubbo-server-nacos": "DEBUG=dubbo* ts-node ./src/dubbo/provider/server-nacos.ts"
"debug:dubbo-server-nacos": "cross-env DEBUG=dubbo* ts-node ./src/dubbo/provider/server-nacos.ts"
},
"keywords": [
"apache-dubbo-js"
@@ -19,9 +19,13 @@
"license": "Apache Licence 2.0",
"devDependencies": {
"@types/koa": "^2.11.4",
"@types/koa-router": "^7.4.1"
"@types/koa-router": "^7.4.1",
"cross-env": "^7.0.3"
},
"dependencies": {
"apache-dubbo-consumer": "^4.0.0",
"apache-dubbo-registry": "0.0.2",
"interpret-util": "0.0.6",
"ip": "^1.1.5",
"koa": "^2.4.1",
"koa-router": "^7.3.0"
@@ -15,13 +15,14 @@
* limitations under the License.
*/

import {Dubbo, setting, nacos} from 'apache-dubbo-js'
import * as service from './service'
import { Dubbo, dubboSetting } from 'apache-dubbo-consumer'
import { Nacos } from 'apache-dubbo-registry'
import * as services from './service'

/**
* setting dubbo invoke params, such version, group etc.
*/
const dubboSetting = setting
const setting = dubboSetting
.match(
[
'org.apache.dubbo.demo.DemoProvider',
@@ -31,18 +32,19 @@ const dubboSetting = setting
version: '1.0.0',
},
)
.match('org.apache.dubbo.demo.BasicTypeProvider', {version: '2.0.0'})
.match('org.apache.dubbo.demo.BasicTypeProvider', { version: '2.0.0' })

/**
* create dubbo instance, it create proxyService
*/
// console.log('nacos-----', nacos);
const dubbo = new Dubbo<typeof service>({
application: {name: 'dubbo-node-consumer'},
service,
dubboSetting,
registry: nacos({
url: 'nacos:localhost:8848',
const dubbo = new Dubbo<typeof services>({
application: {
name: 'dubbo-node-consumer'
},
services,
dubboSetting: setting,
registry: Nacos({
connect: 'localhost:8848',
}),
})

@@ -54,13 +56,4 @@ dubbo.use(async (ctx, next) => {
console.log('-providerAttachments-->', ctx.providerAttachments)
})

/**
* subscribe apache-dubbo-js inner message
*/
dubbo.subscribe({
onTrace(msg) {
console.log(msg)
},
})

export default dubbo
@@ -5,17 +5,18 @@ nodejs 使用原生的 dubbo 协议打通了 dubbo 的 rpc 方法调用 .
## Getting Started

```shell
yarn add dubbo-js
yarn add apache-dubbo-consumer
```

## How to Usage?

```typescript
import { Dubbo, java, TDubboCallResult } from 'dubbo'
import { Dubbo, java, TDubboCallResult } from 'apache-dubbo-consumer'
import { Zk, Nacos } from 'apache-dubbo-registry'
//定义dubbo方法类型接口
//方便代码自动提示
//如果写的JavaScript忽略
// 定义 dubbo 方法类型接口
// 方便代码自动提示
// 如果写的 JavaScript 忽略
interface IDemoService {
sayHello(name: string): TDubboCallResult<string>
@@ -29,22 +30,33 @@ interface IDemoService {
}>
}
//创建dubbo对象
// 创建 dubbo 对象
const dubbo = new Dubbo({
application: { name: 'dubbo-js' },
//zookeeper address
register: 'localhost:2181',
dubboVersion: '2.0.0',
interfaces: ['org.apache.dubbo.demo.DemoService']
application: {
name: 'dubbo-js',
},
// nacos
// registry: Nacos({
// connect: 'localhost:8848',
// }),
// zookeeper
registry: Zk({
connect: 'localhost:2181',
}),
dubboVersion: '2.0.2',
services: {
demoService,
}
})
//代理本地对象->dubbo对象
// 代理本地对象 -> dubbo 对象
const demoService = dubbo.proxyService<IDemoService>({
dubboInterface: 'org.apache.dubbo.demo.DemoService',
version: '1.0.0',
methods: {
sayHello(name) {
//仅仅做参数hessian化转换
// 仅仅做参数 hessian 化转换
return [java.String(name)]
},
@@ -53,7 +65,7 @@ const demoService = dubbo.proxyService<IDemoService>({
test() {},
getUserInfo() {
//仅仅做参数hessian化转换
// 仅仅做参数 hessian 化转换
return [
java.combine('com.alibaba.dubbo.demo.UserRequest', {
id: 1,
@@ -66,12 +78,12 @@ const demoService = dubbo.proxyService<IDemoService>({
})
const result1 = await demoService.sayHello('node')
//print {err: null, res:'hello node from dubbo service'}
// print { err: null, res:'hello node from dubbo service' }
const res = await demoService.echo()
//print {err: null, res: 'pang'}
// print { err: null, res: 'pang' }
const res = await demoService.getUserInfo()
//status: 'ok', info: { id: '1', name: 'test' }
// status: 'ok', info: { id: '1', name: 'test' }
```

## as developer
@@ -80,7 +92,7 @@ const res = await demoService.getUserInfo()
brew install zookeeper
brew services start zookeeper
#运行java/dubbo-simple下面的例子
# 运行 java/dubbo-simple 下面的例子
yarn run test
@@ -94,39 +106,32 @@ DEBUG=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
logger //设置logger对象,可选
register //设置zookeeper注册中心地址 必填 类型string
zkRoot //zk的默认根路径,默认/dubbo 类型string
interfaces //设置zk监听的接口名称 类型 Array<string> 必填
registry // zookeeper/nacos 注册中心地址,类型 IRegistry,必填
services // 接口服务,必填
dubboVersion // 当前 dubbo 的版本,类型 string,必填
application // 注册 consumer 应用的名称,类型:({ name: string }),可选
enableHeartBeat // 是否启用心跳机制,默认 true,类型 boolean,可选
dubboInvokeTimeout // dubbo 调用超时时间,默认 10s, 类型 number, 可选
dubboSocketPool // dubbo 创建 socket 的 pool 大小,默认 4, 类型 number, 可选
logger // logger 对象,默认 console,可选
zkRoot // zk 的默认根路径,默认 /dubbo, 类型 string, 可选,如果是 nacos 注册中心,则不需要该字段
});
// 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
logger //设置logger对象,可选
register //设置zookeeper注册中心地址 必填 类型string
zkRoot //zk的默认根路径,默认/dubbo 类型string
interfaces //设置zk监听的接口名称 类型 Array<string> 必填
...
// 参数同上
})
//dubbo的代理服务
// dubbo 的代理服务
const demoSerivce = Dubbo.proxService({
//代理的服务接口 - string 必传
// 代理的服务接口 - string 必传
dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
//服务接口的版本 - string 必传
// 服务接口的版本 - string 必传
version: '1.0.0',
//接口内的方法 - Array<Function> 必传
// 接口内的方法 - Array<Function> 必传
methods: {
//method name
// method name
xx(params) {
return [
params
@@ -139,13 +144,13 @@ const demoSerivce = Dubbo.proxService({
## FAQ

```javascript
import { Dubbo } from 'dubbo-js'
import { Dubbo } from 'apache-dubbo-consumer'
```

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

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

```javascript
import { Dubbo } from 'dubbo-js/es6'
import { Dubbo } from 'apache-dubbo-consuner/es6'
```
@@ -26,7 +26,7 @@ export interface ITimeoutProps {
export interface INaocsClientProps {
namespace?: string
connect: string
logger: Console
logger?: Console
}
export interface IRegistrySubscriber {
onData: (map: Map<TDubboInterface, Array<TDubboUrl>>) => void
@@ -46,7 +46,8 @@ export async function extra(extraParam: IDubboExtInfo): Promise<IExtraResult> {
execCmd.stdout.on('data', (rowData: Buffer) => {
let output = rowData.toString('utf8')
if (output.includes(startFlag)) {
jarDir = output.match(/Output at :(.*)(\nelapsed.*?s)?/)[1]
const reg = new RegExp(`${startFlag}(.*)(\nelapsed.*?s)?`)
jarDir = output.match(reg)[1]
}
})

@@ -92,7 +92,7 @@ export async function toInterface(

intepretHandle.sourceFile.addImport({
moduleSpecifier: 'interpret-util',
defaultImport: `{${extraImport.join(',')}}`
defaultImport: `{ ${extraImport.join(', ')} }`
})

log('转换 名称::%s 属性 :%j 方法:%j', typeDef.name, properties, methods)
@@ -64,8 +64,8 @@ export async function toTypescript(
sourceFile.addInterface(await toInterface(astJava, intepretHandle))
sourceFile.addVariableStatement(toWrapperClass(astJava, intepretHandle))
sourceFile.addImport({
moduleSpecifier: 'dubbo-js',
defaultImport: '{TDubboCallResult,Dubbo}'
moduleSpecifier: 'apache-dubbo-consumer',
defaultImport: '{ TDubboCallResult, Dubbo }'
})
sourceFile.addFunction(
toProxyFunc({

0 comments on commit 3d7db6f

Please sign in to comment.