【从零实现Json-Rpc框架】- 入门准备篇

embedded/2025/3/31 11:22:23/

📢博客主页:https://blog.csdn.net/2301_779549673
📢博客仓库:https://gitee.com/JohnKingW/linux_test/tree/master/lesson
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📢本文由 JohnKi 原创,首发于 CSDN🙉
📢未来很长,值得我们全力奔赴更美好的生活✨

在这里插入图片描述

在这里插入图片描述

文章目录

  • 📢前言
  • 🏳️‍🌈一、项目介绍
  • 🏳️‍🌈二、技术选型
    • 2.1 目前RPC的实现方案有两种:
    • 2.2 网络传输的参数和返回值怎么映射到对应的RPC接口上?
    • 2.3 网络传输怎么做?
    • 2.4 序列化和反序列化?
  • 🏳️‍🌈三、开发环境及搭建
    • 3.1 安装wget(⼀般情况下默认会⾃带)
    • 3.2 更换国内软件源
    • 3.3 新增完毕后,更新源
    • 3.4 安装lrzsz传输工具
    • 3.5 安装编译器gcc/g++
    • 3.6 安装项目构建工具 make
    • 3.7 安装调试器 gdb
    • 3.8 安装Git
    • 3.9 安装cmake
    • 3.10 安装jsoncpp
    • 3.11 安装Muduo
  • 👥总结


📢前言

RPC(Remote Procedure Call)远程过程调用,是一种通过网络从远程计算机上请求服务,而不需要了解底层网络通信细节。RPC可以使用多种网络协议进行通信,如HTTP、TCP、UDP等,并且在TCP/IP网络四层模型中跨越了传输层和应用层。

简言之RPC就是像调用本地方法一样调用远程方法过程可以理解为业务处理、计算任务,更直白的说,就是程序/方法/函数等,就是像调用本地方法一样调用远程方法。


🏳️‍🌈一、项目介绍

在这里插入图片描述

举个形象的例子:谈恋爱例子
本地过程调用:恋爱对象在你的身边,可以随时约对象吃饭、看电影、约会等等
远端过程调用:好像异地恋一样,隔着千山万水,如果想约会,需要先和对象进行约定,在坐火车/飞机赶到约定的地点

在这里插入图片描述

这个项目是基于C++、JsonCpp、muduo网络库实现一个简单、易用的RPC通信框架,即使是不懂网络的开发者也可以很快速的上手,它实现了同步调用、异步callback调用、异步futrue调用、服务注册/发现,服务上线/下线以及发布订阅等功能设计。

🏳️‍🌈二、技术选型

2.1 目前RPC的实现方案有两种:

方案一:client和server继承公共接口

  • 根据IDL(接口描述语言)定义公共接口。
  • 编写代码生成器根据IDL语言生成相关的C++、Java代码
  • 然后我们的客户端和服务器程序共同向上继承公共接口即可
  • 比如我们常用的Protobuf、json可以定义IDL接口,并生成RPC相关的代码
  • 缺点: 使用pb因为生成一部分代码,所以对理解不够友好;如果是json定义IDL语言 需要自己编写代码生成器难度较大一点,暂不考虑这种方案

方案二:实现一个远程调用接口call,然后通过传入函数名参数来调用RPC接口,我们采用这种实现方案

2.2 网络传输的参数和返回值怎么映射到对应的RPC接口上?

方案一:使用protobuf的反射机制
方案二:使用C++模板、类型萃取、函数萃取等机制
方案三:使用更通用的类型,比如JSON类型,设计好参数和返回值协议即可前两种技术难度和学习成本较高,我们使用第三种方式

2.3 网络传输怎么做?

方案一:原生socket-实现难度较大,暂不考虑
方案二:Boostasio库的异步通信-需要扩展boost库
方案三:muduo库,学习开发成本较低

2.4 序列化和反序列化?

方案一:Protobuf: 可选
方案二:JSON,因为项目需要使用JSON来定义函数参数和返回值,所以砸门项目中直接采用JSON进行序列化和反序列化

🏳️‍🌈三、开发环境及搭建

需要centos的私信我

  • Linux(Ubuntu-22.04)
  • VSCode/Vim
  • g++/gdb
  • Makefile

3.1 安装wget(⼀般情况下默认会⾃带)

sudo apt-get install wget

3.2 更换国内软件源

先备份原来的/etc/apt/source.list⽂件

sudo cp /etc/apt/sources.list /etc/apt/sources.list.

添加软件源⽂件内容,新增以下内容

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

注意一下格式

3.3 新增完毕后,更新源

sudo apt-get update

大约长这样
在这里插入图片描述

3.4 安装lrzsz传输工具

root@VM-20-5-ubuntu:~# sudo apt-get install lrzsz
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
lrzsz is already the newest version (0.12.21-10).
0 upgraded, 0 newly installed, 0 to remove and 210 not upgraded.
root@VM-20-5-ubuntu:~# rz --version
rz (GNU lrzsz) 0.12.21rc

3.5 安装编译器gcc/g++

root@VM-20-5-ubuntu:~# sudo apt-get install gcc g++
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
g++ is already the newest version (4:11.2.0-1ubuntu1).
g++ set to manually installed.
gcc is already the newest version (4:11.2.0-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 210 not upgraded.root@VM-20-5-ubuntu:~# gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.root@VM-20-5-ubuntu:~# g++ --version
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

3.6 安装项目构建工具 make

root@VM-20-5-ubuntu:~# sudo apt-get install make
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
make is already the newest version (4.3-4.1build1).
0 upgraded, 0 newly installed, 0 to remove and 210 not upgraded.
root@VM-20-5-ubuntu:~# make --version
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

3.7 安装调试器 gdb

root@VM-20-5-ubuntu:~# sudo apt-get install gdb
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
gdb is already the newest version (12.1-0ubuntu1~22.04.2).
0 upgraded, 0 newly installed, 0 to remove and 207 not upgraded.
root@VM-20-5-ubuntu:~# gdb --version
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

如果出现这样的界面的话,可以使用 Tab 跳转到 Cancel
在这里插入图片描述

3.8 安装Git

root@VM-20-5-ubuntu:~# sudo apt-get install git
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
git is already the newest version (1:2.34.1-1ubuntu1.12).
0 upgraded, 0 newly installed, 0 to remove and 206 not upgraded.
root@VM-20-5-ubuntu:~# git --version
git version 2.34.1

3.9 安装cmake

root@VM-20-5-ubuntu:~# sudo apt-get install cmake
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
cmake is already the newest version (3.22.1-1ubuntu1.22.04.2).
0 upgraded, 0 newly installed, 0 to remove and 206 not upgraded.
root@VM-20-5-ubuntu:~# cmake --version
cmake version 3.22.1CMake suite maintained and supported by Kitware (kitware.com/cmake).

jsoncpp_214">3.10 安装jsoncpp

root@VM-20-5-ubuntu:~# sudo apt-get install libjsoncpp-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
libjsoncpp-dev is already the newest version (1.9.5-3).
0 upgraded, 0 newly installed, 0 to remove and 206 not upgraded.

3.11 安装Muduo

  1. 下载源码
root@VM-20-5-ubuntu:~# git clone https://github.com/chenshuo/muduo.git
Cloning into 'muduo'...
remote: Enumerating objects: 8442, done.
remote: Counting objects: 100% (263/263), done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 8442 (delta 217), reused 206 (delta 204), pack-reused 8179 (from 2)
Receiving objects: 100% (8442/8442), 1.77 MiB | 2.90 MiB/s, done.
Resolving deltas: 100% (5248/5248), done.
  1. 安装依赖环境
root@VM-20-5-ubuntu:~# sudo apt-get install libz-dev libboost-all-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'zlib1g-dev' instead of 'libz-dev'
libboost-all-dev is already the newest version (1.74.0.3ubuntu7).
zlib1g-dev is already the newest version (1:1.2.11.dfsg-2ubuntu9.2).
0 upgraded, 0 newly installed, 0 to remove and 188 not upgraded.
  1. 运⾏脚本编译安装

这部分就是在解压,太长了不放出来了,预计3分钟左右

./build.sh
./build.sh install

👥总结

本篇博文对 【从零实现Json-Rpc框架】- 入门准备篇 做了一个较为详细的介绍,不知道对你有没有帮助呢

觉得博主写得还不错的三连支持下吧!会继续努力的~

请添加图片描述


http://www.ppmy.cn/embedded/176581.html

相关文章

Netty源码—3.Reactor线程模型三

大纲 5.NioEventLoop的执行总体框架 6.Reactor线程执行一次事件轮询 7.Reactor线程处理产生IO事件的Channel 8.Reactor线程处理任务队列之添加任务 9.Reactor线程处理任务队列之执行任务 10.NioEventLoop总结 5.NioEventLoop的执行总体框架 (1)Reactor线程所做的三件事情…

【UEC++】AI行为树及其相关蓝图部分在C++中的实现

前言 在UE开发中&#xff0c;AI逻辑的书写是一个极其重要的部分。在本人于UE的学习过程中&#xff0c;认为AI的逻辑主要由以下几个基本的部分构成&#xff1a;AIController、作为AI的Character、BehaviorTree、Blackboard、Tasks、Service以及装饰器。本文也将主要就这几个部分…

多条件排序(C# and Lua)

C# 升序排序 OrderBy 按升序对序列的元素进行排序 ThenBy 按升序对序列中的元素执行后续排序 降序排序 OrderByDescending 按降序对序列的元素排序 ThenByDescending 按降序对序列中的元素执行后续排序 public class Fruit {public int id;public string name;publi…

区块链驱动金融第十章——走进另类币与加密货币生态系统:比特币之外的广阔天地

在加密货币的领域中,比特币虽然占据着重要地位,但它并非唯一的主角。随着区块链技术的不断发展,各种各样的另类币如雨后春笋般涌现,共同构建了一个丰富多彩且充满活力的加密货币生态系统。让我们深入第十章的内容,去探寻这一生态系统的奥秘。 另类币:比特币的 “挑战者”…

XSS复现漏洞简单前八关靶场

靶场不需要安装任意环境 链接如下&#xff1a;XSS Game - Learning XSS Made Simple! | Created by PwnFunction 目录 XSS Game 第一关&#xff1a;Ma Spaghet! 第二关&#xff1a;Jefff 第三关&#xff1a;Ugandan Knuckles 第四关&#xff1a;Ricardo Milos 第五关&am…

OpenGL ES ->乒乓缓冲,计算只用两个帧缓冲对象(Frame Buffer Object)+叠加多个滤镜作用后的Bitmap

乒乓缓冲核心思想 不使用乒乓缓冲&#xff0c;如果要每个滤镜作用下的绘制内容&#xff0c;也就是这个滤镜作用下的帧缓冲&#xff0c;需要创建一个Frame Buffer Object加上对应的Frame Buffer Object Texture使用乒乓缓冲&#xff0c;只用两个Frame Buffer Object加上对应的F…

Java医疗知识图谱知识库构建(源码)

基于Java、Neo4j和ElasticSearch构建的医疗知识图谱知识库&#xff0c;是一个融合图数据库技术与搜索引擎的智能化医疗知识管理系统。该系统以Neo4j图数据库为核心&#xff0c;利用其高效的图结构存储能力&#xff0c;将疾病、症状、药品、检查项目、科室等医疗实体抽象为节点&…

Leetcode 刷题笔记 图论part05

卡码网 107 寻找存在的路径 初识并查集 并查集功能&#xff1a; 寻找根节点&#xff0c;函数: find(int u)&#xff0c;也就是判断这个节点的祖先节点是哪个将两个节点接入到同一个集合&#xff0c;函数: join(int u, int v)&#xff0c;将两个节点连在同一个根节点上判断两…