【TARS】用TarsCpp-创建第一个服务

目录

0.参考链接

1.执行cmake_tars_server.sh脚本

2.进入build目录,执行cmake ..;make -j4;make HelloServer-tar;make HelloServer-upload

3.添加测试接口testHello

4.开发客户端

5.服务部署并进行相关配置

6.客户端测试

7.服务端端口查看


0.参考链接

 官方参考链接

官方文档-总括

1.执行cmake_tars_server.sh脚本

[root@localhost tars]# /usr/local/tars/cpp/script/cmake_tars_server.sh TestApp HelloServer Hello
APP:TestApp, SERVER:HelloServer, SERVANT:Hello
[mkdir: HelloServer]
[create server: TestApp.HelloServer ...]
-- The C compiler identification is GNU 9.3.1
-- The CXX compiler identification is GNU 9.3.1
-- Check for working C compiler: /opt/rh/devtoolset-9/root/usr/bin/cc
-- Check for working C compiler: /opt/rh/devtoolset-9/root/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-9/root/usr/bin/c++
-- Check for working CXX compiler: /opt/rh/devtoolset-9/root/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-------------------------------------------------------------------------------------
CMAKE_SOURCE_DIR:          /usr/local/app/tars/HelloServer
CMAKE_BINARY_DIR:          /usr/local/app/tars/HelloServer/build
PROJECT_SOURCE_DIR:        /usr/local/app/tars/HelloServer
CMAKE_BUILD_TYPE:          Release
PLATFORM:                  linux
TARS2CPP:                  /usr/local/tars/cpp/tools/tars2cpp
TARSMERGE:                 /usr/local/tars/cpp/tools/tarsmerge
TARS_MYSQL:                ON
TARS_HTTP2:                OFF
TARS_SSL:                  OFF
TARS_WEB_HOST:             http://web.tars.com
TARS_TOKEN:                
-------------------------------------------------------------------------------------
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/app/tars/HelloServer/build
Scanning dependencies of target tars-HelloServer
[ 25%] /usr/local/tars/cpp/tools/tars2cpp  /usr/local/app/tars/HelloServer/src/Hello.tars
[ 25%] Built target tars-HelloServer
Scanning dependencies of target HelloServer
[ 50%] Building CXX object src/CMakeFiles/HelloServer.dir/HelloImp.cpp.o
[ 75%] Building CXX object src/CMakeFiles/HelloServer.dir/HelloServer.cpp.o
[100%] Linking CXX executable ../bin/HelloServer
[100%] Built target HelloServer
[done.]

These files already contain the basic service framework and the default test interface 
implementation.We just need to fill in the code that reflects the logic of the program.
生成的这些文件已经包含了基础的服务框架和默认的测试接口实现,我们需要做的仅仅就是填充反应
程序逻辑的代码.

2.进入build目录,执行cmake ..;make -j4;make HelloServer-tar;make HelloServer-upload

 

3.添加测试接口testHello

You can modify Hello.tars and add function with the below commands:

module TestApp
{
    interface Hello
   {
     int test();
     int testHello(string sReq, out string sRsp);
   };
};
Use /usr/local/tars/cpp/tools/tars2cpp hello.tars to recreate hello.h. 
Next,modify HelloImp.h/HelloImp.cpp to implement the new interface code.


In HelloImp.h, the testHello method of inheriting from the Hello class is:

virtual int testHello(const std::string &sReq, std::string &sRsp,
tars::TarsCurrentPtr current);


For HelloImp.cpp to implement testHello function:
int HelloImp::testHello(const std::string &sReq, std::string &sRsp,
tars::TarsCurrentPtr current)
{
     TLOGDEBUG("HelloImp::testHellosReq:"<<sReq<<endl);
     sRsp = sReq;
     return 0;
}
The make clean all;make;make tar command can recreate the HelloServer.tgz package.

至此,服务器端的开发就完成了.

4.开发客户端

mkdir -p /home/tarsproto/TestApp/HelloServer
mkdir -p /home/tarsproto/TestApp/HelloClient
将HelloServer下的Hello.h和Hello.cpp文件放到/home/tarsproto/TestApp/HelloClient下.

Makefile
----------------------------------------------------------------------
APP :=TestApp
TARGET :=TestHelloClient
CONFIG :=
STRIP_FLAG := N
INCLUDE += -I/home/tarsproto/TestApp/HelloServer/
LIB +=
#-----------------------------------------------------------------------
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------
#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 192.168.118.138 -p 15319" , prx);
                // 注意 "应用+服务名+对象" 要对应到注册的那个服务上
                comm.stringToProxy("tars.HelloServer.HelloObj@tcp -h 192.168.118.138 -p 15319" , 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 << "line 23 , ex:" << ex.what() << endl;
                }
                catch(...)
                {
                        cerr << "unknown exception." << endl;
                }
        }
        catch(exception& e)
        {
                cerr << "exception:" << e.what() << endl;
        }
        catch (...)
        {
                cerr << "unknown exception." << endl;
        }
        return 0;
} 

5.服务部署并进行相关配置

 

"应用"指你的服务程序归在哪一个应用下,例如:"TestApp"或者图中的"tars";
"服务名称"指你的服务程序的标识名字,例如:"HelloServer";
"服务类型"指你的服务程序用什么语言写的,例如:c++的选择“tars_cpp”。
"模版"指你的服务程序在启动时,设置的配置文件的名称,默认用"tars.default"即可,也可以用对应想要的;
"节点"指服务部署的机器IP;
"Set分组"指设置服务的Set分组信息,Set信息包括3部分:Set名,Set地区,Set组名;
"OBJ名称"指Servant的名称;
"OBJ绑定IP"指服务绑定的机器IP,一般与节点一样;
"端口"指OBJ要绑定的端口;
"端口类型"指使用TCP还是UDP;
"协议"指应用层使用的通信协议,Tars框架默认使用tars协议;
"线程数"指业务处理线程的数目;
"最大连接数"指支持的最大连接数;
"队列最大长度"指请求接收队列的大小;
"队列超时时间"指请求接收队列的超时时间.

 

然后在服务列表中点击启动即可,如果一切正常,就可以正常启动.

6.客户端测试

至此,一个简单的hello world完成了.

7.服务端端口查看

netstat 命令当中的内部地址和外部地址

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值