当前位置:首页 > Web开发 > 正文

手把手编写hyperf JsonRpc demo

2024-03-31 Web开发

1 处事端 provider.php

composer.json文件

"require": { "php": ">=7.2", "ext-swoole": ">=4.4", "hyperf/cache": "~1.1.0", "hyperf/command": "~1.1.0", "hyperf/config": "~1.1.0", "hyperf/contract": "~1.1.0", "hyperf/database": "~1.1.0", "hyperf/db-connection": "~1.1.0", "hyperf/devtool": "~1.1.0", "hyperf/di": "~1.1.0", "hyperf/dispatcher": "~1.1.0", "hyperf/event": "~1.1.0", "hyperf/exception-handler": "~1.1.0", "hyperf/framework": "~1.1.0", "hyperf/guzzle": "~1.1.0", "hyperf/http-server": "~1.1.0", "hyperf/logger": "~1.1.0", "hyperf/memory": "~1.1.0", "hyperf/paginator": "~1.1.0", "hyperf/pool": "~1.1.0", "hyperf/process": "~1.1.0", "hyperf/redis": "~1.1.0", "hyperf/utils": "~1.1.0", "hyperf/json-rpc": "~1.1.0", "hyperf/rpc": "~1.1.0", "hyperf/rpc-client": "~1.1.0", "hyperf/rpc-server": "~1.1.0", "hyperf/service-governance": "~1.1.0", "hyperf/config-apollo": "~1.1.0", "hyperf/constants": "~1.1.0", "hyperf/async-queue": "~1.1.0", "hyperf/amqp": "~1.1.0", "hyperf/model-cache": "~1.1.0", "hyperf/elasticsearch": "~1.1.0", "hyperf/tracer": "~1.1.0", "hyperf/consul": "^1.1", "hyperf/circuit-breaker": "^1.1", "hyperf/rate-limit": "^1.1" },

consul.php

<?php return [ ‘uri‘ => ‘:8500‘, ];

dependencies.php

<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://doc.hyperf.io * @contact [email protected] * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE */ use Hyperf\JsonRpc\JsonRpcPoolTransporter; use Hyperf\JsonRpc\JsonRpcTransporter; return [ JsonRpcTransporter::class => function () { return make(JsonRpcPoolTransporter::class); }, ];

server.php

‘servers‘ => [ [ ‘name‘ => ‘http‘, ‘type‘ => Server::SERVER_HTTP, ‘host‘ => ‘0.0.0.0‘, ‘port‘ => 9601, ‘sock_type‘ => SWOOLE_SOCK_TCP, ‘callbacks‘ => [ SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, ‘onRequest‘], ], ], [ ‘name‘ => ‘jsonrpc-http‘, ‘type‘ => Server::SERVER_HTTP, ‘host‘ => ‘0.0.0.0‘, ‘port‘ => 9604, ‘sock_type‘ => SWOOLE_SOCK_TCP, ‘callbacks‘ => [ SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, ‘onRequest‘], ], ], [ ‘name‘ => ‘jsonrpc‘, ‘type‘ => Server::SERVER_BASE, ‘host‘ => ‘0.0.0.0‘, ‘port‘ => 9603, ‘sock_type‘ => SWOOLE_SOCK_TCP, ‘callbacks‘ => [ SwooleEvent::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, ‘onReceive‘], ], ‘settings‘ => [ ‘open_eof_split‘ => true, ‘package_eof‘ => "\r\n", ], ], ],

新建处事

CalculatorService.php

<?php namespace App\JsonRpc; use Hyperf\RpcServer\Annotation\RpcService; /** * 注意,如但愿通过处事中心来打点处事,需在注解内增加 publishTo 属性 * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http",publishTo="consul") */ class CalculatorService implements CalculatorServiceInterface { // 实现一个加法要领,这里简单的认为参数都是 int 类型 public function add(int $a, int $b): int { $add = $a + $b; var_dump($add); // 这里是处事要领的具体实现 return $add; } }

CalculatorServiceInterface.php

<?php namespace App\JsonRpc; /** * Class CalculatorService * @package App\JsonRpc */ interface CalculatorServiceInterface { public function add(int $a, int $b): int; }

2 消费端

consul.php

<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://doc.hyperf.io * @contact [email protected] * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ ‘uri‘ => ‘:8500‘, ];

dependencies.php

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31936.html