

基于 hprose/hprose-php 开发的Laravel扩展:whereof/laravel-hprose
source link: https://segmentfault.com/a/1190000040773655
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于 hprose/hprose-php 开发的Laravel扩展:whereof/laravel-hprose
基于 hprose/hprose-php 开发的Laravel扩展:whereof/laravel-hprose
composer require whereof/laravel-hprose
<?php return [ //rpc 服务 'server' => [ // hprose 调试模式 'debug' => true, //监听地址 'tcp_uris' => [ 'tcp://0.0.0.0:1314', ], //注册rpc 服务 目录地址 'route_path' => glob(base_path("rpc") . '/*.php'), // 通过路由查看注册的方法 'http' => [ // 如果设置false 在控制台显示调用方法,否在在路由显示调用方法 'enable' => false, //如果设置了true 这里就是路由前缀 'route_prefix' => 'rpc' ], ], //rpc 客户端 'client' => [ // 服务端监听地址 'tcp_uris' => [ 'tcp://127.0.0.1:1314', ], //是否异步 'async' => false ], ];
日志记录 /config/logging.php
'channels' => [ ............ 'hprose' => [ 'driver' => 'daily', 'path' => storage_path('logs/hprose.log'), 'days' => 14, ], ]
非强制配置,不配置,就会走默认的日志记录
laravel配置
//在 `config/app.php` 注册 HproseServiceProvider 'providers' => [ ..... \whereof\laravel\hprose\HproseServiceProvider::class ] php artisan vendor:publish --provider="whereof\laravel\hprose\HproseServiceProvider"
Lumen配置
将配置信息放在/config/hprose.php /bootstrap/app.php $app->register(\whereof\laravel\hprose\HproseServiceProvider::class); /路由注册 rpc/demo.php <?php use whereof\laravel\hprose\Facades\HproseRoute; // 注册callback HproseRoute::add(function () { return 'service hello'; }, 'hello'); // 注册class HproseRoute::add(\whereof\laravel\hprose\Services\UserServer::class); //注册中间价 HproseRoute::addInvokeHandler(function ($name, array &$args, stdClass $context, Closure $next) { \whereof\laravel\hprose\Support\LaravelHelper::log('调用的远程函数/方法名:' . $name, 'debug', $args); $result = $next($name, $args, $context); return $result; });
服务端 方法注入,类注入以及目录下类注入
<?php use whereof\laravel\hprose\Facades\HproseRoute; // 注册callback HproseRoute::add(function () { return 'service hello'; }, 'hello'); // 注册class HproseRoute::add(\whereof\laravel\hprose\Services\UserServer::class); //注册中间价 HproseRoute::addInvokeHandler(function ($name, array &$args, stdClass $context, Closure $next) { \whereof\laravel\hprose\Support\LaravelHelper::log('调用的远程函数/方法名:' . $name, 'debug', $args); $result = $next($name, $args, $context); return $result; }); // 注册整个目录 HproseRoute::addPath(app_path('Services'));
使用addPath的时候要注意:在类中构造方法__construct 参数不能是必传参数.
启动rpc服务
php artisan hprose:socket
客户端调用
$uris =['tcp://127.0.0.1:1314']; $client = new \whereof\laravel\hprose\Clients\SocketClient($uris, false); $client->hello() $client->whereof_hprose_demoService->kan() 需要配置配置 'client' => [ 'tcp_uris' => [ 'tcp://127.0.0.1:1314', ], 'async' => false ], app('hprose.socket.client')->hello()
Recommend
-
8
编写基于PHP扩展库的后门 lxj616 ·
-
13
基于PHP扩展的WAF实现 lightless ·
-
80
什么是 PHP 扩展 通俗说,PHP 扩展是增强 PHP 语言功能的插件。PHP 提供了编程语言的语法,比如分支、循环、函数、类等,这些是 PHP 本身所提供的。在某些情况下需要在 PHP 语言的基础上进行扩展,那么就需要通过 PHP 底层提供...
-
11
Laravel 7 扩展开发教程Summer摈弃世俗浮躁,追求技术精湛 原文链接:
-
9
PHP扩展开发:第一个扩展 浏览:1912次 出处信息 在上一篇文章《PHP扩展...
-
8
PHP扩展开发——函数 发布于 2021-08-01 | 分类于
-
9
PHP扩展开发——准备 发布于 2021-08-01 | 分类于
-
5
PHP扩展开发——EXPECTED与UNEXPECTED | anhoder的进阶日志 文章目录 站点概览 ...
-
1
这篇开始在扩展中实现PHP的类,先定个目标,实现如下的PHP类: <?php class Person { public const MALE = 1; public const FEMALE = 2; private $name; private $age; public function __construct(string $name, st...
-
11
PHP扩展开发-自动生成扩展骨架 2018-03-29 PHP扩展编译有两种方式 作为一个可装载模块或者DSO(动态共享对象) 静态编译到PHP 静态编译的方式直接和PH...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK