{"id":13699703,"url":"https://github.com/hyperf/jet","last_synced_at":"2025-04-06T08:32:05.427Z","repository":{"id":55624769,"uuid":"286282304","full_name":"hyperf/jet","owner":"hyperf","description":"🚀 Jet is a unification model RPC Client, built-in JSON RPC protocol, available to running in ALL PHP environments, including PHP-FPM and Swoole/Hyperf environments.","archived":false,"fork":false,"pushed_at":"2024-01-11T07:50:27.000Z","size":78,"stargazers_count":57,"open_issues_count":0,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-03T12:56:54.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyperf.png","metadata":{"files":{"readme":"README-CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"open_collective":"hyperf","custom":"https://hyperf.wiki/#/zh-cn/donate"}},"created_at":"2020-08-09T17:15:14.000Z","updated_at":"2024-08-04T01:31:58.000Z","dependencies_parsed_at":"2024-01-11T10:55:34.199Z","dependency_job_id":null,"html_url":"https://github.com/hyperf/jet","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Fjet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Fjet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Fjet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Fjet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperf","download_url":"https://codeload.github.com/hyperf/jet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223245061,"owners_count":17112572,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T20:00:42.165Z","updated_at":"2024-11-05T21:09:30.661Z","avatar_url":"https://github.com/hyperf.png","language":"PHP","readme":"[English](./README.md) | 中文\n\n\u003cp align=\"center\"\u003e\u003ca href=\"https://hyperf.io\" target=\"_blank\" rel=\"noopener noreferrer\"\u003e\u003cimg width=\"70\" src=\"https://cdn.jsdelivr.net/gh/hyperf/hyperf/docs/logo.png\" alt=\"Hyperf Logo\"\u003e\u003c/a\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/hyperf/jet/releases\"\u003e\u003cimg src=\"https://poser.pugx.org/hyperf/jet/v/stable\" alt=\"Stable Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/hyperf/jet\"\u003e\u003cimg src=\"https://travis-ci.org/hyperf/jet.svg?branch=master\" alt=\"Build Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/hyperf/jet\"\u003e\u003cimg src=\"https://poser.pugx.org/hyperf/jet/downloads\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/hyperf/jet\"\u003e\u003cimg src=\"https://poser.pugx.org/hyperf/jet/d/monthly\" alt=\"Monthly Downloads\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.php.net\"\u003e\u003cimg src=\"https://img.shields.io/badge/php-%3E=7.2-brightgreen.svg?maxAge=2592000\" alt=\"Php Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/hyperf/jet/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/hyperf/jet.svg?maxAge=2592000\" alt=\"Hyperf Jet License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Jet, by Hyperf\n\nJet 是一个统一模型的 RPC 客户端，内置 JSONRPC 协议的适配，该组件可适用于所有的 PHP 环境，包括 PHP-FPM 和 Swoole 或 Hyperf。（在 Hyperf 环境下，目前仍建议直接使用 `hyperf/json-rpc` 组件来作为客户端使用）\n\n\u003e 未来还会内置 gRPC 和 Tars 协议。\n\n# 安装\n\n```bash\ncomposer require hyperf/jet\n```\n\n# 快速开始\n\n## 注册协议\n\n\u003e 注册协议不是必须的一个步骤，但您可以通过 ProtocolManager 管理所有的协议。\n\n您可以通过 `Hyperf\\Jet\\ProtocolManager` 类来注册管理任意的协议，每个协议会包含 Transporter, Packer, DataFormatter and PathGenerator 几个基本的组件，您可以注册一个 JSONRPC 协议，如下：\n\n```php\n\u003c?php\n\nuse Hyperf\\Jet\\DataFormatter\\DataFormatter;\nuse Hyperf\\Jet\\Packer\\JsonEofPacker;\nuse Hyperf\\Jet\\PathGenerator\\PathGenerator;\nuse Hyperf\\Jet\\ProtocolManager;\nuse Hyperf\\Jet\\Transporter\\StreamSocketTransporter;\n\nProtocolManager::register($protocol = 'jsonrpc', [\n    ProtocolManager::TRANSPORTER =\u003e new StreamSocketTransporter(),\n    ProtocolManager::PACKER =\u003e new JsonEofPacker(),\n    ProtocolManager::PATH_GENERATOR =\u003e new PathGenerator(),\n    ProtocolManager::DATA_FORMATTER =\u003e new DataFormatter(),\n]);\n```\n\n## 注册服务\n\n\u003e 注册服务不是必须的一个步骤，但您可以通过 ServiceManager 管理所有的服务。\n\n在您往 `Hyperf\\Jet\\ProtocolManager` 注册了一个协议之后，您可以通过 `Hyperf\\Jet\\ServiceManager` 将协议绑定到任意的服务上，如下：\n\n```php\n\u003c?php\nuse Hyperf\\Jet\\ServiceManager;\n\n// 绑定 CalculatorService 与 jsonrpc 协议，同时设定静态的节点信息\nServiceManager::register($service = 'CalculatorService', $protocol = 'jsonrpc', [\n    ServiceManager::NODES =\u003e [\n        [$host = '127.0.0.1', $port = 9503],\n    ],\n]);\n```\n// 如果需要使用 Consul 服务\n```php\n\u003c?php\n\nuse Hyperf\\Jet\\DataFormatter\\DataFormatter;\nuse Hyperf\\Jet\\Packer\\JsonEofPacker;\nuse Hyperf\\Jet\\PathGenerator\\PathGenerator;\nuse Hyperf\\Jet\\ProtocolManager;\nuse Hyperf\\Jet\\Transporter\\ConsulTransporter;\n\n\nProtocolManager::register($protocol = 'consul', [\n    ProtocolManager::TRANSPORTER =\u003e new StreamSocketTransporter(),\n    ProtocolManager::PACKER =\u003e new JsonEofPacker(),\n    ProtocolManager::PATH_GENERATOR =\u003e new PathGenerator(),\n    ProtocolManager::DATA_FORMATTER =\u003e new DataFormatter(),\n    ProtocolManager::NODE_SELECTOR =\u003e new NodeSelector($this-\u003ehost, $this-\u003eport, $config), \n]);\n```\n\n## 调用 RPC 方法\n\n### 通过 ClientFactory 调用\n\n在您注册完协议与服务之后，您可以通过 `Hyperf/Jet/ClientFactory` 来获得您的服务的客户端，如下所示：\n\n```php\n\u003c?php\nuse Hyperf\\Jet\\ClientFactory;\n\n$clientFactory = new ClientFactory();\n$client = $clientFactory-\u003ecreate($service = 'CalculatorService', $protocol = 'jsonrpc');\n```\n\n当您拥有 client 对象后，您可以通过该对象调用任意的远程方法，如下：\n\n```php\n// 调用远程方法 `add` 并带上参数 `1` 和 `2`\n// $result 即为远程方法的返回值\n$result = $client-\u003eadd(1, 2);\n```\n\n当您调用一个不存在的远程方法时，客户端会抛出一个 `Hyperf\\Jet\\Exception\\ServerException` 异常。\n\n### 通过自定义客户端调用\n\n您可以创建一个 `Hyperf\\Jet\\AbstractClient` 的子类作为自定义的客户端类，来完成远程方法的调用，比如，您希望定义一个 `CalculatorService` 服务的 `jsonrpc` 协议的客户端类，您可以先定义一个 `CalculatorService` 类，如下所示：\n\n```php\n\u003c?php\n\nuse Hyperf\\Jet\\AbstractClient;\nuse Hyperf\\Jet\\Packer\\JsonEofPacker;\nuse Hyperf\\Jet\\Transporter\\StreamSocketTransporter;\nuse Hyperf\\Rpc\\Contract\\DataFormatterInterface;\nuse Hyperf\\Rpc\\Contract\\PackerInterface;\nuse Hyperf\\Rpc\\Contract\\PathGeneratorInterface;\nuse Hyperf\\Rpc\\Contract\\TransporterInterface;\n\n/**\n * @method int add(int $a, int $b);\n */\nclass CalculatorService extends AbstractClient\n{\n    // 定义 `CalculatorService` 作为 $service 参数的默认值\n    public function __construct(\n        string $service = 'CalculatorService',\n        TransporterInterface $transporter = null,\n        PackerInterface $packer = null,\n        ?DataFormatterInterface $dataFormatter = null,\n        ?PathGeneratorInterface $pathGenerator = null\n    ) {\n        // 这里指定 transporter，您仍然可以通过 ProtocolManager 来获得 transporter 或从构造函数传递\n        $transporter = new StreamSocketTransporter('127.0.0.1', 9503);\n        // 这里指定 packer，您仍然可以通过 ProtocolManager 来获得 packer 或从构造函数传递\n        $packer = new JsonEofPacker();\n        parent::__construct($service, $transporter, $packer, $dataFormatter, $pathGenerator);\n    }\n}\n```\n\n如果使用 Consul 服务，可以按照下面的用法来进行注册\n\n```php\n\u003c?php\n\nuse Hyperf\\Jet\\AbstractClient;\nuse Hyperf\\Jet\\Packer\\JsonEofPacker;\nuse Hyperf\\Jet\\Transporter\\StreamSocketTransporter;\nuse Hyperf\\Rpc\\Contract\\DataFormatterInterface;\nuse Hyperf\\Rpc\\Contract\\PackerInterface;\nuse Hyperf\\Rpc\\Contract\\PathGeneratorInterface;\nuse Hyperf\\Rpc\\Contract\\TransporterInterface;\nuse Hyperf\\Jet\\NodeSelector\\NodeSelector;\n\n/**\n * @method int add(int $a, int $b);\n */\nclass CalculatorService extends AbstractClient\n{\n    // Define `CalculatorService` as the default value of $service.\n    public function __construct(\n        string $service = 'CalculatorService',\n        TransporterInterface $transporter = null,\n        PackerInterface $packer = null,\n        ?DataFormatterInterface $dataFormatter = null,\n        ?PathGeneratorInterface $pathGenerator = null\n    ) {\n        // Specific the transporter here, you could also retrieve the transporter from ProtocolManager or passing by constructor.\n        $transporter = new StreamSocketTransporter();\n        $nodeSelector = new NodeSelector('127.0.0.1', 8500, $config);\n        [$transporter-\u003ehost, $transporter-\u003eport] = $nodeSelector-\u003eselectRandomNode($service, 'jsonrpc');\n        // Specific the packer here, you could also retrieve the packer from ProtocolManager or passing by constructor.\n        $packer = new JsonEofPacker();\n        parent::__construct($service, $transporter, $packer, $dataFormatter, $pathGenerator);\n    }\n}\n```\n\n现在，您可以通过该类来直接调用远程方法了，如下所示：\n\n```php\n// 调用远程方法 `add` 并带上参数 `1` 和 `2`\n// $result 即为远程方法的返回值\n$client = new CalculatorService();\n$result = $client-\u003eadd(1, 2);\n```\n","funding_links":["https://opencollective.com/hyperf","https://hyperf.wiki/#/zh-cn/donate"],"categories":["Frameworks"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Fjet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperf%2Fjet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Fjet/lists"}