轻量级的并发PHP RPC框架

网友投稿 1105 2022-10-22

轻量级的并发PHP RPC框架

轻量级的并发PHP RPC框架

Yar - Yet Another RPC framework for PHP

Light, concurrent RPC framework for PHP(see also: Yar C framework, Yar Java framework)

Requirement

PHP 7.0+ (master branch))PHP 5.2+ (php5 branch)CurljsonMsgpack (Optional)

Introduction

Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications

It has the ability to concurrently call multiple remote services.

Features

Fast, Easy, SimpleConcurrent RPC callsMultiple data packager supported (php, json, msgpack built-in)Multiple transfer protocols supported (http, https, TCP)Detailed debug informations

Install

Install Yar

Yar is an PECL extension, thus you can simply install it by:

pecl install yar

Compile Yar in Linux

$/path/to/phpize$./configure --with-php-config=/path/to/php-config/$make && make install

Available instructions to configure are

--with-curl=DIR--enable(disable)-msgpack--enable(disable)-epoll (require Yar 2.1.2)

Install Yar with msgpack

first you should install msgpack-ext

pecl install msgpack

or , you can get the github source here: https://github.com/msgpack/msgpack-php

then:

$phpize$configure --with-php-config=/path/to/php-config/ --enable-msgpack$make && make install

Runtime Configure

yar.timeout //default 5000 (ms)yar.connect_timeout //default 1000 (ms)yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack"yar.debug //default Offyar.expose_info // default On, whether output the API info for GET requestsyar.content_type // default "application/octet-stream"yar.allow_persistent // default Off

NOTE yar.connect_time is a value in milliseconds, and was measured in seconds in 1.2.1 and before.

Constants

YAR_VERSIONYAR_OPT_PACKAGERYAR_OPT_PERSISTENTYAR_OPT_TIMEOUTYAR_OPT_CONNECT_TIMEOUTYAR_OPT_HEADER // Since 2.0.4

Server

It's very easy to setup a Yar HTTP RPC Server

handle();?>

Usual RPC calls will be issued as HTTP POST requests. If a HTTP GET request is issued to the uri, the service information (commented section above) will be printed on the page:

Client

It's very simple for a PHP client to call remote RPC:

Synchronous call

SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);$client->SetOpt(YAR_OPT_HEADER, array("hd1: val", "hd2: val")); //Custom headers, Since 2.0.4/* call remote service */$result = $client->some_method("parameter");?>

Concurrent call

"json")); //this server accept json packagerYar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_TIMEOUT=>1)); //custom timeout Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests, //the error_callback is optional?>

Persistent call

After Yar 2.1.0, if YAR_OPT_PERSISTENT is set to true, then Yar is able to use HTTP keep-alive to speedup repeated calls to a same address, the link will be released at the end of the PHP request lifecycle.

SetOpt(YAR_OPT_PERSISTENT, 1);$result = $client->some_method("parameter");/* The following calls will speed up due to keep-alive */$result = $client->some_other_method1("parameter");$result = $client->some_other_method2("parameter");$result = $client->some_other_method3("parameter");?>

Custom hostname resolving

After Yar 2.1.0, if Yar runs on HTTP protocl, YAR_OPT_RESOLVE could be used to define custom hostname resolving.

SetOpt(YAR_OPT_RESOLVE, "host:80:127.0.0.1");/* call goes to 127.0.0.1 */$result = $client->some_method("parameter");

Protocols

Yar Header

Since Yar will support multi transfer protocols, so there is a Header struct, I call it Yar Header

#ifdef PHP_WIN32#pragma pack(push)#pragma pack(1)#endiftypedef struct _yar_header { uint32_t id; // transaction id uint16_t version; // protocl version uint32_t magic_num; // default is: 0x80DFEC60 uint32_t reserved; unsigned char provider[32]; // reqeust from who unsigned char token[32]; // request token, used for authentication uint32_t body_len; // request body len}#ifndef PHP_WIN32__attribute__ ((packed))#endifyar_header_t;#ifdef PHP_WIN32#pragma pack(pop)#endif

Packager Header

Since Yar also supports multi packager protocl, so there is a char[8] at the begining of body, to identicate which packager the body is packaged by.

Request

When a Client request a remote server, it will send a struct (in PHP):

'', //transaction id "m" => '', //the method which being called "p" => array(), //parameters)

Server

When a server response a result, it will send a struct (in PHP):

'', "s" => '', //status "r" => '', //return value "o" => '', //output "e" => '', //error or exception)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:centos 6.9 下忘记mysql密码
下一篇:SDL简单动画
相关文章

 发表评论

暂时没有评论,来抢沙发吧~