Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
WineChord committed Nov 13, 2023
1 parent 99f3943 commit f58f340
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 150 deletions.
142 changes: 68 additions & 74 deletions grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,114 +61,108 @@ service Greeter {

8. Write business logic:

- Modify `main.go`, add `trpc-grpc` package, and register in the main function:
- Modify `main.go`, add `trpc-grpc` package, and register in the main function:

> It will be modified to be directly supported by the trpc tool in the future, and it needs to be manually introduced and registered for the time being
> It will be modified to be directly supported by the trpc tool in the future, and it needs to be manually introduced and registered for the time being
```
// Import library files
import "trpc.group/trpc-go/trpc-codec/grpc"
...
func main() {
```go
// Import library files
import "trpc.group/trpc-go/trpc-codec/grpc"
...
func main() {

s := trpc.NewServer()
s := trpc.NewServer()

pb.RegisterGreeterService(s, &greeterServiceImpl{})
pb.RegisterGreeterService(s, &greeterServiceImpl{})

s.Serve()
}
```
s.Serve()
}
```
- Modify the `greeter.go` file of the service interface, as follows:
- Modify the `greeter.go` file of the service interface, as follows:

```
// Package main is the main package.
package main
```go
// Package main is the main package.
package main

import (
"context"
import (
"context"

pb "trpc.group/trpc-go/trpc-codec/grpc/examples/hellogrpc/protocol"
)
pb "trpc.group/trpc-go/trpc-codec/grpc/examples/hellogrpc/protocol"
)

// SayHello ...
func (s *greeterServiceImpl) SayHello(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// new content
rsp.Msg = "hello grpc client: " + req.Msg
// SayHello ...
func (s *greeterServiceImpl) SayHello(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// new content
rsp.Msg = "hello grpc client: " + req.Msg

return nil
}
return nil
}

// SayHi ...
func (s *greeterServiceImpl) SayHi(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// new content
rsp.Msg = "hi grpc client: " + req.Msg
// SayHi ...
func (s *greeterServiceImpl) SayHi(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// new content
rsp.Msg = "hi grpc client: " + req.Msg

return nil
}
```
return nil
}
```

9. Compile: `go build`, will generate the executable file of `hellogrpc`.

10. Modify the protocol field under `service` in the startup configuration `trpc_go.yaml` file under the current path, from `trpc` to `grpc`:

```
service: # The service provided by the business service can have multiple
- name: trpc.test.hellogrpc.Greeter # service route name
ip: 127.0.0.1 # The service listens to the ip address. You can use the placeholder ${ip}, choose one of ip and nic, and give priority to ip
#nic: eth0
port: 8000 # Service listening port can use placeholder ${port}
network: tcp # Network monitoring type tcp/udp
protocol: grpc # Change to grpc
timeout: 1000 # Request maximum processing time, at milliseconds
```
```yaml
service: # The service provided by the business service can have multiple
- name: trpc.test.hellogrpc.Greeter # service route name
ip: 127.0.0.1 # The service listens to the ip address. You can use the placeholder ${ip}, choose one of ip and nic, and give priority to ip
#nic: eth0
port: 8000 # Service listening port can use placeholder ${port}
network: tcp # Network monitoring type tcp/udp
protocol: grpc # Change to grpc
timeout: 1000 # Request maximum processing time, at milliseconds
```
11. Start the service: `./hellogrpc &`

12. Execute tests with grpc-cli:

```
# view service
$ grpc_cli ls localhost:8000
grpc.reflection.v1alpha.ServerReflection
trpc.test.hellogrpc.Greeter
```
# View details of the Greeter service
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter -l
filename: hellogrpc.proto
package: trpc.test.hellogrpc;
service Greeter {
rpc SayHello(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
}
```
# See the details of the Greeter.SayHi method
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter.SayHi -l
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
```
# Debug Greeter.SayHi interface
$ grpc_cli call localhost:8000 'trpc.test.hellogrpc.Greeter.SayHi' "msg: 'I am a test.'"
msg: "hi grpc client: I am a test."
Rpc succeeded with OK status
```
```shell
# view service
$ grpc_cli ls localhost:8000
grpc.reflection.v1alpha.ServerReflection
trpc.test.hellogrpc.Greeter
# View details of the Greeter service
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter -l
filename: hellogrpc.proto
package: trpc.test.hellogrpc;
service Greeter {
rpc SayHello(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
}
# See the details of the Greeter.SayHi method
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter.SayHi -l
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
# Debug Greeter.SayHi interface
$ grpc_cli call localhost:8000 'trpc.test.hellogrpc.Greeter.SayHi' "msg: 'I am a test.'"
msg: "hi grpc client: I am a test."
Rpc succeeded with OK status
```

13. Write client code
Client code generated using grpc-go.
```
```shell
# Generate client code for grpc-go
$ protoc --go_out=plugins=grpc:. protocol/hellogrpc.proto
```

14. Use the grpc-stream method
See example for details

## Problem statement
See [examples](/examples/README.md) for details

## Related References

[grpc protocol](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md)
[http2 frame](https://http2.github.io/http2-spec/#FramingLayer)
[Full analysis of grpc protocol unpacking process](https://zhuanlan.zhihu.com/p/86075992)
[grpc protocol codec implementation](https://zhuanlan.zhihu.com/p/85176945)
144 changes: 68 additions & 76 deletions grpc/README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,114 +65,106 @@ service Greeter {

8. 编写业务逻辑:

- 修改 `main.go`,添加 `trpc-grpc` package,并且在 main 函数中注册:
- 修改 `main.go`,添加 `trpc-grpc` package,并且在 main 函数中注册:

> 后续会修改成 trpc 工具直接支持,暂时需要手动引入和注册
> 后续会修改成 trpc 工具直接支持,暂时需要手动引入和注册
```
//引入库文件
import "trpc.group/trpc-go/trpc-codec/grpc"
...
func main() {
```go
// 引入库文件
import "trpc.group/trpc-go/trpc-codec/grpc"
...
func main() {
s := trpc.NewServer()

s := trpc.NewServer()
pb.RegisterGreeterService(s, &greeterServiceImpl{})

pb.RegisterGreeterService(s, &greeterServiceImpl{})
s.Serve()
}
```
s.Serve()
}
```
- 修改 service 接口 `greeter.go` 文件,形如:
- 修改 service 接口 `greeter.go` 文件,形如:

```
// Package main is the main package.
package main
```go
// Package main is the main package.
package main

import (
"context"
import (
"context"

pb "trpc.group/trpc-go/trpc-codec/grpc/examples/hellogrpc/protocol"
)
pb "trpc.group/trpc-go/trpc-codec/grpc/examples/hellogrpc/protocol"
)

// SayHello ...
func (s *greeterServiceImpl) SayHello(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// 新增内容
rsp.Msg = "hello grpc client: " + req.Msg
// SayHello ...
func (s *greeterServiceImpl) SayHello(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// 新增内容
rsp.Msg = "hello grpc client: " + req.Msg

return nil
}
return nil
}

// SayHi ...
func (s *greeterServiceImpl) SayHi(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// 新增内容
rsp.Msg = "hi grpc client: " + req.Msg
// SayHi ...
func (s *greeterServiceImpl) SayHi(ctx context.Context, req *pb.HelloRequest, rsp *pb.HelloReply) error {
// implement business logic here ...
// 新增内容
rsp.Msg = "hi grpc client: " + req.Msg

return nil
}
```
return nil
}
```

9. 编译:`go build`,将会生成 `hellogrpc` 的可执行文件。

10. 修改当前路径下的启动配置 `trpc_go.yaml` 文件中的 `service` 下的 protocol 字段,从 `trpc` 修改为 `grpc`

```
service: #业务服务提供的 service,可以有多个
- name: trpc.test.hellogrpc.Greeter #service 的路由名称
ip: 127.0.0.1 #服务监听 ip 地址 可使用占位符 ${ip},ip 和 nic 二选一,优先 ip
#nic: eth0
port: 8000 #服务监听端口 可使用占位符 ${port}
network: tcp #网络监听类型 tcp udp
protocol: grpc #修改为 grpc
timeout: 1000 #请求最长处理时间 单位 毫秒
```
```yaml
service: #业务服务提供的 service,可以有多个
- name: trpc.test.hellogrpc.Greeter #service 的路由名称
ip: 127.0.0.1 #服务监听 ip 地址 可使用占位符 ${ip},ip 和 nic 二选一,优先 ip
#nic: eth0
port: 8000 #服务监听端口 可使用占位符 ${port}
network: tcp #网络监听类型 tcp udp
protocol: grpc #修改为 grpc
timeout: 1000 #请求最长处理时间 单位 毫秒
```
11. 启动服务:`./hellogrpc &`

12. 使用 grpc-cli 执行测试:

```
# 查看服务
$ grpc_cli ls localhost:8000
grpc.reflection.v1alpha.ServerReflection
trpc.test.hellogrpc.Greeter
```
# 查看 Greeter 服务的详细信息
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter -l
filename: hellogrpc.proto
package: trpc.test.hellogrpc;
service Greeter {
rpc SayHello(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
}
```
# 查看 Greeter.SayHi 方法的详细信息
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter.SayHi -l
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
```
# 调试 Greeter.SayHi 接口
$ grpc_cli call localhost:8000 'trpc.test.hellogrpc.Greeter.SayHi' "msg: 'I am a test.'"
msg: "hi grpc client: I am a test."
Rpc succeeded with OK status
```
```shell
# 查看服务
$ grpc_cli ls localhost:8000
grpc.reflection.v1alpha.ServerReflection
trpc.test.hellogrpc.Greeter
# 查看 Greeter 服务的详细信息
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter -l
filename: hellogrpc.proto
package: trpc.test.hellogrpc;
service Greeter {
rpc SayHello(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
}
# 查看 Greeter.SayHi 方法的详细信息
$ grpc_cli ls localhost:8000 trpc.test.hellogrpc.Greeter.SayHi -l
rpc SayHi(trpc.test.hellogrpc.HelloRequest) returns (trpc.test.hellogrpc.HelloReply) {}
# 调试 Greeter.SayHi 接口
$ grpc_cli call localhost:8000 'trpc.test.hellogrpc.Greeter.SayHi' "msg: 'I am a test.'"
msg: "hi grpc client: I am a test."
Rpc succeeded with OK status
```

13. 编写客户端代码
使用 grpc-go 生成的客户端代码。
```
```shell
# 生成 grpc-go 的客户端代码
$ protoc --go_out=plugins=grpc:. protocol/hellogrpc.proto
```

14. 使用 grpc—stream 方式
详见 example
## 问题说明
详见 [examples](/examples/README.zh_CN.md)

## 相关参考

[grpc protocol](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md)
[http2 frame](https://http2.github.io/http2-spec/#FramingLayer)
[grpc 协议解包过程全剖析](https://zhuanlan.zhihu.com/p/86075992)
[grpc 协议编解码实现](https://zhuanlan.zhihu.com/p/85176945)
File renamed without changes.

0 comments on commit f58f340

Please sign in to comment.