Home > PHP Framework > Laravel > body text

About Laravel Hprose RPC Service

藏色散人
Release: 2021-04-26 08:54:14
forward
2138 people have browsed it

下面由laravel教程栏目给大家介绍关于 Laravel Hprose RPC 服务,希望对需要的朋友有所帮助!

Laravel Hprose RPC 服务

开源地址:https://github.com/flc1125/laravel-hprose

该项目支持 Laravel 及非 Laravel 项目使用,支持  php >= 5.3 版本语法

注: 以下内容,默认你对 Hprose 已经有了一定了解。内容基于 Laravel 项目介绍。

安装

  • 环境依赖 C 扩展 hprose,详见:Hprose 官网、Hprose Pecl 扩展

    服务端安装即可,客户端可不安装
    pecl install hprose
    Copy after login
  • 安装扩展包

    composer require flc/laravel-hprose
    Copy after login

配置

服务提供者与门面

Laravel >= 5.5 已增加包自动发现。旧版本,请手动设置
  • config/app.php 文件下加入服务提供者

    'providers' => [
    
        ...
    
        Flc\Laravel\Hprose\HproseServiceProvider::class,
    ]
    Copy after login
  • config/app.php 文件下加入门面

    'aliases' => [
    
        ...
    
        'HproseRoute' => Flc\Laravel\Hprose\Facades\HproseRoute::class,
        'HproseServer' => Flc\Laravel\Hprose\Facades\HproseServer::class,
        'HproseClient' => Flc\Laravel\Hprose\Facades\HproseClient::class,
    ]
    Copy after login

初始化文件

php artisan hprose:generator
Copy after login

执行完成后,会分别生成以下文件:

  • 路由服务提供者:app/Providers/HproseRouteServiceProvider.php
  • 路由文件:routes/hprose.php (该路由文件非 Laravel 路由,下文简称 Hprose 路由

    Hprose 路由控制器根目录默认在:app/Http/Controllers/Hprose 目录下,可自行在 app/Providers/HproseRouteServiceProvider.php 调整

config/app.php 文件下加入默认的 Hprose 路由服务提供者

'providers' => [

    ...

    app\Providers\HproseRouteServiceProvider::class,
]
Copy after login

发布配置

php artisan vendor:publish --provider="Flc\Laravel\Hprose\HproseServiceProvider"
Copy after login

执行后,会生成配置文件路径:/config/hprose.php

使用

配置说明

配置文件路径:/config/hprose.php

<?php

return [
    &#39;server&#39; => [
        'default'     => 'http',
        'connections' => [
            'http' => [
                'protocol' => 'http',
            ],
        ],
    ],

    'client' => [
        'default'     => 'http',
        'connections' => array(
            'http' => array(
                'protocol' => 'http',
                'uri'      => 'http://192.168.2.67:9001/api/server',  // 此处为服务端的连接地址
                'async'    => false,
            ),
        ),
    ]
];
Copy after login

目前版本,除客户端配置中的远程服务器地址外,其他配置默认即可

其他均为后续支持 swoole-httptcpwebsocket 等,做提前架设

服务端

增加一个 Laravel 路由,用于启动 Http 服务,如:

<?php

Route::any(&#39;hprose-server&#39;, &#39;HproseController@server&#39;);
Copy after login

对应控制器:

<?php

namespace App\Http\Controllers;

use HproseServer;
use HproseRoute;

class HproseController
{
    public function server()
    {
        HproseServer::setRouter(HproseRoute::getRouter())->start();
    }
}
Copy after login

访问:http://localhost/hprose-server

Hprose 路由配置

<?php

HproseRoute::add(&#39;tests&#39;, &#39;Controller@tests&#39;);
HproseRoute::add(&#39;tests_one&#39;, &#39;Controller@tests&#39;)->option(['...']);
Copy after login
Controller@tests 的方法自行定义

客户端

<?php

namespace App\Http\Controllers;

use HproseClient;

class HproseController
{
    public function client()
    {
        $result = HproseClient::tests(&#39;tests&#39;);
        $result = HproseClient::connection(&#39;other&#39;)->tests('tests');  // 其他连接
        $result = HproseClient::connection()->tests->one('tests');

        print_r($result);
    }
}
Copy after login

非 Laravel 项目使用

服务端

<?php

$router = new \Flc\Laravel\Hprose\Routing\Router;
$router->group(['prefix' => 'tests', 'namespace' => 'App\\Controllers'], function ($router) {
    $router->add('one', 'Controller@one');
    $router->add('two', 'Controller@two')->option(['...']);

    $router->group(['prefix' => 'group'], function ($router) {
        $router->add('one', 'Controller@group_one');

        ...

    });
});

$app = [
    'config' => [
        'hprose.server.default' => 'http',
        'hprose.server.connections' => [
            'http' => [
                'protocol' => 'http',
            ],
        ],
    ],
];

$server = new \Flc\Laravel\Hprose\Server($app);
$server->setRouter($router)->start();
Copy after login

客户端

<?php
$app = [
    &#39;config&#39; => [
        'hprose.client.default' => 'http',
        'hprose.client.connections' => [
            'http' => [
                'protocol' => 'http',
                'uri'      => 'http://localhost/server.php',
                'async'    => false
            ]
        ]
    ]
];

$client = new \Flc\Laravel\Hprose\Client($app);

print_r($client->tests->one('222').PHP_EOL);
Copy after login

The above is the detailed content of About Laravel Hprose RPC Service. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template