# < go 1.16goget-ugithub.com/TarsCloud/TarsGo/tars/tools/tarsgogoget-ugithub.com/TarsCloud/TarsGo/tars/tools/tars2go# >= go 1.16goinstallgithub.com/TarsCloud/TarsGo/tars/tools/tarsgo@latestgoinstallgithub.com/TarsCloud/TarsGo/tars/tools/tars2go@latest
tarsgo make App Server Servant GoModuleName
例如:
tarsgo make TestApp HelloGo SayHello github.com/Tars/test
命令执行后将生成代码当前目录中以Server命名目录,生成代码中也有提示具体路径。
[root@1-1-1-1 ~]# tarsgo make TestApp HelloGo SayHello github.com/Tars/test
🚀 Creating server TestApp.HelloGo, layout repo is https://github.com/TarsCloud/TarsGo.git, please wait a moment.
已经是最新的。
go: creating new go.mod: module github.com/Tars/test
go: to add module requirements and sums:
go mod tidy
CREATED HelloGo/SayHello.tars (171 bytes)
CREATED HelloGo/SayHello_imp.go (620 bytes)
CREATED HelloGo/client/client.go (444 bytes)
CREATED HelloGo/config.conf (967 bytes)
CREATED HelloGo/debugtool/dumpstack.go (412 bytes)
CREATED HelloGo/go.mod (37 bytes)
CREATED HelloGo/main.go (517 bytes)
CREATED HelloGo/makefile (193 bytes)
CREATED HelloGo/scripts/makefile.tars.gomod (4181 bytes)
CREATED HelloGo/start.sh (56 bytes)
>>> Great!Done! You can jump in HelloGo
>>> Tips: After editing the Tars file, execute the following cmd to automatically generate golang files.
>>> /root/gocode/bin/tars2go *.tars
$ cd HelloGo
$ ./start.sh
🤝 Thanks for using TarsGo
📚 Tutorial: https://tarscloud.github.io/TarsDocs/
packagemainimport ("github.com/TarsCloud/TarsGo/tars""github.com/Tars/test/tars-protocol/TestApp")funcmain() {// Get server config cfg := tars.GetServerConfig()// New servant imp imp :=new(SayHelloImp)// New servant app :=new(TestApp.SayHello)// Register Servant app.AddServantWithContext(imp, cfg.App+"."+cfg.Server+".SayHelloObj")// Run application tars.Run()}
编译生成可执行文件,并打包发布包。
cdHelloGo&&make&&maketar
将生成可执行文件 HelloGo 和发布包 HelloGo.tgz
客户端开发
packagemainimport ("fmt""github.com/TarsCloud/TarsGo/tars""github.com/Tars/test/tars-protocol/TestApp")//只需初始化一次,全局的var comm *tars.Communicatorfuncmain() { comm = tars.NewCommunicator() obj :="TestApp.HelloGo.SayHelloObj@tcp -h 127.0.0.1 -p 10015 -t 60000" app :=new(TestApp.SayHello)/* // if your service has been registered at tars registry obj := "TestApp.HelloGo.SayHelloObj" // tarsregistry service at 192.168.1.1:17890 comm.SetProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 192.168.1.1 -p 17890") */ comm.StringToProxy(obj, app) reqStr :="tars"var resp string ret, err := app.EchoHello(reqStr, &resp)if err !=nil { fmt.Println(err)return } fmt.Println("ret: ", ret, "resp: ", resp)}