Hyperf微服务

一、

  hyperf3.0、swoole5.0、

二、

1.安装rpc相关依赖

#安装json-rpc协议
composer require hyperf/json-rpc

#安装rpc服务端
composer require hyperf/rpc-server

#安装rpc客户端
composer require hyperf/rpc-client

2.修改配置文件config/autoload/server.php ,增加TCP Server

<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link     https://www.hyperf.io
* @document https://hyperf.wiki
* @contact  group@hyperf.io
* @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
   'mode' => SWOOLE_PROCESS,
   'servers' => [
   //此处提供TCP Server (适配 jsonrpc 协议)
       [
           'name' => 'jsonrpc',
           'type' => Server::SERVER_BASE,
           'host' => '0.0.0.0',
           'port' => 9503,
           'sock_type' => SWOOLE_SOCK_TCP,
           'callbacks' => [
               Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
           ],
           'settings' => [
               'open_eof_split' => true,
               'package_eof' => "\r\n",
               'package_max_length' => 1024 * 1024 * 2,
           ],
       ],
       //此处配置提供http服务,
       [
           'name' => 'http',
           'type' => Server::SERVER_HTTP,
           'host' => '0.0.0.0',
           'port' => 9501,
           'sock_type' => SWOOLE_SOCK_TCP,
           'callbacks' => [
               Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
           ],
       ],
   ],
   //其他配置
   'settings' => [
       Constant::OPTION_ENABLE_COROUTINE => true,
       Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
       Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
       Constant::OPTION_OPEN_TCP_NODELAY => true,
       Constant::OPTION_MAX_COROUTINE => 100000,
       Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
       Constant::OPTION_MAX_REQUEST => 100000,
       Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
       Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
   ],
   'callbacks' => [
       Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
       Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
       Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
   ],
];

3.安装统一接入层

#安装统一接入层
composer require hyperf/service-governance

#安装适配器
composer require hyperf/service-governance-consul

4.发布服务到consul

#发布配置文件
php bin/hyperf.php vendor:publish hyperf/service-governance

 

 

@RpcService 共有 4 个参数:
name 属性为定义该服务的名称,这里定义一个全局唯一的名字即可,Hyperf 会根据该属性生成对应的 ID 注册到服务中心去;
protocol 属性为定义该服务暴露的协议,目前仅支持 jsonrpc-http, jsonrpc, jsonrpc-tcp-length-check ,分别对应于 HTTP 协议和 TCP 协议下的两种协议,默认值为 jsonrpc-http,这里的值对应在 Hyperf\Rpc\ProtocolManager 里面注册的协议的 key,它们本质上都是 JSON RPC 协议,区别在于数据格式化、数据打包、数据传输器等不同。
server 属性为绑定该服务类发布所要承载的 Server,默认值为 jsonrpc-http,该属性对应 config/autoload/server.php 文件内 servers 下所对应的 name,这里也就意味着我们需要定义一个对应的 Server;
publishTo 属性为定义该服务所要发布的服务中心,目前仅支持 consul、nacos 或为空,为空时代表不发布该服务到服务中心去,但也就意味着您需要手动处理服务发现的问题,要使用此功能需安装 hyperf/service-governance 组件及对应的驱动依赖,具体可参考 服务注册 章节;
以上配置完成后,在启动服务时,Hyperf 会提供HTTP服务和TCP服务,并自动地将 @RpcService 定义了 publishTo 属性为 consul 的服务注册到对应的服务中心去。

 

posted @ 2024-02-19 10:42  黑锦鲤  阅读(84)  评论(0)    收藏  举报