Giter VIP home page Giter VIP logo

easyrpc's Introduction

A RPC framework written in Modern C++

easyrpc是一个纯c++14开发,header-only,使用方便的RPC库,灵感来自于topcpporg/rest_rpc

License

Getting started

首先下载easyrpc:

git clone https://github.com/chxuan/easyrpc.git

然后下载依赖的第三方库:

git submodule update --init --recursive

下载过后包含easyrpc头文件即可使用,在编译时需要指定序列化框架,添加ENABLE_BOOST_SERIALIZATION宏定义来启用boost.serialization序列化框架,添加ENABLE_MSGPACK宏定义来启用msgpack序列化框架,添加ENABLE_JSON宏定义来启用json序列化框架。

easyrpc流程图

此处输入图片的描述

Tutorial

  • Simple server

    #include <easyrpc/easyrpc.hpp>
    
    std::string echo(const std::string& str)
    {
        return str;
    }
    
    class utils
    {
    public:
        int add(int a, int b)
        {
            return a + b;
        }
    };
    
    int main()
    {
        easyrpc::server app;
        utils u;
        app.bind("say_hello", []
    	{ 
    		std::cout << "Hello" << std::endl;
    	});
    	app.bind("echo", &echo);
    	app.bind("add", &utils::add, &u);
    	
        app.listen(50051).timeout(3000).multithreaded(10).run();
        
        std::cin.get();
        return 0;
    }

    服务器调用bind函数绑定handler,支持成员函数、非成员函数以及lambda表达式的绑定,设置3000ms读socket超时(默认为永不超时),启用10个Worker线程处理业务(默认为单线程),内部IO线程使用an io_service-per-CPU(一个ioservice对应一个线程)模式,最大限度提升IO性能。

  • Simple client

    #include <easyrpc/easyrpc.hpp>
    
    EASYRPC_RPC_PROTOCOL_DEFINE(say_hello, void());
    EASYRPC_RPC_PROTOCOL_DEFINE(echo, std::string(const std::string&));
    EASYRPC_RPC_PROTOCOL_DEFINE(add, int(int, int));
    
    int main()
    {
        easyrpc::client app;
        app.connect("localhost:50051").timeout(5000).run();
    	app.call(say_hello);
    	auto str = app.call(echo, "Hello world");
    	auto ret = app.call(add, 1, 2);
    
        return 0;
    }

正如你所看到的,客户端像调用本地函数一样就能够完成与服务端的通信,一切都那么简洁方便,easyrpc目前只支持短连接调用,短连接的好处就是不用担心各个server的启动顺序、调用方便以及不用维护心跳,由于每次call都会去connect,所以没有长连接高效,后期可能会考虑增加长连接call。

  • User-define classes
    struct person_info_req
    {
        int card_id;
        std::string name;
    
    #ifdef ENABLE_BOOST_SERIALIZATION
        template<class Archive>
        void serialize(Archive& ar, const unsigned int)
        {
            ar & card_id;
            ar & name;
        }
    #endif
    
    #ifdef ENABLE_MSGPACK
        MSGPACK_DEFINE(card_id, name);
    #endif
    
    #ifdef ENABLE_JSON
        META(card_id, name);
    #endif
    };
    
    struct person_info_res 
    {
        int card_id;
        std::string name;
        int age;
        std::string national;
        
    #ifdef ENABLE_BOOST_SERIALIZATION
        template<class Archive>
        void serialize(Archive& ar, const unsigned int)
        {
            ar & card_id;
            ar & name;
            ar & age;
            ar & national;
        }
    #endif
        
    #ifdef ENABLE_MSGPACK
        MSGPACK_DEFINE(card_id, name, age, national);
    #endif
    
    #ifdef ENABLE_JSON
        META(card_id, name, age, national);
    #endif
    };

可以看到,boost序列化msgpackjson序列化不需要写IDL交互定义文件,也不会自动生成代码,比protobufthrift方便易用,更多User-define classes细节可以查看各自官网,若想扩展序列化方式,那怎么办呢?不用担心,easyrpc可以支持扩展序列化方式,这里以kapok作为示例。

  • Server

    #include <easyrpc/easyrpc.hpp>
    
    std::string call_person(const std::string& str)
    {
        std::cout << str << std::endl;
        DeSerializer dr;
        person_info_req req;
        dr.Parse(str);
        dr.Deserialize(req);
    
        person_info_res res;
        res.card_id = req.card_id;
        res.name = req.name;
        res.age = 20;
        res.national = "han";
        Serializer sr;
        sr.Serialize(res);
        return sr.GetString();
    }
    
    int main()
    {
        easyrpc::server app;
    	app.bind_raw("call_person", &call_person);
        app.listen(50051).timeout(3000).multithreaded(10).run();
        
        std::cin.get();
        return 0;
    }
  • Client

    #include <easyrpc/easyrpc.hpp>
    
    int main()
    {
        easyrpc::client app;
        app.connect("localhost:50051").timeout(5000).run();
        
        person_info_req req2 { 12345678, "Jack" };
        Serializer sr;
        sr.Serialize(req2);
        std::string str = app.call_raw<easyrpc::two_way>("call_person", sr.GetString());
        std::cout << str << std::endl;
    
        person_info_res res2;
        DeSerializer dr;
        dr.Parse(str);
        dr.Deserialize(res2);
    
        return 0;
    }

其中call_raw<easyrpc::two_way>表示一应一答,若无需应答则使用call_raw<one_way>call_raw的作用就是将序列化的工作交给了用户,起到了扩展序列化。

Warning

  • 以上samples为了简洁,所以没有捕获异常,用户在使用easyrpc时需要捕获异常。
  • 使用boost序列化库时,客户端和服务端序列化库的版本务必统一,要么使用32位的,要么使用64位,不然不能够通信,因为boost.serialization使用std::size_t来存储字节长度,std::size_t在32位下为unsigned int,在64位下为unsigned long。
  • 不支持指针,仅支持栈对象序列化。
  • 发送、接收buf大小为8MB(protocol + body),大于8MB程序将抛出异常,用户可更改发送、接收buf大小。
  • bind/call和bind_raw/call_raw需要保持一致性。

开发平台

  • Ubuntu16.04 LTS gcc5.3.1
  • MSVC2015

依赖性

DONE

  • 短连接调用。
  • 同步调用。
  • TCP协议。
  • worker线程池处理任务。
  • 日志记录。
  • 客户端、服务端超时处理。
  • 支持多种序列化框架(boost序列化、msgpack和json)。
  • 支持扩展序列化方式。

TODO

  • 增加扩展序列化方式
  • 增加长连接调用。
  • 增加发布/订阅模式。
  • 增加其他序列化框架和协议(json、msgpack等)。
  • 服务注册、发现。
  • 支持HTTP/HTTPS协议。
  • 异步调用。

License

This software is licensed under the MIT license. © 2016 chxuan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.