基于Tars高并发IM系统的设计与实现-实战篇3
快速开发入门
创建服务
/usr/local/tars/cpp/script/create_tars_server.sh [App] [Server] [Servant]
本例中执行生成一个认证服务:
/usr/local/tars/cpp/script/create_tars_server.sh otim AuthServer AuthServant
命令执行后,会在当前目录的 otim/AuthServer目录下,生成下面文件:
AuthServer.h
AuthServer.cpp
AuthServant.tars
AuthServantImp.h
AuthServantImp.cpp
makefile
编译服务
make
这些文件,已经包含了最基本的服务框架和默认测试接口实现。
服务实现
tars 接口文件
定义 tars 接口文件的语法和使用,参见 tars_tup
如下:
AuthServant.tars:
module otim
{
struct AuthParam
{
0 require string packId;
1 require LoginReq loginReq;
};
interface AuthServant
{
int auth(otim::ClientContext clientContext, AuthParam authParam, out string extraData);
};
};
采用 tars2cpp 工具自动生成 c++文件:/usr/local/tars/cpp/tools/tars2cpp AuthServant.tars 会生成 AuthServant.h 文件,里面包含客户端和服务端的代码( 编译时会自动处理)。
AuthServantImp 是 Servant 的接口实现类
实现服务定义的 tars 件中的接口,如下:
#ifndef _AuthServantImp_H_
#define _AuthServantImp_H_
#include "servant/Application.h"
#include "AuthServant.h"
/**
*
*
*/
class AuthServantImp : public otim::AuthServant
{
public:
/**
*
*/
virtual ~AuthServantImp() {}
/**
*
*/
virtual void initialize();
/**
*
*/
virtual void destroy();
/**
*
*/
virtual tars::Int32 auth(const otim::ClientContext & clientContext,const otim::AuthParam & authParam,std::string &extraData,tars::TarsCurrentPtr _current_);
};
/
#endif
#include "AuthServantImp.h"
#include "servant/Application.h"
#include "otim.h"
#include "log.h"
using namespace std;
//
void AuthServantImp::initialize()
{
//initialize servant here:
//...
}
//
void AuthServantImp::destroy()
{
//destroy servant here:
//...
}
tars::Int32 AuthServantImp::auth(const otim::ClientContext & clientContext,const otim::AuthParam & authParam,std::string &extraData,tars::TarsCurrentPtr _current_)
{
MLOG_DEBUG("context:"<<clientContext.writeToJsonString()<<" authParam:"<<authParam.writeToJsonString());
//实现认证代码添加到此处
return 0;
}
/
#ifndef _AuthServer_H_
#define _AuthServer_H_
#include <iostream>
#include "servant/Application.h"
using namespace tars;
/**
*
**/
class AuthServer : public Application
{
public:
/**
*
**/
virtual ~AuthServer() {};
/**
*
**/
virtual void initialize();
/**
*
**/
virtual void destroyApp();
};
extern AuthServer g_app;
#endif
/
#include "AuthServer.h"
#include "AuthServantImp.h"
#include "RedisPool.h"
#include "otim_const.h"
using namespace std;
AuthServer g_app;
/
void
AuthServer::initialize()
{
addServant<AuthServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".AuthServantObj");
}
/
void
AuthServer::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)对象对应一个业务处理线程, 因此如果是成 AuthServantImp 的成员变量, 并且只被当前的 AuthServantImp 对象处理, 是不需要加锁的
服务编译
进入代码目录,首先做
make
make tar
会生成 AuthServer.tgz 发布包。
- 客户端同步/异步调用服务
在开发环境上,创建/home/tarsproto/[APP]/[Server]目录。
例如:/home/tarsproto/otim/AuthServer 在刚才编写服务器的代码目录下,
执行 make release 这时会在/home/tarsproto/otim/AuthServer 目录下生成 h、tars 和 mk 文件。
这样在有某个服务需要访问 AuthServer 时,就直接引用 AuthServer 服务 make release 的内容,不需要把 AuthServer 的 tars 拷贝过来(即代码目录下不需要存放 AuthServer 的 tars 文件)。
要使用AuthServer的接口,有两种方式可以进行RPC。
- 同步方式:
otim::AuthParam authParam;
authParam.packId = packId;
authParam.loginReq = loginReq;
otim::AuthServantPrx authServantPrx Application::getCommunicator()->stringToProxy("otim.AuthServer.AuthServantObj", authServantPrx);
authServantPrx->auth(context, authParam);
- 异步方式
class UserAuthServantCallback : public otim::AuthServantPrxCallback
{
public:
otim::ClientContext _context;
otim::AuthParam _authParam;
tars::TarsCurrentPtr _current;
public:
UserAuthServantCallback(tars::TarsCurrentPtr current,const otim::ClientContext& clientContext, otim::AuthParam& param);
virtual void callback_auth(tars::Int32 ret, const std::string &resp);
virtual void callback_auth_exception(tars::Int32 ret);
static void sendLoginAckToClient(tars::TarsCurrentPtr current,const std::string &packId, int code, const otim::LoginReq & loginReq, const std::string& extraData);
};
otim::AuthParam authParam;
authParam.packId = packId;
authParam.loginReq = loginReq;
otim::AuthServantPrx authServantPrx ;
Application::getCommunicator()->stringToProxy("otim.AuthServer.AuthServantObj", authServantPrx);
tars::TC_AutoPtr<UserAuthServantCallback> authSrvCB = new UserAuthServantCallback(current, context, authParam);
authServantPrx->async_auth(authSrvCB, context, authParam);
make 出目标文件,上传到能访问服务器的环境中进行运行测试即
也强烈建议你用 cmake 管理, 方式和服务端一样
其他你可能需要知道的重点:
- examples 下有几个非常重要的调用例子:同步, 异步, 协程, 代理模式, push 模式, HTTP 服务支持等, 建议仔细读一读
- 代码中的 Communicator 是管理客户端资源的, 建议做成全局, 如果不是独立的 Client 客户端, 而是在服务框架中, 直接获取框架提供好的 Communicator, 参见 ProxyServer
- 上述 Client 例子中Application::getCommunicator()->stringToProxy(“otim.AuthServer.AuthServantObj”, authServantPrx), 框架会自动寻址后端的 AuthServer 服务, 并自动完成容灾切换
IM服务系统设计
一个完整高性能的IM服务设计在满足三大指标(高可用,高并发,低延时)、解决五大难题的同时,也要满足基本的业务功能。
要满足高可用,高并发,低延时三大指标,需要在架构设计上能满足要求,Tars高性能框架能够完全满足高可用、高并发两项指标;在使用Tars框架上,我们有多年使用经验。
低延时通过对内存,redis,mysql的合理分级使用可以到达目标。
下面分别给出IM服务体系的部署图,架构图,微服务交互等图。
IM服务分层部署图
IM微服务体系架构图
IM微服务模块架构图
微服务模块交互关系图:
- 接入服务对外提供长链接服务
- 开放平台接口服务针对针外部服务提供rest接口。
- 除了接入服务,开放平台接口服务之外,其他服务都是对内提供RPC服务
- 内部服务根据情况可以有两套RPC接口:通用RPC接口,专用RPC接口。
- 通用RPC接口主要用于接入服务的请求消息派发。