{"id":20518507,"url":"https://github.com/jeff-tian/egg-grpc-client-ts-example","last_synced_at":"2026-02-09T12:32:12.636Z","repository":{"id":42091089,"uuid":"271961253","full_name":"Jeff-Tian/egg-grpc-client-ts-example","owner":"Jeff-Tian","description":"[`egg-grpc-client-ts`](https://github.com/Jeff-Tian/egg-grpc-client-ts) 的应用实例","archived":false,"fork":false,"pushed_at":"2024-12-11T07:21:12.000Z","size":303,"stargazers_count":2,"open_issues_count":23,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-18T06:22:27.729Z","etag":null,"topics":["egg-plugin","eggjs","grpc"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jeff-Tian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"jeff_tian","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-06-13T07:23:13.000Z","updated_at":"2021-01-05T06:16:49.000Z","dependencies_parsed_at":"2023-02-13T13:35:20.682Z","dependency_job_id":null,"html_url":"https://github.com/Jeff-Tian/egg-grpc-client-ts-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jeff-Tian%2Fegg-grpc-client-ts-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jeff-Tian%2Fegg-grpc-client-ts-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jeff-Tian%2Fegg-grpc-client-ts-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jeff-Tian%2Fegg-grpc-client-ts-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jeff-Tian","download_url":"https://codeload.github.com/Jeff-Tian/egg-grpc-client-ts-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234183245,"owners_count":18792698,"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":["egg-plugin","eggjs","grpc"],"created_at":"2024-11-15T21:44:36.090Z","updated_at":"2026-02-09T12:32:07.597Z","avatar_url":"https://github.com/Jeff-Tian.png","language":"TypeScript","funding_links":["https://patreon.com/jeff_tian"],"categories":[],"sub_categories":[],"readme":"egg-grpc-client-ts-example\n\n---\n\n\u003e [`egg-grpc-client-ts`](https://github.com/Jeff-Tian/egg-grpc-client-ts) 的应用实例\n\n## 本地运行\n```shell script\ngit clone https://github.com/Jeff-Tian/egg-grpc-client-ts-example\nnpm i\nnpm run dev\nopen http://localhost:7001\n```\n\n## 来由\n\n应 [该 issue](https://github.com/Jeff-Tian/egg-grpc-client-ts/issues/7) 之请求，制作此仓库已给使用 [`egg-grpc-client-ts`](https://github.com/Jeff-Tian/egg-grpc-client-ts)  一个完整的示例。\n\n## 前置条件\n\n一个正常运行的 gRPC 服务。\n\n本示例使用这个 [gRPC 服务](https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check) ，运行在 5000 端口。\n\n你可以在本地\n\n```shell script\ngit clone https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check\ncd nestjs-hero-grpc-sample-with-health-check\n\nnpm start\n```\n\n运行这个 gRPC 服务。\n\n## 开始\n\n### 安装 egg\n\n因为 [`egg-grpc-client-ts`](https://github.com/Jeff-Tian/egg-grpc-client-ts) 是一个 egg 插件，所以会被用在 egg 项目中。\n\n按照 [eggjs](https://eggjs.org/zh-cn/intro/quickstart.html) 官网，创建 egg 项目：\n\n```shell script\nmkdir egg-grpc-client-ts-example \u0026\u0026 cd egg-grpc-client-ts-example\nnpm init egg --type=ts\nnpm i\n```\n\n### 本地运行\n\n```shell script\nnpm run dev\nopen http://localhost:7001\n```\n\n看到页面返回 `hi, egg`\n\n## 安装 `egg-grpc-client-ts` 插件\n\n```shell script\nnpm i egg-grpc-client-ts --save\n```\n\n### 配置\n\n```shell script\n// config/plugin.ts\n\nplugin.grpcClient = {\n    enable: true,\n    package: 'egg-grpc-client-ts',\n};\n```\n\n```shell script\n// config/config.default.ts\n\ngrpcClient: {\n    clients: [\n        {\n            name: 'hero',\n            protoPath: 'app/proto/hero',\n            host: '0.0.0.0',\n            port: 5000,\n        },\n    ],\n}\n```\n\n### 在 service 中使用\n\n改造 `app/service/Test.ts`：\n\n```shell script\nconst result = await this.app.grpcClient\n            .get('hero').hero\n            .HeroService.FindOne({\n                id: 1\n            })\n\n        return `hi, ${name}, this is from gRPC service: ${JSON.stringify(result)}`\n```\n\n## 运行\n\n```shell script\nnpm run dev\nopen http://localhost:7001\n```\n\n这是看到页面上显示：\n\n```\nhi, egg, this is from gRPC service: {\"id\":1,\"name\":\"John\"}\n```\n\n# 祝贺你，成功了！\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeff-tian%2Fegg-grpc-client-ts-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeff-tian%2Fegg-grpc-client-ts-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeff-tian%2Fegg-grpc-client-ts-example/lists"}