Hyperf jsonrpc

依赖的 composer 包

composer require hyperf/json-rpc

composer require hyperf/rpc-server

composer require hyperf/rpc-client

composer require hyperf/service-governance

composer require hyperf/service-governance-consul

composer require hyperf/service-governance-nacos

`

复制代码
<?php

namespace App\JsonRpc;

use Hyperf\RpcServer\Annotation\RpcService;

/**
 * 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo属性 值为consul, protocol是协议,默认是jsonrpc-http, 这里演示TCP协议 故使用 jsonrpc
 * @RpcService(name="FooService ", protocol="jsonrpc", server="jsonrpc-tcp", publishTo="consul")
 */
class FooService implements FooServiceInterface
{
    /**
     * @inheritDoc
     */
    public function hello()
    {
        return 'This is Foo JsonRpc Service.';
    }
}

配置hyperf服务启动 `/conifg/autoload/server.php` 这里演示TCP协议

复制代码
。。。

[

    'name' => 'jsonrpc-tcp',

    'type' => \Hyperf\Server\Server::SERVER_BASE,

    'host' => '0.0.0.0',

    'port' => 9503,

    'sock_type' => SWOOLE_SOCK_TCP,

    'callbacks' => [

        \Hyperf\Server\Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],

    ],

    'settings' => [

        'open_eof_split' => true,

        'package_eof' => "\r\n",

        'package_max_length' => 1024 * 1024 * 2,

    ],

],

配置注册中心 /conifg/autoload/services.php

复制代码
return [
    'enable'    => [
        // 开启服务发现
        'discovery' => true,
        // 开启服务注册
        'register'  => true,
    ],
    // 服务消费者相关配置
    'consumers' => [],
    // 服务提供者相关配置
    'providers' => [],
    // 服务驱动相关配置
    'drivers'   => [
        'consul' => [
            
        ],
        'nacos' => [
        
        ],
    ],
];
相关推荐
ServBay6 小时前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
JaguarJack3 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo3 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack4 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo4 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack4 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay5 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954485 天前
CTF 伪协议
php
BingoGo8 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack8 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端