{"id":21517387,"url":"https://github.com/142vip/egg-grpc-server","last_synced_at":"2025-03-17T16:28:10.951Z","repository":{"id":47593811,"uuid":"510707530","full_name":"142vip/egg-grpc-server","owner":"142vip","description":"grpc服务端插件，支持开启grpc服务","archived":false,"fork":false,"pushed_at":"2022-07-21T03:16:35.000Z","size":243,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-31T03:31:35.809Z","etag":null,"topics":["142vip","egg-grpc","grpc","grpc-client","grpc-node","grpc-server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/142vip.png","metadata":{"files":{"readme":"README.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}},"created_at":"2022-07-05T11:30:48.000Z","updated_at":"2022-07-16T10:29:23.000Z","dependencies_parsed_at":"2022-09-26T21:21:46.727Z","dependency_job_id":null,"html_url":"https://github.com/142vip/egg-grpc-server","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/142vip%2Fegg-grpc-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/142vip%2Fegg-grpc-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/142vip%2Fegg-grpc-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/142vip%2Fegg-grpc-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/142vip","download_url":"https://codeload.github.com/142vip/egg-grpc-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244068122,"owners_count":20392751,"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":["142vip","egg-grpc","grpc","grpc-client","grpc-node","grpc-server"],"created_at":"2024-11-24T00:41:32.305Z","updated_at":"2025-03-17T16:28:10.912Z","avatar_url":"https://github.com/142vip.png","language":"JavaScript","readme":"## @142vip/egg-grpc-server\n\n\n### 功能规划\n\n- [x] grpc server自定义配置,平滑启动服务端\n- [x] agent.js 单例挂载\n- [x] app.js 多worker工作进程挂载，但存在端口冲突\n- [x] proto文件自动扫描、解析，支持语法报错检查\n- [x] js类自动实例化，仿Controller写法，最大限度支持egg.js框架\n- [x] 多worker启动，端口冲突解决 , 灵活配置agent加载\n- [x] 日志优化 \n\n\n### 安装\n\n```bash\n## 安装插件\nnpm install @142vip/egg-grpc-server --save\n\n## 安装指定版本\nnpm install @142vip/egg-grpc-server@xxx --save\n\n```\n\n### 快速使用\n\n```js\n// {app_root}/config/plugin.js\nexports.grpcServer = {\n  enable: true,\n  package: '@142vip/egg-grpc-server',\n};\n```\n\n### 配置\n\n```js\n// {app_root}/config/config.default.js\nexports.grpcServer = {\n    protoDir: 'app/grpc',   // 存放proto文件的目录，扫描.proto后缀文件\n    serviceDir: 'app/grpc', // 存放服务端远程方法的目录，扫描.js后缀文件\n    host: '127.0.0.1',      // 服务主机ip，建议填写0.0.0.0\n    port: '50051',          // grpc服务端口\n    loaderOptions: {\n        keepCase: true,\n        longs: String,\n        enums: String,\n        defaults: true,\n        oneofs: true,\n    },\n    agent: false,          // 是否在agent.js中加载，默认false\n    app: true,             // 是否在app.js中加载，默认true\n};\n\n\n// 用户配置会和默认配置进行对象深拷贝\nexports.grpcServer={\n    ...\n}\n\n```\n\n查看默认配置 [config/config.default.js](config/config.default.js) \n\n\n\n### 使用示例\n\n新建fairySister.js文件\n```js\n// {app_root}/app/grpc/fairySister.js\n'use strict';\n\nconst {GrpcBaseController}=require('egg-grpc-server')\n\nclass FairySisterController extends GrpcBaseController{\n    \n    \n    /**\n     * 调用示例\n     * @param request 请求参数，已封装成对象，具体属性可自行解构\n     * @returns{Object} 返回值，建议返回对象\n     */\n    async sayHello(request){\n        // 可以从this对象中解构出ctx和app，类似与egg中controller\n        const {ctx}=this;\n        const {username,password}=request\n        \n        // 支持service调用 ctx.sevice.xxxx\n        \n        // 返回对象，方便对应客户端解析\n        return {username,password}\n        \n    }\n\n\n    /**\n     * 健康检查\n     * @param data 请求参数，已封装成对象，具体属性可自行解构\n     * @returns{Object} 返回值，建议返回对象\n     */\n    async healthCheck(request){\n        const {ctx}=this\n        // 注意校验规则\n        ctx.validate({},request)\n        \n        return {started:true}\n    }\n}\n\n```\n\n新建fairySister.proto文件\n```protobuf\n// {app_root}/app/grpc/fairySister.proto\n\nsyntax = \"proto3\";\n\npackage FairySister;\n\n// 服务 可以有多个 , 但约定为一个\nservice vip {\n  rpc sayHello (RequestData) returns (ResponseData) {}\n  rpc healthCheck (RequestData) returns (ResponseData) {}\n}\n\n// grpc请求体结构 约定，无序改动\nmessage RequestData {\n  string data = 1;\n}\n\n// grpc响应体结构 约定，无序改动\nmessage ResponseData {\n  string data = 1;\n}\n```\n\n以上两个文件名称没有强制要求统一，但请注意：\n- .js文件建议导出class文件，并且继承`GrpcBaseController`类方便集成到egg.js中\n- .proto文件需要遵循protobuf规范，但注意插件本身封装的强约束\n- 建议用户只需要关注package、service、rpc这三部分，并且packageName 和 serviceName建议唯一，rpcName即对应方法可有多个，名称也唯一\n\n\n\n完成上述配置后，便可以如普通插件一样，执行npm指令启动egg项目，可以在启动日志中看到grpc服务端启动成功\n\n![img.png](./grpc-server-started.png)\n\n\n关于插件的任何问题，欢迎issues交流。详细grpc客户端、服务端插件使用，可以查看插件示例[egg-grpc-demo](https://github.com/142vip/egg-grpc-demo)\n\n### 证书\n\n```text\nMIT License\n\nCopyright (c) 2022 142vip FairySister Rong姐姐好可爱\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F142vip%2Fegg-grpc-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F142vip%2Fegg-grpc-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F142vip%2Fegg-grpc-server/lists"}