forked from apache/dubbo-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#325 from godkun/dubbo3
feat: 调整dubbo transport
- Loading branch information
Showing
7 changed files
with
302 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "apache-dubbo3-transport", | ||
"version": "0.0.1", | ||
"main": "./lib/index.js", | ||
"types": "./lib/typings/index.d.ts", | ||
"description": "apache dubbo3 transport", | ||
"keywords": [ | ||
"dubbo3", | ||
"nodejs", | ||
"dubbo3-transport" | ||
], | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"debug": "^4.3.4", | ||
"lodash": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@types/lodash": "^4.14.189" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DubboClientTransport, DubboServerTransport } from '../index' | ||
|
||
describe(`dubbo-transport`, () => { | ||
it('test dubbo-transport', () => { | ||
// 创建服务端 transport 实例 | ||
const server = new DubboServerTransport({ | ||
url: 'http://localhost:3600', | ||
port: 3600 | ||
}) | ||
expect(server.port).toBe(3600) | ||
// 创建客户端 transport 实例 | ||
const client = new DubboClientTransport({ | ||
url: 'http://localhost:3600', | ||
port: 3600 | ||
}) | ||
expect(client.url).toBe('http://localhost:3600') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { debug } from 'debug' | ||
import http2 from 'node:http2' | ||
import { IDubboClientTransport, DubboContext } from './transport' | ||
|
||
// init log | ||
const log = debug('dubbo3:transport:client') | ||
|
||
export class DubboClientTransport implements IDubboClientTransport { | ||
// transport 实例 | ||
private transport: any | ||
private ctx: DubboContext | ||
|
||
constructor(opts: DubboContext) { | ||
this.ctx = opts | ||
this.connect() | ||
} | ||
|
||
get url() { | ||
return this.ctx.url | ||
} | ||
|
||
/** | ||
* 建立连接 | ||
*/ | ||
connect() { | ||
this.transport = http2.connect(this.url) | ||
|
||
this.transport.once('connect', () => { | ||
log('has connected') | ||
}) | ||
} | ||
|
||
/** | ||
* 发送消息 | ||
* @param msg | ||
*/ | ||
async send(msg: DubboContext): Promise<void> { | ||
this.transport.request(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { DubboClientTransport } from './client' | ||
import { DubboServerTransport } from './server' | ||
|
||
export { DubboClientTransport, DubboServerTransport } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import debug from 'debug' | ||
import EventEmitter from 'node:events' | ||
import http2 from 'node:http2' | ||
import { IDubboServerTransport, DubboContext } from './transport' | ||
|
||
// init log | ||
const log = debug('dubbo3:transport:client') | ||
|
||
export class DubboServerTransport | ||
extends EventEmitter | ||
implements IDubboServerTransport | ||
{ | ||
private ctx: DubboContext | ||
transport: any | ||
|
||
constructor(opts: DubboContext) { | ||
super() | ||
this.ctx = opts | ||
this.transport = this.start() | ||
} | ||
|
||
get url() { | ||
return this.ctx.url | ||
} | ||
|
||
get port() { | ||
return this.ctx.port | ||
} | ||
|
||
/** | ||
* 启动服务端 transport | ||
* @returns | ||
*/ | ||
start() { | ||
const server = http2.createServer() | ||
server.on('stream', (stream, headers) => { | ||
log(stream) | ||
stream.on('data', (data) => { | ||
log(data) | ||
// TODO: | ||
}) | ||
stream.on('end', () => { | ||
log('end...') | ||
// TODO: 通知 client | ||
}) | ||
stream.on('error', (error) => { | ||
log(error) | ||
}) | ||
}) | ||
server.listen(this.port) | ||
return server | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export interface DubboContext { | ||
url: string | ||
body?: Object | null | ||
port: number | ||
} | ||
|
||
export interface IDubboClientTransport { | ||
send(msg: DubboContext): Promise<any> | ||
} | ||
|
||
export interface IDubboServerTransport { | ||
// start(msg: DubboContext): Promise<any> | ||
} |
Oops, something went wrong.