php 使用yar作为rpc框架

如果只是php项目,不涉及go、python等,使用yar还是很方便的,尤其通过http get 能直接访问服务类的文档并行请求,很方便好用

一、下面是相关的一些帮助文档:

  1、https://www.laruence.com/2012/09/15/2779.html

  2、https://www.laruence.com/2020/03/16/5578.html

  3、https://github.com/laruence/yar

  4、https://pecl.php.net/package/yar

二、安装:

  Ubuntu

    1、采用pecl方式安装:pecl install yar (或指定版本安装:pecl install http://pecl.php.net/get/yar-2.2.1.tgz

    2、如果在安装过程中遇到错误:configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

     需要安装 libcurl4-openssl-dev (sudo apt-get install libcurl4-openssl-dev)

    3、如果遇到错误:ERROR: `/tmp/pear/temp/yar/configure --with-php-config=/usr/bin/php-config --enable-msgpack=yes' failed 

     需要安装:msgpack(sudo pecl install msgpack,这个可以提前安装)

  macOS

    1、mac系统自带的curl包不全,需要重新编译安装curl(下载地址:https://curl.haxx.se),如果按照完还报错的,可以根据错误提示安装相应的依赖

     我遇到的需要安装re2c(https://re2c.org/)brew install re2c,然后再编译安装curl

    2、安装yar和msgpack同Ubuntu安装方式

三、配置:

  需要把yar(yar.so)和msgpark(msgpack.so)的扩展配置到对应的php目录下,重启php

四、测试:

  参考最上面的文档或github

五、使用建议:

  1、最好简单封装一下框架,增加client和server端的配置,增加请求的安全验证(我是这样用的,代码未开源就不在这贴了)

  2、最好使用msgpack格式传递数据

  3、最新版本的yar,请求文档是有一段乱码(post请求没问题,强迫症可以安装2.2.1版本)

  

  

    

posted @ 2023-12-21 16:17  钻牛角尖儿  阅读(35)  评论(0编辑  收藏  举报