一、环境准备:
CentOS7,CMake编译器
Mysql使用官方c语言版本客户端,不使用c++库,因为c++库依赖boost库
Linux安装mysql时,有这个包,mysql-community-devel-5.7.18-1.el7.x86_64.rpm -- 里面内容是c语言版本的客户端库函数和头文件
https://downloads.mysql.com/archives/community/
Redis使用自带的hiredis库,也是c语言实现
https://github.com/antirez/redis/tree/unstable/deps/hiredis
https://github.com/redis/hiredis
二、完整的工程源码:https://download.csdn.net/download/libaineu2004/10300017
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
#工程主要参考TeamTalk
#https://github.com/meili/TeamTalk/tree/master/server/src/db_proxy_server
PROJECT(mysqlredis_pool)
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#mysql客户端,我们不使用c++版本,因为它依赖boost库,太啰嗦
#mysql-connector-c++-1.1.8-linux-el7-x86-64bit.tar.gz -- https://downloads.mysql.com/archives/c-cpp/
#推荐使用c版本 -- https://downloads.mysql.com/archives/c-c/
#mysql-community-devel-5.7.18-1.el7.x86_64.rpm 自带了c语言客户端头文件和库
#安装说明 -- http://blog.csdn.net/libaineu2004/article/details/76212125
SET(MYSQL_INCLUDE_DIR /usr/include/mysql)
SET(MYSQL_LIB /usr/lib64/mysql)
#redis客户端,使用c版本 -- https://github.com/redis/hiredis
#安装说明 -- http://blog.csdn.net/libaineu2004/article/details/76209267
SET(REDIS_INCLUDE_DIR /usr/local/include/hiredis/)
SET(REDIS_LIB /usr/local/lib/)
INCLUDE_DIRECTORIES(${MYSQL_INCLUDE_DIR} ${REDIS_INCLUDE_DIR})
LINK_DIRECTORIES(${MYSQL_LIB} ${REDIS_LIB})
#ADD_EXECUTABLE必须在TARGET_LINK_LIBRARIES前面,否则会报错
ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} mysqlclient hiredis pthread)
main.cpp
#include "DBPool.h" #include "CachePool.h" #include "ThreadPool.h" #include "ProxyTask.h" #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> //daemonize #include <string> #include <string.h> void handler(uint32_t conn_uuid) { printf("hello,%d\n", conn_uuid); CDBManager* pDBManger = CDBManager::getInstance(); if (!pDBManger) { printf("DBManager init failed"); return; } CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_master");//get a db conn if (pDBConn) { string strSql = "select * from IMUser"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { delete pResultSet; } pDBManger->RelDBConn(pDBConn);//注意,con用完之后一定要记得归还,ReleaseConnection } } int main(int argc, char* argv[]) { signal(SIGPIPE, SIG_IGN); //案例1,仅仅使用mysql连接池,不使用线程池 CDBManager* pDBManger = CDBManager::getInstance(); if (!pDBManger) { printf("DBManager init failed"); return -1; } CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_master");//get a db conn if (pDBConn) { string strSql = "select * from IMUser"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { string strResult, strSalt; uint32_t nId, nGender, nDeptId, nStatus; string strNick, strAvatar, strEmail, strRealName, strTel, strDomain,strSignInfo; while (pResultSet->Next()) { nId = pResultSet->GetInt("id"); strResult = pResultSet->GetString("password"); strSalt = pResultSet->GetString("salt"); strNick = pResultSet->GetString("nick"); nGender = pResultSet->GetInt("sex"); strRealName = pResultSet->GetString("name"); strDomain = pResultSet->GetString("domain"); strTel = pResultSet->GetString("phone"); strEmail = pResultSet->GetString("email"); strAvatar = pResultSet->GetString("avatar"); nDeptId = pResultSet->GetInt("departId"); nStatus = pResultSet->GetInt("status"); strSignInfo = pResultSet->GetString("sign_info"); } delete pResultSet; } pDBManger->RelDBConn(pDBConn);//注意,con用完之后一定要记得归还,ReleaseConnection } //案例2,使用mysql连接池,也使用线程池 //top -H -p <pid> 终端查看某个特定进程<pid>并检查该进程内运行的线程状况 CThreadPool thread_pool; thread_pool.Init(10); CTask* pTask1 = new CProxyTask(1, handler);//void CWorkerThread::Execute()执行完任务会删除指针CTask* thread_pool.AddTask(pTask1); CTask* pTask2 = new CProxyTask(2, handler); thread_pool.AddTask(pTask2); CTask* pTask3 = new CProxyTask(3, handler); thread_pool.AddTask(pTask3); //案例3,仅仅使用redis连接池,不使用线程池 CacheManager* pCacheManager = CacheManager::getInstance(); if (!pCacheManager) { printf("CacheManager init failed"); return -1; } CacheConn* pCacheConn = pCacheManager->GetCacheConn("redis"); if (pCacheConn) { string strVal = "sodesiname"; pCacheConn->set("total_user_update", strVal); string str = pCacheConn->get("total_user_update"); printf("%s\n", str.c_str()); pCacheManager->RelCacheConn(pCacheConn); } //进程挂起,等待任务执行完成 printf("\ninput:\n"); char s[20]; gets(s); //等待输入字符串直到回车结束 return 0; }