trpc-group / trpc-cpp

A pluggable, high-performance RPC framework written in cpp
Other
267 stars 77 forks source link

【腾讯犀牛鸟计划】分别使用tRPC-Cpp的三种序列化方式完成对tRPC服务的调用 #136

Open weimch opened 2 months ago

weimch commented 2 months ago

issue介绍

tRPC-Cpp提供了多种消息(反)序列化方式,以应对业务丰富多样的场景,比如http通常用json来交互,rpc通常用protobuf来交互,你甚至可以自定义一种消息的序列化反序列化格式用noop,然后自己做序列化/反序列化。

你需要使用不同的(反)序列化方法,完成对tRPC服务的调用,并用腾讯文档输出一份报告到issue回复里,报告需要包含你的实践过程(关键的代码以及日志输出等)以及你的心得体会,最后提交相关变更代码到你个人的repo以及编译运行指引到issue回复里,方便我们测试验证你的确完成了issue。

具体地,你可以按下面的步骤完成此issue 1、交互的消息可以按json格式来表达: { name: "issueshooter", age: 18, hobby: ["opensource project","movies", "books"] } 2、改写examples/helloworld的proto文件及相应代码,让其传递1中的信息 3、改写examples/features/trpc_json传递的json内容,让其传递1中的信息 4、改写examples/features/trpc_noop传递的内容,你可以按自己的想法,合理地设计一套规则,在客户端将1拼接成一个字符串,在服务端从这个字符串解出信息

参考资料

其他说明

本issue为2024犀牛鸟开源人才培养活动专属issue,仅供在校大学生参与领取 【认领issue】在研学基地"issue营地"对应issue行的M~R列抢滩报名,即视为认领成功。 【完成issue】已认领issue的同学,请同步在本issue评论区回复“已成功领取本issue”; 如7天内无提交任何进展包括不限于comment \ commit \ Pull Request,则将视为同学主动放弃issue,组委会将释放issue给下一位等候者。

Vitalis001 commented 1 month ago

文档: https://github.com/Vitalis001/trpc-cpp/blob/main/docs/zh/trpc_protobuf_json_noop_guide.md

代码仓库: https://github.com/Vitalis001/trpc-cpp

编译运行:

protobuf:

编译整个示例代码

bazel build //examples/helloworld/...

运行服务端程序

./bazel-bin/examples/helloworld/helloworld_svr --config=./examples/helloworld/conf/trpc_cpp_fiber.yaml

打开另外一个终端,运行客户端测试程序。

./bazel-bin/examples/helloworld/test/fiber_client --client_config=./examples/helloworld/test/conf/trpc_cpp_fiber.yaml

或者
./bazel-bin/examples/helloworld/test/future_client --client_config=./examples/helloworld/test/conf/trpc_cpp_future.yaml

将看到如下输出

FLAGS_service_name:trpc.test.helloworld.Greeter
FLAGS_client_config:./examples/helloworld/test/conf/trpc_cpp_fiber.yaml
get rsp: 
 name: hello issueshooter
 age: 19
 hobby: opensource project, movies, books

或者
FLAGS_service_name:trpc.test.helloworld.Greeter
FLAGS_client_config:./examples/helloworld/test/conf/trpc_cpp_future.yaml
get rsp:
 name: hello issueshooter
 age: 19
 hobby: opensource project, movies, books

使用 cat helloworld_fiber.log命令查看服务端的日志信息,可以看到服务端接收到客户端如下信息:

request name: issueshooter
request age: 18
request hobby: opensource project, movies, books
json

编译示例代码

bazel build //examples/features/trpc_json/...

运行服务端程序

./bazel-bin/examples/features/trpc_json/server/demo_server --config=./examples/features/trpc_json/server/trpc_cpp_fiber.yam

运行客户端程序

./bazel-bin/examples/features/trpc_json/client/client --client_config=./examples/features/trpc_json/client/trpc_cpp_fiber.yaml

将看到如下输出

FLAGS_target:trpc.test.helloworld.demo_service
FLAGS_client_config:./examples/features/trpc_json/client/trpc_cpp_fiber.yaml
son name: name, value: hello issueshooter
json name: age, value: 19
json name: hobby, value: opensource project,movies,book
noop

编译示例代码

bazel build //examples/features/trpc_noop/...

运行服务端程序

./bazel-bin/examples/features/trpc_noop/server/demo_server --config=./examples/features/trpc_noop/server/trpc_cpp_fiber.yaml

运行客户端程序

./bazel-bin/examples/features/trpc_noop/client/client --client_config=./examples/features/trpc_noop/client/trpc_cpp_fiber.yaml

将看到如下输出


FLAGS_target:trpc.test.helloworld.demo_service
FLAGS_client_config:./examples/features/trpc_noop/client/trpc_cpp_fiber.yaml
logging plugin configurations is not setted, log will be printed to the console.
response msg: {"age":19,"hobby":["opensource project","movies","books"],"name":"hello issueshooter"}
response msg: {"age":20,"hobby":["opensource project","movies","books"],"name":"bye issueshooter"}
weimch commented 1 month ago

Noop的测试编译失败了,同时文档缺少心得体会部分,可以补充上

image
weimch commented 1 month ago

另外,这个issue对于noop的功能,更愿意看到noop数据格式由你自己组织/解析,而不是复用json(因为已经有了json传输的能力)