tars cpp项目开发流程

1.安装依赖环境

软件	                  软件要求
linux内核版本:	     2.6.18及以上版本
gcc版本:	             4.8 及以上版本、glibc-devel
bison工具版本:	     2.5及以上版本
flex工具版本:	     2.5及以上版本
cmake版本:    	     2.8.8及以上版本
mysql版本:	         5.6及以上版本

在Centos7下,执行:

yum install glibc-devel gcc gcc-c++ bison flex cmake

2. 安装TARS开发工具


git clone https://github.com/TarsCloud/TarsCpp.git --recursive
cd TarsCpp
mkdir build
cd build
cmake ..
make
make install

你如果要开启SSL, HTTP2等的支持, 你可以:

cmake .. -DTARS_SSL=ON -DTARS_HTTP2=ON
make
make install

关闭:

cmake .. -DTARS_SSL=OFF -DTARS_HTTP2=OFF
make
make install

注意默认情况下 examples的demo服务都没有编译, 如果希望编译这些demo服务, 请开启:

cmake .. -DONLY_LIB=OFF

3.创建项目

/usr/local/tars/cpp/script/cmake_tars_server.sh [App] [Server] [Servant]
本例中执行:/usr/local/tars/cpp/script/cmake_tars_server.sh TestApp HelloServer Hello
命令执行后,会在当前目录的 TestApp/HelloServer/src 目录下,生成下面文件:
HelloServer.h HelloServer.cpp Hello.tars HelloImp.h HelloImp.cpp CMakeLists.txt
编译服务

cd build;
cmake ..
make -j4

4.创建接口文件

Hello.tars:
	module TestApp
	{
	interface Hello
	{
	int test();
	};
	};

采用 tars2cpp 工具自动生成 c++文件:/usr/local/tars/cpp/tools/tars2cpp hello.tars 会生成 hello.h 文件,里面包含客户端和服务端的代码( 编译时会自动处理)。

5.接口类实现

HelloImp 是 Servant 的接口实现类
HelloImp.h
#ifndef _HelloImp_H_
#define _HelloImp_H_
#include "servant/Application.h"
#include "Hello.h"
/**
 1. HelloImp继承hello.h中定义的Hello对象
 2. */
class HelloImp : public TestApp::Hello
{
public:
/**
 3. */

virtual ~HelloImp() {}
/**
 4. 初始化,Hello的虚拟函数,HelloImp初始化时调用
*/
virtual void initialize();
/**
 5. 析构,Hello的虚拟函数,服务析构HelloImp退出时调用
*/
virtual void destroy();
/**
 6. 实现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:
//...
}

6.服务类实现

HelloServer 是服务的实现类

HelloServer.h:
#ifndef _HelloServer_H_
#define _HelloServer_H_
#include <iostream>
#include "servant/Application.h"
using namespace tars;
/**
 7. HelloServer继承框架的Application类
**/
class HelloServer : public Application
{
public:
/**
 8. **/
virtual ~HelloServer() {};
/**
 9. 服务的初始化接口
**/
virtual void initialize();
/**
 10. 服务退出时的清理接口
**/
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;
}
/

每个 Servant(Obj)对象对应一个业务处理线程, 因此如果是成 HelloImp 的成员变量, 并且只被当前的 HelloImp 对象处理, 是不需要加锁的

7 .服务编译

进入代码目录,首先做

cd build
cmake ..
make -j4
make HelloServer-tar
make HelloServer-upload

8.扩展功能

Tars 框架提供了接口定义语言的功能,可以在 tars 文件中,增加一下接口和方法,扩展服务的功能。
可以修改由 cmake_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 发布包。

9 客户端同步/异步调用服务

在开发环境上,创建/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;
}

异步方式
编写 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
#-----------------------------------------------------------------------

make 出目标文件,上传到能访问服务器的环境中进行运行测试即
也强烈建议你用 cmake 管理, 方式和服务端一样

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值