wenjianla / mprpc

高性能分布式网络通信框架(muduo,zookeeper,protobuf)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mprpc

Mprpc is developed based on Muduo high-performance network library and Protobuf.

1.build

Tested on: unbuntu 20.04 and unbuntu 22.04

Install required packages:

sudo apt-get install gcc g++ make cmake build-essential

1.1 build muduo

sudo apt-get install libboost-dev
sudo apt install zlib1g-dev
git clone https://github.com/chenshuo/muduo.git  # Alternatively, manually download and unzip the file
cd muduo
mkdir build
cd build
cmake .. && make
sudo make install

1.2 build protobuf

sudo apt install unzip
unzip protobuf-3.19.4.zip # directly manually downloaded version 3.19.4
cd protobuf-3.19.4
sudo apt-get install autoconf automake libtool curl make
./autogen.sh
./configure
make
sudo make install
sudo ldconfig  # Refresh Dynamic Library
protoc --version  # libprotoc 3.19.4
protoc test.proto --cpp_out=./     # Compile *.proto file command
g++ -o main main.cc test.pb.cc -lprotobuf   # 

1.3 build zookeeper

sudo apt-get install openjdk-11-jdk  # install jdk
tar -xzf zookeeper-3.4.10.tar.gz
cd zookeeper-3.4.10/src/c
sudo ./configure
sudo make  

Make compilation error in the native C API interface of Zookeeper during Linux installation see also https://blog.csdn.net/weixin_43604792/article/details/103879578

sudo vim Makefile # 将548行改为 AM_CFLAGS = -Wall 即可重新编译
sudo make
sudo make install
sudo ldconfig

Error while loading shared libraries: reasons and solutions for errors. The shared library has been installed, but when an external program calls the shared library, the program cannot find the shared library file according to the default path (/usr/lib, /lib). The shared libraries of the Ubuntu system are usually installed in the /usr/local/lib directory. If unsure, you can use the following to check if there are any library files you need.

ls /usr/local/lib

Then open /etc/ld.so.conf and add /usr/local/lib at the end of the file (or enter the following at the command line).

echo "/usr/local/lib" > sudo/etc/ld.so.conf

Save and exit, execute the ldconfig command.

ldconfig

1.4 buid mprpc

To prevent inconsistencies between the protobuf version of the compiled *. proto file and the compiled project, first, delete the result files generated by the previous historical compilation of *. proto files, and then recompile the *. proto files. Continue to compile the MPRPC project

cd mprpc/src
rm rpcheader.pb.h rpcheader.pb.cc
protoc rpcheader.proto --cpp_out=./
cd ../example/
rm friend.pb.* user.pb.*
protoc friend.proto --cpp_out=./
protoc user.proto --cpp_out=./
cd ..
sh autobuild.sh

2.run

2.1 run zookeeper

cd zookeeper-3.4.10/conf/
cp zoo_sample.cfg zoo.cfg

修改 zoo.cfg 配置文件

cd zookeeper-3.4.10/
mkdir data  # 用于存放zk中的数据
cd conf
sudo vim zoo.cfg

首先,zoo.cfg 中 dataDir=/tmp/zookeeper 表示 zk 中数据存放位置 默认存放在 /tmp/zookeeper 目录下,/tmp 是临时文件,当主机系统重启之后,/tmp 临时文件就删除了 启动 zk 后配置的数据也就不存在了,之前配置的数据丢失了,因此可以重新指定一下 zk 中数据存放位置 可以修改如下行配置 dataDir=/tmp/zookeeper => dataDir=/home/mprpc/zookeeper-3.4.10/data 修改之后保存退出

cd zookeeper-3.4.10/bin/
./zkServer.sh start          # must install openjdk 
ps -ef | grep zookeeper
sudo netstat -tanp

可以查看到如下信息

tcp6    0   0   :::2181   :::*    LISTEN  28549/java 

运行 zk 的客户端

cd zookeeper-3.4.10/bin/
./zkCli.sh

zk 客户端常用命令: ls get create set delete

列出根目录有哪些节点

ls /      # [zookeeper] 刚开始只有这一个节点 node

查询 zookeeper 节点上的详细信息

get /zookeeper

会显示如下内容

                     # 第1行为空表示节点存储的数据为空
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
ls /zookeeper   # 显示 [quota]
get /zookeeper/quota    # 访问 zk 的每个节点的路径都要从根路径写起
create /sl 20   # 创建节点
set /sl 30
delete /sl
quit

2.2 run mprpc

./bin/provider -i mprpc.conf

./bin/provider: error while loading shared libraries: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory

sudo ldconfig
./bin/consumer -i mprpc.conf

About

高性能分布式网络通信框架(muduo,zookeeper,protobuf)


Languages

Language:Makefile 83.1%Language:C++ 9.1%Language:CMake 4.1%Language:C 3.7%Language:TypeScript 0.1%Language:Shell 0.0%