Skip to content

Commit

Permalink
add http rpc doc
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Aug 23, 2021
1 parent 3c83f21 commit ccd568e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它
- 灵活的超时设置功能[Done]
- 分包chunk支持,针对大数据包支持拆分包的发送的功能[Done]
- 支持 Web管理能力以及内置能力[Done] [查看](https://github.com/jhunters/brpcweb)
- 支持同步发布为Http JSON协议[Doing]
- 支持同步发布为Http JSON协议[Done] [>= v1.2.0]
### Installing

Expand All @@ -45,6 +45,7 @@ $ go get github.com/baidu-golang/pbrpc

[Quick Start(服务发布)](./docs/quickstart_server.md) <br>
[Quick Start(客户端调用)](./docs/quickstart_client.md) <br>
[同步发布http rpc服务](./docs/httprpc.md) <br>
[更多特性使用说明](./docs/Demo.md)<br>
[Demo开发示例代码](./example)<br>
## License
Expand Down
64 changes: 64 additions & 0 deletions docs/httprpc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!--
* @Author: Malin Xie
* @Description:
* @Date: 2021-08-23 16:01:03
-->
<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>



### 同步发布http rpc服务
要同步发布http rpc 服务,部署非常简单,直接调用 EnableHttp 方法即可。会与rpc复用同一端口。

```go
rpcServer.EnableHttp()
```

```go
serverMeta := baidurpc.ServerMeta{}
serverMeta.Host = nil
serverMeta.Port = Int(*port)
rpcServer := baidurpc.NewTpcServer(&serverMeta)

echoService := new(EchoService)

rpcServer.Register(echoService)
// 开启http rpc服务
rpcServer.EnableHttp()
// 启动RPC服务
err := rpcServer.Start()

if err != nil {
baidurpc.Error(err)
}
```

### 相关事项说明
1. 发布http rpc 服务, 需要struct 增加json tag字段说明
```go
type DataMessage struct {
Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"`
}
```
2. http 协议统一使用POST方法进行发布。 url path 协同 http://host:port/rpc/service_name/method_name
3. http请求内容
header 定义 "Content-Type" "application/json;charset=utf-8"
POST body直接为json结构
4. http返回内容, json结果.
<pre>
errno错误码: 0表示成功,其它则为错误
message: 错误信息,errno非0时,会设置
data:返回内容
</pre>
```json
{
"errno" : 0,
"message" : "",
"data" : {}
}
```
1 change: 1 addition & 0 deletions example/server_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func ExampleRpcServerWithHttp() {
mapping["Echo"] = "echo"
rpcServer.RegisterNameWithMethodMapping("echoService", echoService, mapping)

// start http rpc mode
rpcServer.EnableHttp()
rpcServer.Start()
defer rpcServer.Stop()
Expand Down

0 comments on commit ccd568e

Please sign in to comment.