# ohos_grpc_node **Repository Path**: wurep/ohos_grpc_node ## Basic Information - **Project Name**: ohos_grpc_node - **Description**: 移步到仓库https://gitcode.com/openharmony-sig/ohos_grpc_node - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-10 - **Last Updated**: 2025-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ohos_grpc_node ## 简介 ohos_grpc_node是参考gRPC使用arkTs语言重构实现的一个高性能的远程过程调用(RPC)框架,旨在简化和优化分布式系统中的服务通信。 ## 使用说明 ### 安装grpc和http2相关模块: - 把当前工程下的library和libraryHttp2模块导入项目中 - 根目录下的build-profile.json5添加配置如下: ```shell { "name": "library", "srcPath": "./library" }, { "name": "libraryHttp2", "srcPath": "./libraryHttp2", } ``` - entry模块下oh-package.json5添加配置如下: ```shell "dependencies": { "@ohos/grpc": "file:../library", "@ohos/http2": "file:../libraryHttp2", "google-protobuf": "^3.21.2" } ``` ### 替换protobufjs的index.d.ts ```shell 把当前工程下的文件protobufjs/index.d.ts替换路径library/oh_modules/@ohos/protobufjs/src/main/ets/index.d.ts ``` ### proto生成ts - [下载安装配置protocolbuffers环境变量:](https://github.com/protocolbuffers/protobuf/releases/tag/v29.3) 解压之后把bin路径添加环境变量path中。 - [npm install -g protoc-gen-ts](https://github.com/thesayyn/protoc-gen-ts) - 生成命令:protoc -I=./proto --ts_out=./out ./proto/hello.proto - 生成之后需要修改生成文件如下: ```shell import * as grpc_1 from "@grpc/grpc-js" 改成 import { grpc as grpc_1 } from "@ohos/grpc" import buffer from '@ohos.buffer'; * 注意在服务端响应反序列化消息中(deserialize),可参考entry中的helloworld.ts,修改如下: switch (reader.getFieldNumber()) { case 1: message.message = reader.readString(); break; default: reader.skipField(); } 改成 if (reader.getFieldNumber() >= 0) { message.message = buffer.from(reader.getBuffer()).toString(); break; } * 另外在请求接口中的Buffer参数需要改成ArrayBufferLike | Uint8Array * Buffer.from改成buffer.from ``` ### 调用接口 ```shell import { grpc } from '@ohos/grpc' import { helloworld} from '../helloworld/helloworld' const rootCert = getContext().resourceManager.getRawFileContentSync("xxxxx.crt") let client = new helloworld.GreeterClient('192.168.25.220:50051',grpc.credentials.createSsl(buffer.from(rootCert))) let request = new helloworld.HelloRequest(["hello world"]); client.SayHello(request, (err, response) => { console.log('SayHello request:', request); if (err) { console.log('SayHello err:', err); } console.log('SayHello response:', response); }) ``` ## 约束与限制 在下述版本中验证通过: DevEco Studio 5.0.1 Release ## 目录结构 ```javascript |---- ohos_grpc_node | |---- entry # 示例代码文件夹 | |---- library # grpc模块 | |---- libraryHttp2 # http2模块 | |---- README.md # 安装使用方法 ``` ## 贡献代码 使用过程中发现任何问题都可以提[Issue](https://gitee.com/openharmony-sig/ohos_grpc_node/issues) 给组件,当然,也非常欢迎发[PR](https://gitee.com/openharmony-sig/ohos_grpc_node/pulls)共建。 ## 开源协议 本项目基于 [Apache License 2.0](LICENSE),请自由地享受和参与开源。