导语:在进行本文实践之前,需要先完成TARS架的搭建,参考TARS框架部署文档。在我们的实际应用中,目前基于 TARS,主要采用 Node.js 和 C++ 进行开发。对于 C++ 程序员来说,目前最热门的后台开发语言莫过于 Google 的 Go。TARS 框架最新的版本已经把内部的 TafGo 开源为 TarsGo。作为与时俱进的程序员,当然要尝鲜啦。
环境准备
GO环境
1.91.14$GOPATH$GOROOT$HOME/goGo 1.8go$ go env
TarsGo包
go
go get github.com/TarsCloud/TarsGo/tars
cd $GOPATH/src/github.com/TarsCloud/TarsGo/tars/tools/tars2go && go build .
sudo cp tars2go $GOPATH/bin/
上述命令会把 TarsGo 下载下来,并且将比较重要的一个命令 tars2go 安装好。同时需要注意cp命令目的路径参数最后的斜杆不要漏了,漏了会把tars2go重命名为bin
代码设计
TarsGo 的官方 Quick Start 文档 的第一个例子,就是使用 tars 协议进行 server-client 的通信。不过我个人觉得,要说后台服务程序的 hello world 的话,第一个应该是 http 服务嘛,毕竟程序一运行就可以看到效果,这才是 hello world 嘛。
给服务命名
tarsstattars.tarsstat.StatObj
amc.GoWebServer.GoWebObj
创建基础框架
create_tars_server.shcreate_http_server.sh
cd $GOPATH/src/github.com/TarsCloud/TarsGo/tars/tools
chmod +x create_tars_server.sh
./create_tars_server.sh amc GoWebServer GoWeb
执行后我们可以查看生成的文件,清除不需要的:
cd $GOPATH/src/amc/GoWebServer
rm -rf GoWeb.tars client debugtool
chmod +x start.sh
ls -l
文件如下
total 44
-rwxr-xr-x. 1 root root 955 7月 23 01:53 config.conf
-rwxr-xr-x. 1 root root 604 7月 23 01:53 goweb_imp.go
-rwxr-xr-x. 1 root root 365 7月 23 02:02 main.go
-rw-r--r--. 1 root root 251 7月 23 01:53 makefile
-rwxr-xr-x. 1 root root 52 7月 23 01:53 start.sh
drwxr-xr-x. 3 root root 36 7月 23 02:02 vendor
makefilemakefile
APP := amc
TARGET := GoWebServer
MFLAGS :=
DFLAGS :=
CONFIG := client
STRIP_FLAG:= N
J2GO_FLAG :=
libpath=${subst :, ,$(GOPATH)}
$(foreach path,$(libpath),$(eval -include $(path)/src/github.com/TarsCloud/TarsGo/tars/makefile.tars))
CONFIG := client
代码修改
main.go
main.gogoweb_imp.goimpmain.go
package main
import (
"github.com/TarsCloud/TarsGo/tars"
)
func main() {
mux := &tars.TarsHttpMux{}
mux.HandleFunc("/", HttpRootHandler)
cfg := tars.GetServerConfig()
tars.AddHttpServant(mux, cfg.App+"."+cfg.Server+".GoWebObj") //Register http server
tars.Run()
}
代码还是比较简单的,无需多言。
goweb_imp.go
main.goHTTPRootHandlergoweb_imp.go
package main
import (
"fmt"
"time"
"net/http"
)
func HttpRootHandler(w http.ResponseWriter, r *http.Request) {
time_fmt := "2006-01-02 15:04:05"
local_time := time.Now().Local()
time_str = local_time.Format(time_fmt)
ret_str = fmt.Sprintf("{\"msg\":\"Hello, Tars-Go!\", \"time\":\"%s\"}", time_str)
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Write([]byte(ret_str))
return
}
部署发布
编译打包
编译打包上面的工程:
cd $GOPATH/src/amc/GoWebServer
make && make tar
GoWebServer.tgz
部署发布
创建服务
在 TARS 管理平台主页中,点击 “运维管理”,界面如下:
appserverobjectamc.GoWebServer.GoWebObj
amcGoWebServertars_gotars.go.defaultGoWebObjTCP非TARS
确定
发布服务
GoWebServer发布管理服务列表发布选中节点
再点击 “上传发布包”,进入如下界面:
点击 “发布包” 右边的 “确定” 按钮,在弹出的对话框中选择前面提到的 GoWebServer.tgz 文件。给这个发布包写好描述之后,点击确认,开始上传发布包:
发布成功后,回到 “发布管理” 界面,在该界面中,选择刚才发布的包,然后点击发布,一切正常情况下,即可发布成功。
服务验证
18869192.168.211.128
curl 192.168.211.128:18869
返回结果
{"msg":"Hello, Tars-Go!","unix":1546747070,"time":"2019-01-06 11:57:50","client":":-1"}
这就验证 OK 啦,同时也说明了 TARS 管理平台的配置值配置正确了。
总结
TARS可以在考虑到易用性和高性能的同时快速构建系统并自动生成代码,帮助开发人员和企业以微服务的方式快速构建自己稳定可靠的分布式应用,从而令开发人员只关注业务逻辑,提高运营效率。多语言、敏捷研发、高可用和高效运营的特性使 TARS 成为企业级产品。
TARS微服务助您数字化转型,欢迎访问:
TARS官网:https://TarsCloud.org
TARS源码:https://github.com/TarsCloud
获取《TARS官方培训电子书》:https://wj.qq.com/s2/6570357/3adb/
或扫码获取: