docker 拉取相关镜像
客户端
docker run --name hyperf \
-v /workspace/skeleton:/data/project \
-p 9501:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.0-alpine-v3.15-swoole
服务端
docker run --name hyperfRpc \
-v /workspace/skeleton:/data/project \
-p 9553:9553 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.0-alpine-v3.15-swoole
consul
docker pull consul
docker run --name=self-consul -p 8500:8500 -e CONSUL_BIND_INTERFACE=eth0 -d consul
consul 安装成功以后输入对应的IP地址 http://xxx.xxx.xxx.xx:8500/ui/进入后出现如下页面。
安装如下composer包
- composer require hyperf/json-rpc / 该组件只是 JSON RPC 的协议处理的组件
- composer require hyperf/rpc-server / JSON RPC 服务端
- composer require hyperf/rpc-client / JSON RPC 客户端
- composer require hyperf/service-governance / 安装统一接入层
- composer require hyperf/service-governance-consul / 服务注册支持 Consul
- composer require hyperf/consul /Consul服务发现和配置中心扩展包
创建 services.php文件
执行如下命令后扩展包的默认配置文件将被复制到config/autoload目录下,重点是其中会多一个services.php
php bin/hyperf.php vendor:publish hyperf/service-governance
配置服务端
创建一个服务接口
<?php
declare(strict_types=1);
namespace App\JsonRpc;
interface CalculatorServiceInterface
{
public function add(int $a,int $b);
}
创建一个服务
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
# name 属性为定义该服务的名称,这里定义一个全局唯一的名字即可,Hyperf 会根据该属性生成对应的 ID 注册到服务中心去;
# protocol 属性为定义该服务暴露的协议,目前仅支持 jsonrpc 和 jsonrpc-http,分别对应于 TCP 协议和 HTTP 协议下的两种协议,默认值为 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 组件及对应的驱动依赖;
#[RpcService(name: "CalculatorService", protocol: "jsonrpc-http", server: "jsonrpc-http",publishTo: 'consul')]
class CalculatorService implements CalculatorServiceInterface
{
public function add(int $a, int $b)
{
return $a+$b;
}
}
配置 server.php
<?php
declare(strict_types=1);
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;
return [
'mode' => SWOOLE_PROCESS,
'servers' => [
[
// 这里基于jsonrpc-http 因为这里我们只需要作为服务所以只需要这一个server就够了
'name' => 'jsonrpc-http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9503,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::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'],
],
];
配置 services.php
<?php
declare(strict_types=1);
return [
'enable' => [
// 开启服务发现
'discovery' => true,
// 开启服务注册
'register' => true,
],
// 服务消费者相关配置
'consumers' => [],
// 服务提供者相关配置
'providers' => [],
// 服务驱动相关配置
'drivers' => [
'consul' => [
// 这里就是上面docker拉去的 consul暴露的地址+端口
'uri' => 'http://xxx.xxx.xxx.xxx:8500',
'token' => '',
'check' => [
'deregister_critical_service_after' => '90m',
'interval' => '1s',
],
]
],
];
客户端配置
创建相对应的接口
<?php
declare(strict_types=1);
namespace App\JsonRpc;
interface CalculatorServiceInterface
{
public function add(int $a,int $b);
}
配置server.php
<?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' => [
[
'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'],
],
'providers' => [
// \App\Provider\HelperProvider::class
],
];
配置services.php 【这里比较重要】
<?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
*/
return [
'enable' => [
'discovery' => true,
'register' => true,
],
'consumers' => [
[
// The service name, this name should as same as with the name of service provider.
'name' => 'CalculatorService',
// The service registry, if `nodes` is missing below, then you should provide this configs.
// 服务接口名,可选,默认值等于 name 配置的值,如果 name 直接定义为接口类则可忽略此行配置,如 name 为字符串则需要配置 service 对应到接口类
'service' => \App\JsonRpc\CalculatorServiceInterface::class,
// 服务提供者的服务协议,可选,默认值为 jsonrpc-http
'protocol' => 'jsonrpc-http',
// 负载均衡算法,可选,默认值为 random
'load_balancer' => 'random',
// 对应容器对象 ID,可选,默认值等于 service 配置的值,用来定义依赖注入的 key
'id' => \App\JsonRpc\CalculatorServiceInterface::class,
// If `registry` is missing, then you should provide the nodes configs.
'nodes' => [
// 这里就是服务发布的位置就是服务端配置server.php中的那个
['host' => 'xxx.xxx.xxx.xxx', 'port' => 9503]
],
// 配置项,会影响到 Packer 和 Transporter
'options' => [
'connect_timeout' => 5.0,
'recv_timeout' => 5.0,
'settings' => [
// 根据协议不同,区分配置
'open_eof_split' => true,
'package_eof' => "\r\n",
// 'open_length_check' => true,
// 'package_length_type' => 'N',
// 'package_length_offset' => 0,
// 'package_body_offset' => 4,
],
// 当使用 JsonRpcPoolTransporter 时会用到以下配置
'pool' => [
'min_connections' => 1,
'max_connections' => 32,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => 60.0,
],
]
],
],
'providers' => [],
'drivers' => [
'consul' => [
// 这里就是上面docker拉去的 consul暴露的地址+端口
'uri' => 'http://xxx.xxx.xxx.xxx:8500',
'token' => '',
'check' => [
'deregister_critical_service_after' => '90m',
'interval' => '1s',
],
]
],
];
在客户端中进行测试
#[Inject]
public CalculatorServiceInterface $cal;
public function index()
{
return $this->cal->add(1,2);
}