遇到的坑
README.md很糟心,有以下问题
- 代码错误(根本编译不过);
- 配置文件错误;
- 帮助文档和实际生成的代码并不致【毕竟刚开源,不完善算是正常现象】。
让第一个Demo跑起来
安装
环境信息
- 操作系统:CentOS Linux release 7.3.1611 (Core)
- golang版本:go1.9.2 linux/amd64
- 内核:Linux 3.10.0-514.21.1.el7.x86_64
- gcc版本:gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
- 已正确配置GOPATH环境变量
TarsGo
$ go get -u -v github.com/TarsCloud/TarsGo/tars
github.com/TarsCloud/TarsGo (download)
github.com/TarsCloud/TarsGo/tars/protocol/res/basef
github.com/TarsCloud/TarsGo/tars/util/gpool
github.com/TarsCloud/TarsGo/tars/util/tools
github.com/TarsCloud/TarsGo/tars/util/set
github.com/TarsCloud/TarsGo/tars/util/rtimer
github.com/TarsCloud/TarsGo/tars/protocol/codec
github.com/TarsCloud/TarsGo/tars/util/current
github.com/TarsCloud/TarsGo/tars/util/rogger
github.com/TarsCloud/TarsGo/tars/util/conf
github.com/TarsCloud/TarsGo/tars/util/debug
github.com/TarsCloud/TarsGo/tars/protocol/res/requestf
github.com/TarsCloud/TarsGo/tars/protocol/res/endpointf
github.com/TarsCloud/TarsGo/tars/transport
github.com/TarsCloud/TarsGo/tars/util/endpoint
github.com/TarsCloud/TarsGo/tars/model
github.com/TarsCloud/TarsGo/tars/protocol/res/adminf
github.com/TarsCloud/TarsGo/tars/protocol/res/configf
github.com/TarsCloud/TarsGo/tars/protocol/res/logf
github.com/TarsCloud/TarsGo/tars/protocol/res/nodef
github.com/TarsCloud/TarsGo/tars/protocol/res/notifyf
github.com/TarsCloud/TarsGo/tars/protocol/res/propertyf
github.com/TarsCloud/TarsGo/tars/protocol/res/queryf
github.com/TarsCloud/TarsGo/tars/protocol/res/statf
github.com/TarsCloud/TarsGo/tars
tars2go
将tars接口文件转换成go目标文件的工具,必须要安装
$ cd $GOPATH/src/github.com/TarsCloud/TarsGo/tars/tools/tars2go && go build && go install
测试tars2go
$ tars2go
Usage: tars2go [flags] *.tars
tars2go -I tars/protocol/res/endpoint [-I ...] QueryF.tars
-E Generate code before fmt for troubleshooting
-I value
Specify a specific import path
-add-servant
Generate AddServant function (default true)
-outdir string
which dir to put generated code
-tarsPath string
Specify the tars source path. (default "github.com/TarsCloud/TarsGo/tars")
如果提示的是“command not found”,则安装异常
生成第一个测试Demon
TarsGo比较人性化,提供了一个生成Demo的工具
$ cd $GOPATH/src/github.com/TarsCloud/TarsGo/tars/tools
$ ./create_tars_server.sh TestApp HelloGo SayHello
[create server: TestApp.HelloGo ...]
[mkdir: /home/luojie/gopath//src/TestApp/HelloGo/]
>>>Now doing:./Servant.tars >>>>
>>>Now doing:./start.sh >>>>
>>>Now doing:./Server.go >>>>
>>>Now doing:./Server.conf >>>>
>>>Now doing:./makefile >>>>
>>>Now doing:./ServantImp.go >>>>
>>>Now doing:client/client.go >>>>
>>>Now doing:vendor/vendor.json >>>>
>>>Now doing:debugtool/dumpstack.go >>>>
>>> Great!Done! You can jump in /home/luojie/gopath/src/TestApp/HelloGo
>>> Tips: After editing the Tars file, execute the following cmd to automatically generate golang files.
>>> /home/luojie/gopath//bin/tars2go *.tars
查看生成的代码
如果生成的步骤正常,将会在$GOPATH/src/目录下面的生成TestApp目录
目录结构如下:
$ tree
.
`-- HelloGo
|-- client
| `-- client.go
|-- debugtool
| `-- dumpstack.go
|-- HelloGo
|-- HelloGo.conf
|-- HelloGo.go
|-- HelloGo.tgz
|-- makefile
|-- SayHelloImp.go
|-- SayHello.tars
|-- start.sh
|-- TestApp
| `-- SayHello_IF.go
`-- vendor
|-- _APP_
| `-- _SERVANT__IF.go
|-- TestApp
| `-- SayHello_IF.go
`-- vendor.json
7 directories, 14 files
说明
- SayHello.tars是接口文件,编译时由tars2go生成go文件
- HelloGo.conf是服务启动的的配置文件[先可以不用理会]
- start.sh是服务启动脚本,会先执行make,再启动服务
完善实现
$ cat SayHelloImp.go
package main
type SayHelloImp struct {
}
func (imp *SayHelloImp) Add(a int32, b int32, c *int32) (int32, error) {
//Doing something in your function
//...
return 0, nil
}
func (imp *SayHelloImp) Sub(a int32, b int32, c *int32) (int32, error) {
//Doing something in your function
//...
return 0, nil
}
帮助手册让我们生成的SayHello程序,接口竟然变成了Add和Sub,所以,看到SayHello的时候,不要觉得奇怪
请自行补充缺少的代码
编译代码
$ cd HelloGo/
$ make && make tar
/home/luojie/gopath//bin/tars2go -outdir=vendor SayHello.tars
/home/luojie/software/go//bin/go build -o HelloGo
/home/luojie/gopath//bin/tars2go -outdir=vendor SayHello.tars
/home/luojie/software/go//bin/go build -o HelloGo
HelloGo/
HelloGo/client/
HelloGo/client/client.go
HelloGo/HelloGo
‘HelloGo.tgz’ -> ‘HelloGo.20190122213710.tgz’
tar cvfz HelloGo.tgz ...
出现以上的结果,说明编译正常。
启动服务
可直接运行start.sh或者执行以下命令,让服务在后台启动
$ ./HelloGo --config=HelloGo.conf &
注意:直接运行start.sh时,会给人一种假像,好像卡住了
启动客户端
$ cd client
$ go run client.go
0 369
个人感受
- 虽然帮助文档让人很蛋疼
- 但是提供的这些工具功能还是非常强大的
- 可以让新手最快体验tarsGo带来的便利
写在最后
- 如果你的代码无法正常编译,请根据平台调整相关参数,或者手动编译【例如:ubuntu18.04中make时会出错】
- 遇到问题时,请仔细阅读错误信息,你遇到的问题,万千网友早已遇到,百度/谷歌可以找到答案
有疑问加站长微信联系(非本文作者)