腾讯trpc-go教程——一文搞懂trpc框架
前言
最近腾讯开源了trpc框架,小编心血来潮就去研究了以下,现在将研究结果分享给大家。
安装trpc
首先将以下内容添加到你的 ~/.gitconfig
中:
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
然后执行
go install trpc.group/trpc-go/trpc-cmdline/trpc@latest
如果报错了可以尝试一下的代理地址:
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPROXY=https://goproxy.io,direct
go env -w GOPROXY=https://goproxy.baidu.com/
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/
执行一下命令查看是否安装成功
trpc version
#打印版本号就是成功了
trpc-group/trpc-cmdline version: v1.0.5
然后执行一下代码,安装下依赖
trpc setup
安装protoc
我们可以点击protoc工具下载链接,目前的最新版本是v25.0
大家可以根据自己需求安装。安装之后配置下环境变量。
然后执行:
protoc --version
查看下版本号,如果没有输出版本号就是没安装成功或者环境变量没配好。
第一个trpc程序
创建项目目录
创建一下的目录结构,模拟项目操作
编写helloworld.proto文件
syntax = "proto3";
package helloworld;
option go_package = "github.com/some-repo/examples/helloworld";
// HelloRequest is hello request.
message HelloRequest {
string msg = 1;
}
// HelloResponse is hello response.
message HelloResponse {
string msg = 1;
}
// HelloWorldService handles hello request and echo message.
service HelloWorldService {
// Hello says hello.
rpc Hello(HelloRequest) returns(HelloResponse);
}
执行命令
trpc create -p helloworld.proto -o out
cd out
go run .
执行命令后会生成以下文件目录
trpc_go.yaml是trpc的配置文件
global: # Global configuration.
namespace: Development # Environment type, either Production or Development.
env_name: test # Environment name for non-production environments.
server: # Server configuration.
app: yourAppName # Application name for the business.</