PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

http://github.com/laruence/yar
Markdown | 233 lines | 199 code | 34 blank | 0 comment | 0 complexity | 6810887664823ddb78f4ecde6b8cfb3e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. # Yar - Yet Another RPC framework for PHP
  2. [![Build Status](https://secure.travis-ci.org/laruence/yar.png)](http://travis-ci.org/laruence/yar) [![Build status](https://ci.appveyor.com/api/projects/status/syhw33wlt2nad64i/branch/master?svg=true)](https://ci.appveyor.com/project/laruence/yar/branch/master) [![Build Status](https://github.com/laruence/yar/workflows/integrate/badge.svg?branch=master)](https://github.com/laruence/yar/actions?query=workflow%3Aintegrate)
  3. Light, concurrent RPC framework for PHP(see also: [Yar C framework](https://github.com/laruence/yar-c), [Yar Java framework](https://github.com/weibocom/yar-java))
  4. ## Requirement
  5. - PHP 7.0+ (master branch))
  6. - PHP 5.2+ ([php5 branch](https://github.com/laruence/yar/tree/php5))
  7. - Curl
  8. - Json
  9. - Msgpack (Optional)
  10. ## Introduction
  11. Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications
  12. It has the ability to concurrently call multiple remote services.
  13. ## Features
  14. - Fast, Easy, Simple
  15. - Concurrent RPC calls
  16. - Multiple data packager supported (php, json, msgpack built-in)
  17. - Multiple transfer protocols supported (http, https, TCP)
  18. - Detailed debug informations
  19. ## Install
  20. ### Install Yar
  21. Yar is an PECL extension, thus you can simply install it by:
  22. ```
  23. pecl install yar
  24. ```
  25. ### Compile Yar in Linux
  26. ```
  27. $/path/to/phpize
  28. $./configure --with-php-config=/path/to/php-config/
  29. $make && make install
  30. ```
  31. Available instructions to configure are
  32. ```
  33. --with-curl=DIR
  34. --enable(disable)-msgpack
  35. --enable(disable)-epoll (require Yar 2.1.2)
  36. ```
  37. ### Install Yar with msgpack
  38. first you should install msgpack-ext
  39. ```
  40. pecl install msgpack
  41. ```
  42. or , you can get the github source here: https://github.com/msgpack/msgpack-php
  43. then:
  44. ```
  45. $phpize
  46. $configure --with-php-config=/path/to/php-config/ --enable-msgpack
  47. $make && make install
  48. ```
  49. ## Runtime Configure
  50. - yar.timeout //default 5000 (ms)
  51. - yar.connect_timeout //default 1000 (ms)
  52. - yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack"
  53. - yar.debug //default Off
  54. - yar.expose_info // default On, whether output the API info for GET requests
  55. - yar.content_type // default "application/octet-stream"
  56. - yar.allow_persistent // default Off
  57. *NOTE* yar.connect_time is a value in milliseconds, and was measured in seconds in 1.2.1 and before.
  58. ## Constants
  59. - YAR_VERSION
  60. - YAR_OPT_PACKAGER
  61. - YAR_OPT_PERSISTENT
  62. - YAR_OPT_TIMEOUT
  63. - YAR_OPT_CONNECT_TIMEOUT
  64. - YAR_OPT_HEADER // Since 2.0.4
  65. - YAR_OPT_PROXY //Since 2.2.0
  66. ## Server
  67. It's very easy to setup a Yar HTTP RPC Server
  68. ```php
  69. <?php
  70. class API {
  71. /**
  72. * the doc info will be generated automatically into service info page.
  73. * @params
  74. * @return
  75. */
  76. public function some_method($parameter, $option = "foo") {
  77. }
  78. protected function client_can_not_see() {
  79. }
  80. }
  81. $service = new Yar_Server(new API());
  82. $service->handle();
  83. ?>
  84. ```
  85. 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:
  86. ![yar service info page](https://github.com/laruence/laruence.github.com/raw/master/yar_server.png)
  87. ## Client
  88. It's very simple for a PHP client to call remote RPC:
  89. ### Synchronous call
  90. ```php
  91. <?php
  92. $client = new Yar_Client("http://host/api/");
  93. /* the following setopt is optinal */
  94. $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);
  95. $client->SetOpt(YAR_OPT_HEADER, array("hd1: val", "hd2: val")); //Custom headers, Since 2.0.4
  96. /* call remote service */
  97. $result = $client->some_method("parameter");
  98. ?>
  99. ```
  100. ### Concurrent call
  101. ```php
  102. <?php
  103. function callback($retval, $callinfo) {
  104. var_dump($retval);
  105. }
  106. function error_callback($type, $error, $callinfo) {
  107. error_log($error);
  108. }
  109. Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
  110. Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters")); // if the callback is not specificed,
  111. // callback in loop will be used
  112. Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json"));
  113. //this server accept json packager
  114. Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_TIMEOUT=>1));
  115. //custom timeout
  116. Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
  117. //the error_callback is optional
  118. ?>
  119. ```
  120. ### Persistent call
  121. 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.
  122. ```php
  123. <?php
  124. $client = new Yar_Client("http://host/api/");
  125. $client->SetOpt(YAR_OPT_PERSISTENT, 1);
  126. $result = $client->some_method("parameter");
  127. /* The following calls will speed up due to keep-alive */
  128. $result = $client->some_other_method1("parameter");
  129. $result = $client->some_other_method2("parameter");
  130. $result = $client->some_other_method3("parameter");
  131. ?>
  132. ```
  133. ### Custom hostname resolving
  134. After Yar 2.1.0, if Yar runs on HTTP protocol, YAR_OPT_RESOLVE could be used to define custom hostname resolving.
  135. ```php
  136. <?php
  137. $client = new Yar_Client("http://host/api/");
  138. $client->SetOpt(YAR_OPT_RESOLVE, "host:80:127.0.0.1");
  139. /* call goes to 127.0.0.1 */
  140. $result = $client->some_method("parameter");
  141. ```
  142. ### Use http proxy
  143. After Yar 2.2.1, if Yar runs on HTTP protocol, YAR_OPT_PROXY could be used to define http proxy , such as fidder or charles.
  144. ```
  145. <?php
  146. $client = new Yar_Client("http://host/api/");
  147. $client->SetOpt(YAR_OPT_PROXY,"127.0.0.1:8888"); //http proxy , Since 2.2.0
  148. /* call goes to 127.0.0.1 */
  149. $result = $client->some_method("parameter");
  150. ```
  151. ## Protocols
  152. ### Yar Header
  153. Since Yar will support multi transfer protocols, so there is a Header struct, I call it Yar Header
  154. ```C
  155. #ifdef PHP_WIN32
  156. #pragma pack(push)
  157. #pragma pack(1)
  158. #endif
  159. typedef struct _yar_header {
  160. uint32_t id; // transaction id
  161. uint16_t version; // protocol version
  162. uint32_t magic_num; // default is: 0x80DFEC60
  163. uint32_t reserved;
  164. unsigned char provider[32]; // reqeust from who
  165. unsigned char token[32]; // request token, used for authentication
  166. uint32_t body_len; // request body len
  167. }
  168. #ifndef PHP_WIN32
  169. __attribute__ ((packed))
  170. #endif
  171. yar_header_t;
  172. #ifdef PHP_WIN32
  173. #pragma pack(pop)
  174. #endif
  175. ````
  176. ### Packager Header
  177. Since Yar also supports multi packager protocol, so there is a char[8] at the begining of body, to identicate which packager the body is packaged by.
  178. ### Request
  179. When a Client request a remote server, it will send a struct (in PHP):
  180. ```php
  181. <?php
  182. array(
  183. "i" => '', //transaction id
  184. "m" => '', //the method which being called
  185. "p" => array(), //parameters
  186. )
  187. ```
  188. ### Server
  189. When a server response a result, it will send a struct (in PHP):
  190. ```php
  191. <?php
  192. array(
  193. "i" => '',
  194. "s" => '', //status
  195. "r" => '', //return value
  196. "o" => '', //output
  197. "e" => '', //error or exception
  198. )
  199. ```