motan / motan-php
Motan For PHP
Requires
- wei-breeze/breeze: v0.1.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is not auto-updated.
Last update: 2024-09-26 07:32:38 UTC
README
概述
Motan 是一个跨语言的远程过程调用(RPC)框架,用于快速开发高性能分布式服务。
本项目是 PHP Motan 的实现。提供 PHP motan 客户端。
快速开始
安装
使用 composer
只需克隆此项目并将其添加到您的 composer.json
文件中。
不使用 composer
如果您没有使用 composer 来管理 PHP 库,您可以手动安装 motan-php,例如使用 git clone
,请查看 motan-example 中的示例。
用法
我们需要一个定义的常量 MOTAN_PHP_ROOT
来加载 motan php 库。就像示例中所做的那样。
define('MOTAN_PHP_ROOT', './vendor/motan/motan-php/src/Motan/'); require MOTAN_PHP_ROOT . 'init.php';
快速开始提供了一个在同一台机器上运行客户端和服务器的基本示例。有关使用和开发 Motan 的详细信息,请转到 文档。示例案例在 main/ 目录下。
Motan 服务器
我们使用 Weibo-Mesh 支持一个 PHP 服务器,Weibo-Mesh 是用 Golang 编写的本地代理。但不仅是一个代理,Weib-Mesh 还具备服务治理的能力。在 motan-example 中有一个示例。
作为 php-fpm 的 CGI 代理
cgi-mesh-example-helloworld: path: com.weibo.motan.HelloWorldService export: "motan2:9991" provider: cgi CGI_HOST: 10.211.55.3 CGI_PORT: 9000 CGI_REQUEST_METHOD: GET CGI_SCRIPT_FILENAME: /motan-examples/php-server/index.php CGI_DOCUMENT_ROOT: /motan-examples/php-server basicRefer: mesh-server-basicService
作为任何 HTTP 服务器的 HTTP 代理
http-mesh-example-helloworld: path: com.weibo.motan.HelloWorldService export: "motan2:9990" provider: http HTTP_REQUEST_METHOD: GET HTTP_URL: http://10.211.55.3:9900/http_server basicRefer: mesh-server-basicService
Motan 客户端
这里有一个关于 Motan 客户端的简单示例,它将通过 [Weibo-Mesh Testhelper][testhelper] 调用远程服务提供者,您可以在 [phpt 测试][phpts] 中找到更多示例,您只需运行 ./run.sh
即可找到更多。
$app_name = 'search'; $service = 'com.weibo.HelloMTService'; $group = 'motan-demo-rpc'; $remote_method = 'HelloW'; $params = ['idevz'=>'for weibo-mesh']; $cx = new Motan\MClient( $app_name ); $request = new \Motan\Request($service, $remote_method, $params); $request->setGroup($group); try{ $res = $cx->doCall($request); } catch(Exception $e) { var_dump($e->getMessage()); }
单元测试
如果您在本地 PHP 环境中运行单元测试,请确保您的 PHP 版本在 7.2 到 7.4 之间,支持 pcntl_fork、posix_kill、pcntl_signal 等功能,并下载了 composer 组件。您可以通过以下说明运行单元测试。
git clone https://github.com/weibocom/motan-php.git cd motan-php git checkout $(test branch) composer install composer test # just run phpunit composer test-coverage # run phpunit and generate report
如果您不想安装与 PHP 相关的环境,建议您使用 docker 来避免环境不一致的问题。您可以按照以下说明操作。
git clone https://github.com/weibocom/motan-php.git cd motan-php git checkout $(test branch) docker run --rm -it -v $PWD:/mnt --entrypoint="" snail007/php:7.4 bash cd /mnt composer install composer test # just run phpunit composer test-coverage # run phpunit and generate report
贡献者
许可证
Motan 根据 Apache License 2.0 发布。
欢迎提交问题和贡献代码!