Skip to content

Commit

Permalink
add quick start document
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Aug 6, 2021
1 parent 703af01 commit 03fe48f
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 14 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<!--
* @Author: Malin Xie
* @Description:
* @Date: 2021-07-24 16:54:14
-->
# baidurpc

<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>

[![Go Report Card](https://goreportcard.com/badge/github.com/baidu-golang/pbrpc?style=flat-square)](https://goreportcard.com/report/github.com/baidu-golang/pbrpc)
[![Go](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml/badge.svg)](https://github.com/baidu-golang/pbrpc/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/baidu-golang/pbrpc/branch/master/graph/badge.svg?token=EY9Z88E82P)](https://codecov.io/gh/baidu-golang/pbrpc)
Expand All @@ -12,10 +14,7 @@
[![LICENSE](https://img.shields.io/github/license/baidu-golang/pbrpc.svg?style=flat-square)](https://github.com/baidu-golang/pbrpc/blob/master/LICENSE)


baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它以Protobuf作为基本的数据交换格式。
本版本基于golang实现.完全兼容jprotobuf-rpc-socket: https://github.com/Baidu-ecom/Jprotobuf-rpc-socket

features:
### features:

- 内置连接池,具备更高的性能,低延迟 QPS: 5w+
- 支持自动重连功能[Done]
Expand All @@ -37,7 +36,10 @@ $ go get github.com/baidu-golang/pbrpc
```

### 使用说明与Demo
[Demo开发示例](./Demo.md)

[Quick Start(服务发布)](./docs/quickstart_server.md) <br>
[Quick Start(客户端调用)](./docs/quickstart_client.md) <br>
[更多特性使用说明](./docs/Demo.md)<br>
[Demo开发示例代码](./example)<br>
## License
brpc is [Apache 2.0 licensed](./LICENSE).
20 changes: 17 additions & 3 deletions Demo.md → docs/Demo.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
### Demo示例
<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>

### 更多特性使用介绍

#### 开发RPC服务端

Expand Down Expand Up @@ -29,8 +36,8 @@
// Echo test publish method with return type has context argument
// 方法要求
// 参数个数必须为2个, 第一个类型必须为 context.Context
// 第二个类型必须是实现 proto.Message接口
// 参数个数必须为1个或2个, 第一个类型必须为 context.Context
// 第二个类型必须是实现 proto.Message接口(如果是无参,可以省略)
// 返回个数可以为1个或2个 第一个类型必须是实现 proto.Message接口
// 第2个参数为可选。 当使用时,必须为 context.Context类型
func (rpc *EchoService) Echo(c context.Context, in *DataMessage) (*DataMessage, context.Context) {
Expand All @@ -51,6 +58,12 @@
}
```

以下都是合法的定义方法
1. Echo(c context.Context, in *DataMessage) (*DataMessage, context.Context)
2. Echo(c context.Context) (*DataMessage, context.Context)
3. Echo(c context.Context) (*DataMessage)


2. 指定发布端口,把EchoService发布成RPC服务

```go
Expand Down Expand Up @@ -104,6 +117,7 @@
fmt.Println(err)
os.Exit(-1)
}
defer rpcClient.Close()
// 调用RPC
serviceName := "echoService"
methodName := "echo"
Expand Down
88 changes: 88 additions & 0 deletions docs/quickstart_client.md
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, &parameterOut)
if err != nil {
fmt.Println(err)
}
fmt.Println(*parameterOut.Name)
```

77 changes: 77 additions & 0 deletions docs/quickstart_server.md
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)
}
```

0 comments on commit 03fe48f

Please sign in to comment.