-
Notifications
You must be signed in to change notification settings - Fork 930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ftr: Add dubbo-go-cli telnet tool support #818
Merged
Merged
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3b9229b
ftr: add cli tool
LaurenceLiZhixin 16baeda
fix: add readme
LaurenceLiZhixin 63ae531
fix: change go import
LaurenceLiZhixin b99152a
fix: fix import block
LaurenceLiZhixin 4f2d462
fix: add lisence
LaurenceLiZhixin 6d5a472
chore: add all license
LaurenceLiZhixin 841c4e6
fix: change comment to English
LaurenceLiZhixin 0008fa9
chore: retry cicd
LaurenceLiZhixin 4fe1da9
fix: add enEnglish readme, add timeout param
LaurenceLiZhixin 54d4984
fix: change hessian register method
LaurenceLiZhixin 7b7b8a5
fix: use vv.Type to check POJO
LaurenceLiZhixin 34e0999
chore
LaurenceLiZhixin 8f7fc94
chore: comment fix
LaurenceLiZhixin 62ec58a
Fix: change hessian pkg
LaurenceLiZhixin b41de25
fix
aebfe94
fix: change rely repository to dubbogo/gost
11fd33b
fix: fix merge conflict
1599fd4
fix: change hessian2 version to 1.8.0
d4ce11b
fix: change gost to 1.10
53db19c
fix: change gost version to 1.10
07754c0
fix: change cli gost version to 1.10.0
b6022bb
fix ctx linter error
AlexStocks d305b67
Merge remote-tracking branch 'upstream/develop' into develop
LaurenceLiZhixin 93168df
fix: fix conflict
LaurenceLiZhixin 34289f4
Fix: go fmt
LaurenceLiZhixin 1ab564f
fix: go mod tidy
LaurenceLiZhixin 5919b11
Merge remote-tracking branch 'upstream/develop' into develop
LaurenceLiZhixin 3dc2f17
Merge remote-tracking branch 'upstream/develop' into develop
LaurenceLiZhixin 55737aa
Merge remote-tracking branch 'upstream/develop' into develop
LaurenceLiZhixin 65cd856
Merge remote-tracking branch 'upstream/develop' into develop
LaurenceLiZhixin f3ef409
Merge branch 'develop' into feature/cli-support
LaurenceLiZhixin 18ab1c4
fix: add comment
LaurenceLiZhixin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 @@ | ||
.idea/ | ||
|
||
# Binary | ||
example/dubbo-go-cli | ||
|
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 @@ | ||
# dubbo-go-cli | ||
|
||
### 1. Problem we solved. | ||
|
||
For the running dubbo-go server, we need a telnet-cli tool to test if the server works healthily. | ||
The tool should support dubbo protocol, making it easy for you to define your own request pkg, get rsp struct of your server, and total costing time. | ||
|
||
|
||
### 2. How to get cli-tool | ||
run in dubbo-go/tools/cli \ | ||
`$ sh build.sh`\ | ||
and you can get dubbo-go-cli | ||
|
||
### 3. Quick start:[example](example/README_CN.md) | ||
|
||
|
||
|
||
### Third party dependence(temporary) | ||
|
||
github.com/LaurenceLiZhixin/dubbo-go-hessian2 \ | ||
github.com/LaurenceLiZhixin/json-interface-parser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls submit ur code to github.com/dubbogo/gost. |
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,17 @@ | ||
# dubbo-go-cli | ||
|
||
### 1. 解决问题 | ||
|
||
针对正在运行的dubbo服务,需要拥有一个命令行工具来针对服务进行测试。 | ||
该服务需要支持dubbo协议,方便用户进行自定义传输包体。 | ||
|
||
### 2. cli工具获取方法 | ||
`sh build.sh` | ||
|
||
### 3. 使用方法:见[example](example/README_CN.md) | ||
|
||
|
||
### 第三方依赖(临时兼容) | ||
|
||
github.com/LaurenceLiZhixin/dubbo-go-hessian2 \ | ||
github.com/LaurenceLiZhixin/json-interface-parser |
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,2 @@ | ||
export GOPROXY="http://goproxy.io" | ||
go build -o dubbo-go-cli |
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,196 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"strconv" | ||
"sync" | ||
"time" | ||
) | ||
|
||
import ( | ||
"go.uber.org/atomic" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/tools/cli/common" | ||
"github.com/apache/dubbo-go/tools/cli/protocol" | ||
_ "github.com/apache/dubbo-go/tools/cli/protocol/dubbo" | ||
) | ||
|
||
const defaultBufferSize = 1024 * 1024 * 4 | ||
AlexStocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TelnetClient maintain a connection to target | ||
type TelnetClient struct { | ||
tcpAddr string | ||
responseTimeout time.Duration | ||
protocolName string | ||
requestList []*protocol.Request | ||
conn *net.TCPConn | ||
proto protocol.Protocol | ||
|
||
sequence atomic.Uint64 | ||
pendingResponses *sync.Map | ||
waitNum atomic.Uint64 | ||
} | ||
|
||
// NewTelnetClient create a new tcp connection, and create default request | ||
func NewTelnetClient(host string, port int, protocolName, interfaceID, version, group, method string, reqPkg interface{}, timeout int) (*TelnetClient, error) { | ||
tcpAddr := net.JoinHostPort(host, strconv.Itoa(port)) | ||
resolved := resolveTCPAddr(tcpAddr) | ||
conn, err := net.DialTCP("tcp", nil, resolved) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Printf("connected to %s:%d\n", host, port) | ||
log.Printf("try calling interface:%s.%s\n", interfaceID, method) | ||
log.Printf("with protocol:%s\n\n", protocolName) | ||
proto := common.GetProtocol(protocolName) | ||
|
||
return &TelnetClient{ | ||
tcpAddr: tcpAddr, | ||
conn: conn, | ||
responseTimeout: time.Duration(timeout) * time.Millisecond, //default timeout | ||
protocolName: protocolName, | ||
pendingResponses: &sync.Map{}, | ||
proto: proto, | ||
requestList: []*protocol.Request{ | ||
{ | ||
InterfaceID: interfaceID, | ||
Version: version, | ||
Method: method, | ||
Group: group, | ||
Params: []interface{}{reqPkg}, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func resolveTCPAddr(addr string) *net.TCPAddr { | ||
resolved, error := net.ResolveTCPAddr("tcp", addr) | ||
if nil != error { | ||
log.Fatalf("Error occured while resolving TCP address \"%v\": %v\n", addr, error) | ||
} | ||
|
||
return resolved | ||
} | ||
|
||
// ProcessRequests send all requests | ||
func (t *TelnetClient) ProcessRequests(userPkg interface{}) { | ||
for i, _ := range t.requestList { | ||
t.processSingleRequest(t.requestList[i], userPkg) | ||
} | ||
} | ||
|
||
// addPendingResponse add a response @model to pending queue | ||
// once the rsp got, the model will be used. | ||
func (t *TelnetClient) addPendingResponse(model interface{}) uint64 { | ||
seqId := t.sequence.Load() | ||
t.pendingResponses.Store(seqId, model) | ||
t.waitNum.Inc() | ||
t.sequence.Inc() | ||
return seqId | ||
} | ||
|
||
// removePendingResponse delete item from pending queue by @seq | ||
func (t *TelnetClient) removePendingResponse(seq uint64) { | ||
if t.pendingResponses == nil { | ||
return | ||
} | ||
if _, ok := t.pendingResponses.Load(seq); ok { | ||
t.pendingResponses.Delete(seq) | ||
t.waitNum.Dec() | ||
} | ||
return | ||
} | ||
|
||
// processSingleRequest call one req. | ||
func (t *TelnetClient) processSingleRequest(req *protocol.Request, userPkg interface{}) { | ||
// proto create package procedure | ||
req.ID = t.sequence.Load() | ||
inputData, err := t.proto.Write(req) | ||
if err != nil { | ||
log.Fatalln("error: handler.Writer err = ", err) | ||
} | ||
startTime := time.Now() | ||
|
||
// init rsp Package and add to pending queue | ||
seqId := t.addPendingResponse(userPkg) | ||
defer t.removePendingResponse(seqId) | ||
|
||
requestDataChannel := make(chan []byte, 0) | ||
responseDataChannel := make(chan []byte, 0) | ||
|
||
// start data transfer procedure | ||
go t.readInputData(string(inputData), requestDataChannel) | ||
go t.readServerData(t.conn, responseDataChannel) | ||
|
||
timeAfter := time.After(t.responseTimeout) | ||
|
||
for { | ||
select { | ||
case <-timeAfter: | ||
log.Println("request timeout to:", t.tcpAddr) | ||
return | ||
case request := <-requestDataChannel: | ||
if _, err := t.conn.Write(request); nil != err { | ||
log.Fatalf("Error occured while writing to TCP socket: %v\n", err) | ||
} | ||
case response := <-responseDataChannel: | ||
rspPkg, _, err := t.proto.Read(response, t.pendingResponses) | ||
if err != nil { | ||
log.Fatalln("Error with protocol Read(): ", err) | ||
} | ||
t.removePendingResponse(seqId) | ||
log.Printf("After %dms , Got Rsp:", time.Now().Sub(startTime).Milliseconds()) | ||
common.PrintInterface(rspPkg) | ||
if t.waitNum.Sub(0) == 0 { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (t *TelnetClient) readInputData(inputData string, toSent chan<- []byte) { | ||
toSent <- []byte(inputData) | ||
} | ||
|
||
func (t *TelnetClient) readServerData(connection *net.TCPConn, received chan<- []byte) { | ||
buffer := make([]byte, defaultBufferSize) | ||
var err error | ||
var n int | ||
for nil == err { | ||
n, err = connection.Read(buffer) | ||
received <- buffer[:n] | ||
} | ||
|
||
t.assertEOF(err) | ||
} | ||
|
||
func (t *TelnetClient) assertEOF(error error) { | ||
if "EOF" != error.Error() { | ||
log.Fatalf("Error occured while operating on TCP socket: %v\n", error) | ||
} | ||
} | ||
|
||
// Destroy close the tcp conn | ||
func (t *TelnetClient) Destroy() { | ||
t.conn.Close() | ||
} |
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,39 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/tools/cli/protocol" | ||
) | ||
|
||
var ( | ||
protocols = make(map[string]func() protocol.Protocol, 8) | ||
) | ||
|
||
// SetProtocol sets the protocol extension with @name | ||
func SetProtocol(name string, v func() protocol.Protocol) { | ||
protocols[name] = v | ||
} | ||
|
||
// GetProtocol finds the protocol extension with @name | ||
func GetProtocol(name string) protocol.Protocol { | ||
if protocols[name] == nil { | ||
panic("protocol for " + name + " is not existing, make sure you have import the package.") | ||
} | ||
return protocols[name]() | ||
} |
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,39 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
) | ||
|
||
// PrintInterface print the interface by level | ||
func PrintInterface(v interface{}) { | ||
val := reflect.ValueOf(v).Elem() | ||
typ := reflect.TypeOf(v) | ||
log.Printf("%+v\n", v) | ||
nums := val.NumField() | ||
for i := 0; i < nums; i++ { | ||
if typ.Elem().Field(i).Type.Kind() == reflect.Ptr { | ||
log.Printf("%s: ", typ.Elem().Field(i).Name) | ||
PrintInterface(val.Field(i).Interface()) | ||
} | ||
} | ||
fmt.Println("") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why using ur branch?