创建服务
/usr/local/tars/cpp/script/cmake_tars_server.sh MyApp HelloServer Hello
命令执行后,会在当前目录的TestApp/HelloServer/src 目录下,生成下面文件:
HelloServer.h HelloServer.cpp Hello.tars HelloImp.h HelloImp.cpp CMakeLists.txt
编译服务:
cd build;
cmake ..
make -j4
服务实现
Hello.tars:
module TestApp
{
interface Hello
{
int test();
int testHello(string sReq, out string sRsp);
};
};
采用tars2cpp工具自动生成c++文件:/usr/local/tars/cpp/tools/tars2cpp hello.tars会生成hello.h文件,里面包含客户端和服务端的代码( 编译时会自动处理)。
实现服务定义的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;};
virtual int testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current);
};
/////////////////////////////////////////////////////
#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:
//...
}
int HelloImp::testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current)
{
TLOGDEBUG("HelloImp::testHellosReq:"<<sReq<<endl);
sRsp = sReq;
return 0;
}
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;
}
/////////////////////////////////////////////////////////////////
服务编译:
cd build
cmake ..
make -j4
make HelloServer-tar
make HelloServer-upload
客户端同步/异步调用服务
同步方式:
#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,里面/home/tarsproto/APP/Serve, 如下
#-----------------------------------------------------------------------
APP :=TestApp
TARGET :=TestHelloClient
CONFIG :=
STRIP_FLAG := N
INCLUDE += -I/home/tarsproto/TestApp/HelloServer/
LIB +=
#-----------------------------------------------------------------------
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------