RPC框架Thrift初探实践
最近在学习rpc框架,虽然主要是学习grpc 但是 作为竞品也来试试thrift 。发现facebook真的不愧被业界成为管拉不管擦的典范,文档很不友好,grpc的文档示例顺畅多了。另外也对自己的奶头习惯,拿来习惯要引以为戒,不能学习一个东西都要别人嚼碎了来喂。其实文档自己探索下也是可以用的。
这里来记录下thrift的流程
官方地址:
https://thrift.apache.org/
https://github.com/apache/thrift
编译器:因为我是mac 直接用brew 安装了,其他的官方文档有
brew install thrift
本来准备用php来搞得,结果发现php体验太差,最后还是用go来弄
先来看一下官方的go文档 这就说到上面我的浮躁的心看不太下去 看了好几遍
# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # gen-go/tutorial/calculator.go gen-go/shared/shared_service.go: $(top_srcdir)/tutorial/tutorial.thrift $(THRIFT) --gen go$(COMPILER_EXTRAFLAG) -r $< all-local: gen-go/tutorial/calculator.go check: src/github.com/apache/thrift/lib/go/thrift thirdparty-dep $(THRIFT) -r --gen go$(COMPILER_EXTRAFLAG) $(top_srcdir)/tutorial/tutorial.thrift cp -r gen-go/* src/ GOPATH=`pwd` $(GO) build -o go-tutorial ./src GOPATH=`pwd` $(GO) build -o calculator-remote src/tutorial/calculator-remote/calculator-remote.go src/github.com/apache/thrift/lib/go/thrift: mkdir -p src/github.com/apache/thrift/lib/go ln -sf $(realpath $(top_srcdir)/lib/go/thrift) src/github.com/apache/thrift/lib/go/thrift thirdparty-dep: tutorialserver: all GOPATH=`pwd` $(GO) run src/*.go -server=true tutorialclient: all GOPATH=`pwd` $(GO) run src/*.go tutorialsecureserver: all GOPATH=`pwd` $(GO) run src/*.go -server=true -secure=true tutorialsecureclient: all GOPATH=`pwd` $(GO) run src/*.go -secure=true clean-local: $(RM) -r gen-* src/shared src/tutorial src/git.apache.org go-tutorial calculator-remote EXTRA_DIST = \ src/client.go \ src/handler.go \ src/server.go \ src/main.go \ server.crt \ server.key
下面按照我的说一下
首先下载
https://github.com/apache/thrift 如果自己能看得下去那么 按照官方的说法已经够用了
Tutorial ======== 1) First things first, you'll need to install the Thrift compiler and the language libraries. Do that using the instructions in the top level README.md file. 2) Read tutorial.thrift to learn about the syntax of a Thrift file 3) Compile the code for the language of your choice: ``` $ thrift $ thrift -r --gen cpp tutorial.thrift ``` 4) Take a look at the generated code. 5) Look in the language directories for sample client/server code. 6) That's about it for now. This tutorial is intentionally brief. It should be just enough to get you started and ready to build your own project.
下载thrift 框架
git clone git@github.com:apache/thrift.git
这个是thrift的全部文件包含源码和各个语言的lib库和tutorial示例
需要用到thrift的go库文件
先来展示下 tutorial的目录
其中 gen-go目录 用thrift的生成器生成对应代码
thrift -r --gen go tutorial.thrift
更改其中的import的包路径到当前项目的路径 然后就
这边的client和server是一个文件 根据参数-server=true 区分
在当前目录下编译并启动server
go build
运行客户端
最近疫情在家学习效率不太高。不过已经万事从一个helloworld 开始把。会去研究grpc了