# thrift_tutorial_go **Repository Path**: walter80/thrift_tutorial_go ## Basic Information - **Project Name**: thrift_tutorial_go - **Description**: thrift go教程 - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-08-07 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Thrift的灵感来源于pillar, 一款由Adam D’Angelo编写的轻量级RPC工具, 同时也借鉴了Google的protocol buffers。 A RPC server and client using Thrift in Golang. ## Requirements - [GoConvey](https://github.com/smartystreets/goconvey) - [Thrift](https://thrift.apache.org/) - [Thrift Golang client](https://github.com/apache/thrift/tree/master/lib/go) ## Tests ```bash go test -v ``` ## 介绍 [《Apache Thrift官方文档》简介](http://ifeve.com/%E3%80%8Aapache-thrift%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3%E3%80%8B%E7%AE%80%E4%BB%8B/) ## 安装教程 ./go-tutorial -server=true -secure=true ./go-tutorial -secure=true GO = /usr/local/bin/go ~/thrift/tutorial/go   master ●  make check ~/thrift/tutorial/go   master ●  ./go-tutorial -server=true *thrift.TServerSocket Starting the simple server... on localhost:9090 ping() add(1,1) calculate(1, {DIVIDE,1,0}) calculate(1, {SUBTRACT,15,10}) getStruct(1) ~/thrift/tutorial/go   master ●  ./go-tutorial ping() 1+1=2 Invalid operation: InvalidOperation({WhatOp:4 Why:Cannot divide by 0}) 15-10=5 Check log: 5 export GO111MODULE=auto export GOPATH=~/GolandProjects/thrift_tutorial_go/tutorial/go/ make check to create a module there, run: cd ../.. && go mod init 'go mod init example.com/m' to initialize a v0 or v1 module 'go mod init example.com/m/v2' to initialize a v2 module `~/GolandProjects/thrift_tutorial_go   master ●✚  go run main.go` Starting the simple server... on localhost:9090 export GOPROXY=https://goproxy.cn export GOPATH=~/GolandProjects/thrift_tutorial_go/IDL go build EchoServerImp.go go build EchoClient.go 生成go代码:thrift -o -gen go {对应版本的hbase源码地址}\src\main\resources\org\apache\Hadoop\hbase\thrift2 将对应版本中golang接口code复制到当前golang安装目录,{$GOROOT}\src或者{$GOPATH}\src… 再通过git获取外部资源git.apache.org/thrift.git/lib/go/thrift : go get git.apache.org/thrift.git/lib/go/thrift 或者直接到 https://github.com/apache/thrift 直接下载zip包,将至放在$GPPATH\src\git.apache.org\thrift.git\ 目录下即可。 GOPATH="/Users/wolingzong/go" export GOPATH=~/GolandProjects/thrift_tutorial_go/tutorial/go/:~/GolandProjects/thrift_tutorial_go/IDL export GOROOT=~/GolandProjects/thrift_tutorial_go/ export CGO_ENABLED=0 go test -v 什么时候要选择使用 Thrift 1.高并发 2.请求和响应的数据传输量大 3.业务系统跨多种语言 thrift -version -->>> Thrift version 0.14.0 > IDE插件 *1、JetBrains PhpStorm 可以在插件里找到Thrift Support安装,重启IDE后就支持Thrift格式语法了。 *2、VScode 在扩展里搜索 Thrift,安装即可。 ## 编译 go install ## 一切顺利的话,这里就在 $GOPATH/bin/hbaseThriftTest 的可执行文件,记得把 $GOPATH/bin 加入到 $PATH 变量中 `thrift --gen go timeRPC.thrift` `thrift -r --gen go tutorial.thrift` `thrift --gen go shared.thrift` ``` thrift -r --gen go HelloThrift.thrift brew info go brew install go go build main.go server.go handler.go client.go ./main -server=true ``` ~/thrift/tutorial/go/ https://github.com/apache/thrift/blob/master/tutorial/go/Makefile.am 首先我们初始化go mod: go mod init sample 首先是安装golang的库: go get git.apache.org/thrift.git/lib/go/thrift/... go get git.apache.org/thrift.git/lib/go/thrift thrift -gen go RpcService.thrift go get github.com/apache/thrift/lib/go/thrift go get git.apache.org/thrift.git/lib/go/thrift go get github.com/apache/thrift/lib/go/thrift/... go install git.apache.org/thrift.git/lib/go/thrift go get git.apache.org/thrift.git/lib/go/thrift "github.com/apache/thrift/lib/go/thrift" ##测试 `go run server.go` `go test -v` export http_proxy=socks5://127.0.0.1:1086\nexport https_proxy=$http_proxy\nexport all_proxy=$http_proxy go get git.apache.org/thrift.git/lib/go/thrift https://sookocheff.com/post/thrift/so-you-want-to-use-thrift/ go build main.go client.go handler.go server.go ✘  ~/GolandProjects/thrift_tutorial_go/thrift/tutorial_sample   master ●✚  ./main -server=true *thrift.TServerSocket Starting the simple server... on localhost:9090 ping() add(1,1) calculate(1, {DIVIDE,1,0}) calculate(1, {SUBTRACT,15,10}) getStruct(1) ✘  ~/GolandProjects/thrift_tutorial_go/thrift/tutorial_sample   master ●✚  ./main ping() 1+1=2 Invalid operation: InvalidOperation({WhatOp:4 Why:Cannot divide by 0}) 15-10=5 Check log: 5 1.最简单的go电商网站 访问 http://localhost:4040/ 正常显示 Let's Shopping! 2.mysql访问 3.go的Cgo使用 下载win环境下gcc下载:TDM gcc,安装后会自动加入到PATH下,cmd里就能使用gcc命令。 http://downloads.sourceforge.net/project/tdm-gcc/TDM-GCC%20Installer/tdm-gcc-4.8.1-3.exe?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Ftdm-gcc%2F&ts=1391271291&use_mirror=ncu 05.golang访问mongodb4.0 下载monggo驱动 git clone http://gopkg.in/mgo.v2 运行输出: G:/work/go/05_mongo/05_mongo.exe [G:/work/go/05_mongo] Name: superWang Phone: 13478808311 成功: 进程退出代码 0.