Package Data | |
---|---|
Maintainer Username: | poplary |
Package Create Date: | 2020-08-13 |
Package Last Update: | 2020-12-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:07:17 |
Package Statistics | |
---|---|
Total Downloads: | 683 |
Monthly Downloads: | 113 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
直接使用
composer require poplary/lumen-hprose
在 bootstrap/app.php 注册 ServiceProvider
$app->register(\Poplary\LumenHprose\ServiceProvider::class);
配置.env文件
// 服务的名称
HPROSE_SERVICE=product
// 是否开启 debug
HPROSE_DEBUG=true
// Hprose 服务启用方式,可选 socket 和 swoole,选择 swoole 时需要安装 swoole 扩展
HPROSE_SERVER=socket
// 监听的 TCP 端口
HPROSE_URI=tcp://0.0.0.0:8889
创建配置
和路由
文件:
cp ./vendor/poplary/lumen-hprose/config/hprose.php ./config/hprose.php
cp ./vendor/poplary/lumen-hprose/routes/hprose.php ./routes/hprose.php
路由文件
routes/hprose.php
添加路由方法
use Poplary\LumenHprose\Facades\Router;
Router::add(string $name, string|callable $action, array $options = []);
发布远程调用方法 getUserByName
和 update
Router::add('getServiceName', 'Poplary\LumenHprose\Controllers\DemoController@getServiceName');
控制器
<?php
namespace Poplary\LumenHprose\Controllers;
/**
* Class DemoController.
*/
class DemoController
{
/**
* @return string
*/
public function getServiceName(): string
{
return config('hprose.service');
}
}
客户端调用 客户端可以只安装 Hprose
$client = new \Hprose\Socket\Client('tcp://127.0.0.1:8889', false);
echo $client->getServiceName();
路由组
Router::group(array $attributes, callable $callback);
Router::group(['namespace' => 'Poplary\LumenHprose\Controllers'], function ($route) {
$route->add('getServiceName', 'DemoController@getServiceName');
});
客户端调用
echo $client->getServiceName();
前缀
Router::group(['namespace' => 'Poplary\LumenHprose\Controllers', 'prefix' => 'demo'], function ($route) {
$route->add('getServiceName', 'DemoController@getServiceName');
});
客户端调用
echo $client->demo->getServiceName();
// 或者
echo $client->demo_getServiceName();
如果服务端出现 Exception ,因为 Hprose 没有返回 code,需要将code 合并到message用json方式包裹返回
try{
$client->user->getServiceName();
}catch(\Exception $e){
$info = json_decode($e->getMessage(),true);
$message = $info['message'];
$code = $info['code'];
}
php artisan hprose:server
更新了路由后需要重新启动服务
php artisan hprose:client:demo