-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiemalin
committed
Aug 6, 2021
1 parent
703af01
commit 03fe48f
Showing
4 changed files
with
195 additions
and
14 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
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
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,88 @@ | ||
<!-- | ||
* @Author: Malin Xie | ||
* @Description: | ||
* @Date: 2021-08-06 13:37:43 | ||
--> | ||
<h1 align="center">baidurpc</h1> | ||
|
||
<p align="center"> | ||
baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它以Protobuf作为基本的数据交换格式。 | ||
本版本基于golang实现.完全兼容jprotobuf-rpc-socket: https://github.com/Baidu-ecom/Jprotobuf-rpc-socket | ||
</p> | ||
|
||
|
||
### Installing | ||
|
||
To start using pbrpc, install Go and run `go get`: | ||
|
||
```sh | ||
$ go get github.com/baidu-golang/pbrpc | ||
``` | ||
|
||
### 定义protobuf 对象 | ||
以下按pb2进行定义 | ||
```go | ||
//手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" | ||
type DataMessage struct { | ||
Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` | ||
} | ||
|
||
func (m *DataMessage) Reset() { *m = DataMessage{} } | ||
func (m *DataMessage) String() string { return proto.CompactTextString(m) } | ||
func (*DataMessage) ProtoMessage() {} | ||
|
||
func (m *DataMessage) GetName() string { | ||
if m.Name != nil { | ||
return *m.Name | ||
} | ||
return "" | ||
} | ||
``` | ||
|
||
|
||
### 开发RPC客户端 | ||
客户端开发,使用RpcClient进行调用。 | ||
|
||
```go | ||
host := "localhost" | ||
port := 1031 | ||
// 创建链接 | ||
url := baidurpc.URL{} | ||
url.SetHost(&host).SetPort(&port) | ||
timeout := time.Second * 500 | ||
// create client by simple connection | ||
connection, err := baidurpc.NewTCPConnection(url, &timeout) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer connection.Close() | ||
// 创建client | ||
rpcClient, err := baidurpc.NewRpcCient(connection) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer rpcClient.Close() | ||
// 调用RPC | ||
serviceName := "echoService" | ||
methodName := "echo" | ||
rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName) | ||
message := "say hello from xiemalin中文测试" | ||
dm := DataMessage{&message} | ||
rpcInvocation.SetParameterIn(&dm) | ||
parameterOut := DataMessage{} | ||
_, err := rpcClient.SendRpcRequest(rpcInvocation, ¶meterOut) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(*parameterOut.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,77 @@ | ||
<h1 align="center">baidurpc</h1> | ||
|
||
<p align="center"> | ||
baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它以Protobuf作为基本的数据交换格式。 | ||
本版本基于golang实现.完全兼容jprotobuf-rpc-socket: https://github.com/Baidu-ecom/Jprotobuf-rpc-socket | ||
</p> | ||
|
||
|
||
### Installing | ||
|
||
To start using pbrpc, install Go and run `go get`: | ||
|
||
```sh | ||
$ go get github.com/baidu-golang/pbrpc | ||
``` | ||
|
||
### 定义protobuf 对象 | ||
以下按pb2进行定义 | ||
```go | ||
//手工定义pb生成的代码, tag 格式 = protobuf:"type,order,req|opt|rep|packed,name=fieldname" | ||
type DataMessage struct { | ||
Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` | ||
} | ||
|
||
func (m *DataMessage) Reset() { *m = DataMessage{} } | ||
func (m *DataMessage) String() string { return proto.CompactTextString(m) } | ||
func (*DataMessage) ProtoMessage() {} | ||
|
||
func (m *DataMessage) GetName() string { | ||
if m.Name != nil { | ||
return *m.Name | ||
} | ||
return "" | ||
} | ||
``` | ||
|
||
|
||
### 开发RPC服务端 | ||
要发布一个RPC服务,需要先定义一个对象以及方法,用于发布服务。 与传统的golang基础库的rpc发布非常一致。 | ||
|
||
1. 以下定义了 <b>EchoService对象</b> 并 增加 <b>Echo</b> 方法 | ||
```go | ||
type EchoService struct { | ||
} | ||
func (rpc *EchoService) Echo(c context.Context, in *DataMessage) (*DataMessage, context.Context) { | ||
var ret = "hello " | ||
if len(*in.Name) == 0 { | ||
ret = ret + "veryone" | ||
} else { | ||
ret = ret + *in.Name | ||
} | ||
dm := DataMessage{} | ||
dm.Name = proto.String(ret) | ||
return &dm, baidurpc.BindAttachement(context.Background(), []byte("hello")) // return with attachement | ||
} | ||
``` | ||
|
||
2. 指定发布端口,把EchoService发布成RPC服务 | ||
|
||
```go | ||
serverMeta := baidurpc.ServerMeta{} | ||
serverMeta.Host = nil | ||
serverMeta.Port = Int(*port) | ||
rpcServer := baidurpc.NewTpcServer(&serverMeta) | ||
echoService := new(EchoService) | ||
rpcServer.Register(echoService) | ||
// 启动RPC服务 | ||
err := rpcServer.Start() | ||
if err != nil { | ||
baidurpc.Error(err) | ||
} | ||
``` |