# ohos_grpc_node **Repository Path**: openharmony-sig/ohos_grpc_node ## Basic Information - **Project Name**: ohos_grpc_node - **Description**: grpc-js 是一个基于grpc协议的网络发包三方库 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://gitee.com/openharmony-sig/ohos_grpc_node - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 7 - **Created**: 2024-12-06 - **Last Updated**: 2025-05-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 🚨 **重要提示 | IMPORTANT** > > **⚠️ 此代码仓已归档。新地址请访问 [ohos_grpc_node](https://gitcode.com/openharmony-sig/ohos_grpc_node)。| ⚠️ This repository has been archived. For the new address, please visit [ohos_grpc_node](https://gitcode.com/openharmony-sig/ohos_grpc_node).** > --- > # ohos_grpc_node ## 简介 ohos_grpc_node是参考gRPC使用arkTs语言重构实现的一个高性能的远程过程调用(RPC)框架,旨在简化和优化分布式系统中的服务通信。 ## 使用说明 ### 下载源码 ```shell git clone https://gitee.com/openharmony-sig/ohos_grpc_node.git --recurse-submodules ``` ### 编译依赖库nghttp2、openssl_quic、json - [参考教程](https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/master/lycium) - [nghttp2脚本](https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/fixformat/thirdparty/nghttp2) - [openssl_quic脚本](https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/fixformat/thirdparty/openssl_quic) - [json脚本](https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/fixformat/thirdparty/json) - 拷贝生成的文件到thirdparty目录,如下图: ![img.png](img/img.png) - 删除json库中此行代码,路径:include\nlohmann\detail\output\serializer.hpp:513行 ```shell JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr)); ``` ### 替换protobufjs的index.d.ts ```shell 把当前工程下的文件protobufjs/index.d.ts替换路径library/oh_modules/@ohos/protobufjs/src/main/ets/index.d.ts ``` ### 安装grpc ```shell ohpm install @ohos/grpc ``` OpenHarmony ohpm 环境配置等更多内容,请参考[如何安装 OpenHarmony ohpm 包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) ### 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,修改如下: if (reader.isEndGroup()) { break; } 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模块 | |---- 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),请自由地享受和参与开源。