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' => [
        
        ],
    ],
];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值