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' => [
        
        ],
    ],
];
相关推荐
百锦再6 小时前
对前后端分离与前后端不分离(通常指服务端渲染)的架构进行全方位的对比分析
java·开发语言·python·架构·eclipse·php·maven
andux14 小时前
layuiadmin与laravel 12 前后端分离nginx配置
php·layui·laravel·layuiadmin
m0_5791466515 小时前
docker desktop的容器间通信
docker·容器·php
007php00717 小时前
百度面试题解析:synchronized、volatile、JMM内存模型、JVM运行时区域及堆和方法区(三)
java·开发语言·jvm·缓存·面试·golang·php
KevinLyu19 小时前
内存管理篇(一)· zend_alloc 的基本概念
php
星光一影19 小时前
大型酒店管理系统源码(多酒店版)
mysql·php
从零开始的ops生活1 天前
【Day 80】Linux-NAS 和 SAN 存储
linux·运维·php
shizhenshide1 天前
为什么有时候 reCAPTCHA 通过率偏低,常见原因有哪些
开发语言·php·验证码·captcha·recaptcha·ezcaptcha
偶尔贪玩的骑士2 天前
Kioptrix Level 1渗透测试
linux·开发语言·网络安全·php
迎風吹頭髮2 天前
Linux服务器编程实践58-getnameinfo函数:通过socket地址获取主机名与服务名
开发语言·数据库·php