美文网首页
trpc-go 2022-05-05

trpc-go 2022-05-05

作者: 9_SooHyun | 来源:发表于2022-05-07 22:10 被阅读0次

trpc-go上手实践

参考了文档

https://km.woa.com/group/37444/articles/show/427086?kmref=search&from_page=1&no=1

https://km.woa.com/group/29073/articles/show/376902?kmref=search&from_page=1&no=1

下面记录主要实践过程和部分问题解决

devcloud的trpc-go环境配置

  • 配置devcloud的go语言环境(go语言安装 + go环境变量配置 + go env配置以拉取工蜂代码)

  • 安装TRPC-Go框架的两个必须依赖:protoc and proto-gen-go。(这两个插件建议安装在全局,这样不同的用户均可访问

    安装protoc插件:
    git clone https://github.com/protocolbuffers/protobuf
    # 进入从 git 拉下来的目录
    # 依次执行
    ./autogen.sh
    ./configure --prefix=/usr/local/protobuf   // 这里是把 protobuf 的安装路径设为 /usr/local/protobuf
    make -j8
    make install
    

    安装完成后,

    /etc/profile/usr/local/protobuf/bin 添加到 PATH 中

    再将以下内容也添加到 /etc/profile 中,最后 source /etc/profile

    export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/protobuf/include/
    export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/protobuf/include/
    export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
    
    安装proto-gen-go插件:
    # go get默认安装到 $GOPATH/bin 目录下 
    go get github.com/golang/protobuf/protoc-gen-go
    

    为了更通用一点,可将protoc-gen-go移到 /usr/local/bin 目录,并确保 /usr/local/bin加入了PATH

    # 移动
    cd $GOPATH/bin
    mv protoc-gen-go /usr/local/bin/
    # 之后把 /usr/local/bin 添加到 /etc/profile 的 export PATH 中,更新文件
    

安装完毕,shell键入protoc检测是否安装好

  • trpc工具安装(可选,建议安装)

    trpc-commandline安装(这里使用源码安装)
    git clone git@git.code.oa.com:trpc-go/trpc-go-cmdline.git
    cd trpc-go-cmdline
    make && make install  //如要卸载: make uninstall
    

    安装完毕后注意确认生成的二进制文件路径加入PATH

    trpc-cli安装(这里还是使用源码安装)

    go 1.16.4 直接install 会报错,提示是git.code.oa.com的https证书问题,但install前已在devcloud更新了CA证书,所以证书无效?

    # go install git.code.oa.com/trpc-go/trpc-cli@latest
    go install git.code.oa.com/trpc-go/trpc-cli@latest: unrecognized import path "git.code.oa.com/trpc-go/trpc-cli": https fetch: Get "https://git.code.oa.com/trpc-go/trpc-cli?go-get=1": x509: certificate has expired or is not yet valid: current time 2021-10-26T11:42:50+08:00 is after 2021-09-06T05:19:55Z
    

    由于devcloud的go env配置了公司内网提供的 GOPROXY、GOSUMDB 服务,因此go installgo get不再依赖git等工具,而是 GOPROXY直接通过https下载。而git.code.oa.com又存在https证书问题

    于是通过git clone git@git.woa.com:trpc-go/trpc-cli.git下载源码,绕过git.code.oa.com的https证书问题,然后cd trpc-cli & go install完成安装

    [root@VM-centos trpc-cli]# go install
    go: downloading git.code.oa.com/trpc-go/trpc-go v0.6.1
    go: downloading github.com/spf13/cobra v1.0.0
    go: downloading git.code.oa.com/eab/eab-go v0.0.0-20210226085346-e963c81e2ba4
    go: downloading git.code.oa.com/NGTest/gen-code v0.0.0-20201223052102-8e106233d4ce
    go: downloading git.code.oa.com/NGTest/ngtest-gen-data v1.0.13
    go: downloading github.com/bitly/go-simplejson v0.5.0
    go: downloading github.com/jhump/protoreflect v1.7.0
    go: downloading git.code.oa.com/NGTest/common/trpc_parser v0.0.0-20201210120858-c6c18b935237
    ...
    

    在go1.16中,The go install command can now install an executable at a specific version by specifying an @version suffix. When using this syntax, go install installs the command from that exact module version, ignoring any go.modfiles in the current directory and parent directories. (Without the @version suffix, go install continues to operate as it always has, building the program using the version requirements and replacements listed in the current module’s go.mod.). This is useful for installing executables without affecting the dependencies of the main module.

创建自己的第一个trpc项目

  • 建一个git项目。基于服务解耦的思想,每个服务应当对应一个git项目

  • go mod init 初始化,如

    [root@VM-165-116-centos trpc-helloword]# go mod init git.woa.com/trpc-go/wxxdemo
    go: creating new go.mod: module git.woa.com/trpc-go/wxxdemo
    
  • 编写proto协议文件

    示例

    syntax = "proto3";
    
    // package包名命名为三级trpc.app.server
    // app是一个业务项目分类{应用名},server是具体的进程{服务名},一定要注意! ! ! !
    // 该命名一定要谨慎,后期服务上线也要用到该名称,要保持高度一致,否则可能出现各种奇怪的上线编译错误
    package trpc.wxx.helloworld;
    
    // 必须指定 option go_package,表明协议的git地址(这是啥意思?)
    option go_package="git.code.oa.com/trpcprotocol/test/helloworld";
    
    // 定义service提供的若干rpc方法,一个server可以有多个service,但一般都是一个server一个service
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply) {}
      rpc SayBye (HelloRequest) returns (HelloReply) {}
    }
    
    message HelloRequest {
      string msg = 1;
    }
    
    message HelloReply {
      string msg = 1;
    }
    
  • trpc create -p wxx_helloworld.proto
    

    会生成stub目录(里面存放协议文件),main.go,greeter.go(接口实现文件),greeter_test.go(客户端测试文件)以及trpc_go.yaml(trpc服务配置文件)

  • 填充接口实现文件greeter.go和程序入口main文件,提示一些依赖没装的话直接go mod tidy一下

  • 完成后go build生成可执行文件

  • 测试

    使用trpc-cli工具测试:

    [root@VM-165-116-centos ~]# trpc-cli -func /trpc.wxx.helloworld.Greeter/SayHello -target ip://127.0.0.1:8000 -body '{"msg":"hello"}'
    Current version: v2.0.1, build hash: v2.0.1
    Latest  version: v2.2.15, latest build hash: 961de7b66375e47802ace96f3828d84dcc9ca899 
    
    
    [rsp json body]:{"msg":"hello from wxx: hello"}
    [root@VM-165-116-centos ~]#
    [root@VM-165-116-centos ~]# trpc-cli -func /trpc.wxx.helloworld.Greeter/SayBye -target ip://127.0.0.1:8000 -body '{"msg":"hello"}'
    Current version: v2.0.1, build hash: v2.0.1
    Latest  version: v2.2.15, latest build hash: 961de7b66375e47802ace96f3828d84dcc9ca899 
    
    
    [rsp json body]:{"msg":"bye from wxx: hello"}
    [root@VM-165-116-centos ~]#
    

    如果.yaml配置的server项支持了http协议,则可以通过curl测试

    .yaml配置

    global:                             #全局配置
      namespace: Development            #环境类型,分正式production和非正式development两种类型
      env_name: test                    #环境名称,非正式环境下多环境的名称
    
    server:                                            #服务端配置
      app: wxx                                        #业务的应用名
      server: helloworld                             #进程服务名
      bin_path: /usr/local/trpc/bin/                   #二进制可执行文件和框架配置文件所在路径
      conf_path: /usr/local/trpc/conf/                 #业务配置文件所在路径
      data_path: /usr/local/trpc/data/                 #业务数据文件所在路径
      filter:                                          #针对所有service处理函数前后的拦截器列表
        - simpledebuglog
        - recovery                                     #拦截框架创建的业务处理协程panic
      service:                                         #业务服务提供的service,可以有多个
        - name: trpc.wxx.helloworld.Greeter      #service的路由名称
          ip: 127.0.0.1                            #服务监听ip地址 可使用占位符 ${ip},ip和nic二选一,优先ip
          #nic: eth0
          port: 8000                #服务监听端口 可使用占位符 ${port}
          network: tcp                             #网络监听类型  tcp udp
          protocol: trpc               #应用层协议 trpc http
          timeout: 1000                            #请求最长处理时间 单位 毫秒
        - name: trpc.wxx.helloworld.Greeter1      #service的路由名称
          ip: 127.0.0.1                            #服务监听ip地址 可使用占位符 ${ip},ip和nic二选一,优先ip
          #nic: eth0
          port: 8080                #服务监听端口 可使用占位符 ${port}
          network: tcp                             #网络监听类型  tcp udp
          protocol: http               #应用层协议 trpc http
          timeout: 1000                            #请求最长处理时间 单位 毫秒
        
    
    client:                                            #客户端调用的后端配置
      timeout: 1000                                    #针对所有后端的请求最长处理时间
      namespace: Development                           #针对所有后端的环境
      filter:                                          #针对所有后端调用函数前后的拦截器列表
      service:                                         #针对单个后端的配置
        - name: trpc.wxx.helloworld.Greeter      #后端服务的service name
          namespace: Development                   #后端服务的环境
          network: tcp                             #后端服务的网络类型 tcp udp 配置优先
          protocol: trpc               #应用层协议 trpc http
          target: ip://127.0.0.1:8000              #请求服务地址
          timeout: 1000                            #请求最长处理时间
        
    
    plugins:                                          #插件配置
      log:                                            #日志配置
        default:                                      #默认日志的配置,可支持多输出
          - writer: console                           #控制台标准输出 默认
            level: debug                              #标准输出日志的级别
          - writer: file                              #本地文件日志
            level: info                               #本地文件滚动日志的级别
            writer_config:
              filename: ./trpc.log                      #本地文件滚动日志存放的路径
              max_size: 10                              #本地文件滚动日志的大小 单位 MB
              max_backups: 10                           #最大日志文件数
              max_age: 7                                #最大日志保留天数
              compress:  false                          #日志文件是否压缩
    
    curl -X POST -d '{"msg":"hello"}' -H "Content-Type:application/json" http://127.0.0.1:8080/trpc.wxx.helloworld.Greeter/SayHello
    

相关文章

网友评论

      本文标题:trpc-go 2022-05-05

      本文链接:https://www.haomeiwen.com/subject/tdcoyrtx.html