Wallogit.com
2017 © Pedro Peláez
laravel swoole扩展
基于 swoole 的 laravel RPC 扩展包, 目前提供了 RPC Client , RPC Server 和集群机器节点管理 server 功能, (*1)
安装, (*2)
composer require jackdou/laravel-swoole
php artisan vender:publish
Provider: JackDou\Swoole\SwooleServiceProvider 生成配置文件config/swoole.php 中修改config/server_node.php 中可以根据自己的服务名称配置多个机器ipmanagement 管理后台,需要将服务发现方式改为 2(推荐)php artisan swoole:server server_name start/stop/reload //开启、关闭、热重启 server_name 服务ps aux | grep 'your server_name' 可以查看启动的进程信息fpm 中使用同步客户端访问微服务 demo, (*3)
编写微服务的业务代码:默认目录为 app/Services,新建 TestService.php 类并添加如下内容, (*4)
namespace App\Services;
class TestService
{
//服务类都需要是静态属性
public static function test($msg = 'hello world')
{
return ['code' => 1, 'msg' => $msg];
}
}
在项目任意可以访问的 Controller 中添加如下内容, (*5)
use JackDou\Swoole\Facade\Service;
//getResult 参数为超时时间,单位秒,默认0.5秒。如果服务在0.5秒内没有返回结果则返回null
$res = Service::getInstance('swoole')->call('TestService::test', '你好')->getResult(0.5);
dd($res);
若你的 laravel 项目使用 swooletw/laravel-swoole 等类似组件将框架常驻进程或者在服务的项目代码里可以使用协程异步客户端提高性能, (*6)
Coroutine\Client 使用 demo, (*7)
namespace App\Services;
class TestService
{
//测试协程客户端
public static function test_co($msg = 'hello world')
{
//常规调用
try {
$co = new SwooleCoClient();
$co->getInstance('swoole')
->call('TestService::test')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($err_msg, $err_code) {
//请求失败了,在这里记录日志等
})->run();
} catch (\Throwable $throwable) {
//寻找服务等发生错误,在这里看原因
}
//在一个请求内对一个服务多次调用
try {
$co = new SwooleCoClient();
$swoole = $co->getInstance();//同一个服务只用指定一次
$swoole->call('TestService::test')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
$swoole->call('TestService::test_node')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
$swoole->run();
} catch (\Throwable $throwable) {
//寻找服务等错误,在这里查看
}
//在一个请求内对不同服务调用
try {
$co = new SwooleCoClient();
$swoole = $co->getInstance('swoole');
$swoole->call('TestService::test')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
//重新指定另一个服务
$swoole = $co->getInstance('swoole2');
$swoole->call('TestService::test_node')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
$swoole->run();
} catch (\Throwable $throwable) {
//寻找服务等错误,在这里查看
}
//请求成功一个以后请求另一个服务
try {
$co = new SwooleCoClient();
$swoole = $co->getInstance('swoole');
$swoole->call('TestService::test')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
$co2 = new SwooleCoClient();
$swoole2 = $co2->getInstance('swoole2');
$swoole2->call('TestService::test_node')
->onSuccess(function ($response_data) {
//$response_data 响应数据,请求的service返回的数据结构
//在这里处理请求成功返回数据以后的逻辑
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
$swoole2->run();
})->onFail(function ($msg, $code) {
//请求失败了,在这里记录日志等
});
$swoole->run();
} catch (\Throwable $throwable) {
//寻找服务等错误,在这里查看
}
//代码在这里返回,此次请求结束,但是上面的服务调用可能还在执行中!!!
return ['code' => 1, 'msg' => $msg];
}
}
supervisor 管理 swoole 进程v1.2.2 后支持通过 .env 文件区分测试环境和正式环境监听的服务 ip 和 port
reload 或重启服务进程,不然不会生效php fpm 和 cli 的 php.ini 通常不是一个文件,在 fpm 中使用时需要保证对应的 php.ini 文件中添加了 swoole 扩展cpu io 的场景下可以提高性能,推荐使用,但是注意异步代码的执行逻辑和编写方式