tar的c++版本入门教程
文章目录
1. 环境搭建
- C++环境搭建参考tars_install.md
2. 服务命名
-
APP:应用名,标识一组服务的一个集合
- 须唯一
- 如:TestApp
-
Server:
- 提供服务的进程名
- 命名为:XXServer,例如HelloServer;
-
Servant:
- 服务者,提供具体服务的接口或实例
- HelloImp
- 一个Server可含多个Servant
- 系统用App + Server + Servant来定义服务在系统中的路由名称,
- 称路由Obj
- TestApp.HelloServer.HelloObj
3. Tars管理系统
- 用户登录后,进入Tars系统

-
管理系统的菜单树下,有
-
业务管理:
- 已部署的服务,
- 服务管理
- 发布管理
- 服务配置
- 服务监控
- 特性监控
-
运维管理:
- 包括服务部署、扩容、模版管理
4. 服务部署
-
也可服务开发后,不过建议先做
-
如下:

-
“TestApp”。
-
“HelloServer”。
-
c++选“tars_cpp”
-
服务程序启动时,配置文件名称
- 默认”tars.default
-
“节点“ 指服务部署的机器IP
-
“Set分组“ 指设置服务的Set分组信息,
- 3部分:Set名、Set地区、Set组名。
-
“OBJ名称“ 指Servant名称。
-
“OBJ绑定IP“ 指服务绑定的机器IP,一般与节点一样。
-
“端口“ 指OBJ要绑定的端口。
-
“端口类型“ TCP还是UDP。
-
“协议“ 指应用层使用的通信协议,
- Tars框架默认使用tars协议。
-
“线程数“
- 业务处理线程的数目。
-
“最大连接数“ 指支持的最大连接数。
-
“队列最大长度“ 请求接收队列大小。
-
“队列超时时间“ 指请求接收队列的超时时间。
-
点击“提交“,成功后,
-
菜单数下的TestApp应用将出现HelloServer,
-
右侧看到你新增的服务程序信息,如下图:

- 到此只是使你的服务在管理系统上占了个位置,真实程序尚未发布。
5. 服务开发
5.1. 创建服务
5.1.1. 运行tars脚本
/usr/local/tars/cpp/script/create_tars_server.sh
[App] [Server] [Servant]
-
/usr/local/tars/cpp/script/create_tars_server.sh TestApp HelloServer Hello
-
执行后,当前目录的TestApp/HelloServer/,生成:
HelloServer.h
HelloServer.cpp
Hello.tars
HelloImp.h
HelloImp.cpp
makefile
- 这些文件包含最基本的服务框架和默认测试接口实现。
5.1.2. tars接口文件
- 定义tars接口文件的语法和使用,见tars_tup.md。
Hello.tars:
module TestApp
{
interface Hello
{
int test();
};
};
- /usr/local/tars/cpp/tools/tars2cpp hello.tars会生成hello.h,含客户端和服务端代码。
5.1.3. HelloImp是Servant的接口实现类
实现服务定义的tars件中的接口
HelloImp.h
#ifndef _HelloImp_H_
#define _HelloImp_H_
#include "servant/Application.h"
#include "Hello.h"
/**
* HelloImp继承hello.h中定义的Hello对象
*
*/
class HelloImp : public TestApp::Hello
{
public:
/**
*
*/
virtual ~HelloImp() {}
/**
* 初始化,Hello的虚拟函数,HelloImp初始化时调用
*/
virtual void initialize();
/**
* 析构,Hello的虚拟函数,服务析构HelloImp退出时调用
*/
virtual void destroy();
/**
* 实现tars文件中定义的test接口
*/
virtual int test(tars::TarsCurrentPtr current) { return 0;};
};
/////////////////////////////////////////////////////
#endif
HelloImp.cpp:
#include "HelloImp.h"
#include "servant/Application.h"
using namespace std;
//////////////////////////////////////////////////////
void HelloImp::initialize()
{
//initialize servant here:
//...
}
//////////////////////////////////////////////////////
void HelloImp::destroy()
{
//destroy servant here:
//...
}
5.1.4. HelloServer是服务的实现类
HelloServer.h:
#ifndef _HelloServer_H_
#define _HelloServer_H_
#include <iostream>
#include "servant/Application.h"
using namespace tars;
/**
* HelloServer继承框架的Application类
**/
class HelloServer : public Application
{
public:
/**
*
**/
virtual ~HelloServer() {};
/**
* 服务的初始化接口
**/
virtual void initialize();
/**
* 服务退出时的清理接口
**/
virtual void destroyApp();
};
extern HelloServer g_app;
////////////////////////////////////////////
#endif
HelloServer.cpp
#include "HelloServer.h"
#include "HelloImp.h"
using namespace std;
HelloServer g_app;
/////////////////////////////////////////////////////////////////
void
HelloServer::initialize()
{
//initialize application here:
//添加Servant接口实现类HelloImp与路由Obj绑定关系
addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj");
}
/////////////////////////////////////////////////////////////////
void
HelloServer::destroyApp()
{
//destroy application here:
//...
}
/////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception& e)
{
cerr << "std::exception:" << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////
5.2. 服务编译
进入代码目录
make cleanall
make
make tar
5.3. 扩展功能
- 修改由create_tars_server.sh生成的tars文件,
- 以下3个接口方法中,
- test是默认生成的,
- testHello是新增加的
module TestApp
{
interface Hello
{
int test();
int testHello(string sReq, out string sRsp);
};
};
用/usr/local/tars/cpp/tools/tars2cpp hello.tars,重新生成hello.h。
修改HelloImp.h/HelloImp.cpp,实现新的接口代码。
其中HelloImp.h中继承Hello类的testHello方法:
virtual int testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current);
HelloImp.cpp实现testHello方法:
int HelloImp::testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current)
{
TLOGDEBUG("HelloImp::testHellosReq:"<<sReq<<endl);
sRsp = sReq;
return 0;
}
重新make cleanall;make;make tar,会重新生成HelloServer.tgz发布包。
5.4. 客户端同步/异步调用服务
-
开发环境上,创建/home/tarsproto/[APP]/[Server]。
-
如:/home/tarsproto/TestApp/HelloServer
-
在刚才编写服务器的代码目录下,执行 make release
- 这时在/home/tarsproto/TestApp/HelloServer下生成
- h、tars和mk文件
- 这样在有某个服务要访问HelloServer时,
- 就直接用HelloServer服务make release的内容,
- 不需把HelloServer的tars拷贝过来
- (即代码目录下不需存放HelloServer的tars文件)。
- 建立客户端代码目录,TestHelloClient/
- 编写main.cpp,创建实例并调用刚编写的接口函数测试。
同步方式:
#include <iostream>
#include "servant/Communicator.h"
#include "Hello.h"
using namespace std;
using namespace TestApp;
using namespace tars;
int main(int argc,char ** argv)
{
Communicator comm;
try
{
HelloPrx prx;
comm.stringToProxy("TestApp.HelloServer.HelloObj@tcp -h 10.120.129.226 -p 20001" , prx);
try
{
string sReq("hello world");
string sRsp("");
int iRet = prx->testHello(sReq, sRsp);
cout<<"iRet:"<<iRet<<" sReq:"<<sReq<<" sRsp:"<<sRsp<<endl;
}
catch(exception &ex)
{
cerr << "ex:" << ex.what() << endl;
}
catch(...)
{
cerr << "unknown exception." << endl;
}
}
catch(exception& e)
{
cerr << "exception:" << e.what() << endl;
}
catch (...)
{
cerr << "unknown exception." << endl;
}
return 0;
}
异步方式
#include <iostream>
#include "servant/Communicator.h"
#include "Hello.h"
using namespace std;
using namespace TestApp;
using namespace tars;
class HelloCallBack : public HelloPrxCallback
{
public:
HelloCallBack(){}
virtual ~HelloCallBack(){}
virtual void callback_testHello(tars::Int32 ret, const std::string& sRsp)
{
cout<<"callback_testHello ret:"<< ret << "|sRsp:" << sRsp <<endl;
}
virtual void callback_testHello_exception(tars::Int32 ret)
{
cout<<"callback_testHello_exception ret:"<< ret <<endl;
}
};
int main(int argc,char ** argv)
{
Communicator comm;
try
{
HelloPrx prx;
comm.stringToProxy("TestApp.HelloServer.HelloObj@tcp -h 10.120.129.226 -p 20001" , prx);
try
{
string sReq("hello world");
HelloPrxCallbackPtr cb = new HelloCallBack();
prx->async_testHello(cb, sReq);
cout<<" sReq:"<<sReq<<endl;
}
catch(exception &ex)
{
cerr<<"ex:"<<ex.what() <<endl;
}
catch(...)
{
cerr<<"unknown exception."<<endl;
}
}
catch(exception& e)
{
cerr<<"exception:"<<e.what() <<endl;
}
catch (...)
{
cerr<<"unknown exception."<<endl;
}
getchar();
return 0;
}
- 编写makefile,
- 里面包含刚才通过make release生成的/home/tarsproto/APP/Server目录下的mk文件
- 如下:
#-----------------------------------------------------------------------
APP :=TestApp
TARGET :=TestHelloClient
CONFIG :=
STRIP_FLAG := N
INCLUDE +=
LIB +=
#-----------------------------------------------------------------------
include /home/tarsproto/TestApp/HelloServer/HelloServer.mk
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------
- make出目标文件,上传到能访问服务器的环境中运行测试即可
6. 服务发布
-
在管理系统的菜单树下,找到你部署的服务,点击进入服务页面。
-
选择“发布管理”,选中要发布的节点,点击“发布选中节点”,点击“上传发布包”,选择已经编译好的发布包,如下

- 上传好发布包后,“选择发布版本”下拉框就会出现你上传的服务程序,选择最上面的一个(最新上传的)。如下:

- 点击“发布”,服务开始发布,发布成功后,出现下面的界面,如下图:

若失败的话,可能是命名问题,上传问题,以及其他环境问题。
参考文件
- https://github.com/gamegrd/taf
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)