docker - Dockerizing gRPC 服务器(节点)问题:[错误:无法加载 gRPC 二进制模块,因为它不是为当前系统安装的]
问题描述
每当我运行我的 docker 映像时,我都会收到关于节点的系统/架构之间不兼容的错误...
我试过这个命令:
npm rebuild --target=8.1.0 --target_platform=linux --target_arch=x64 --target_libc=glibc --update-binary
但是问题依然存在...
这是我的文件以获取更多上下文:
完全错误在我尝试运行我的 docker 映像后
Error: Failed to load gRPC binary module because it was not installed for the current system
Expected directory: node-v83-linux-x64-glibc
Found: [node-v72-darwin-x64-unknown]
This problem can often be fixed by running "npm rebuild" on the current system
Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v83-linux-x64-glibc/grpc_node.node'
Require stack:
- /usr/src/app/node_modules/grpc/src/grpc_extension.js
- /usr/src/app/node_modules/grpc/src/client_interceptors.js
- /usr/src/app/node_modules/grpc/src/client.js
- /usr/src/app/node_modules/grpc/index.js
- /usr/src/app/booksServer.js
at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/grpc_extension.js:53:17)
at Module._compile (internal/modules/cjs/loader.js:1236:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10)
at Module.load (internal/modules/cjs/loader.js:1085:32)
at Function.Module._load (internal/modules/cjs/loader.js:950:14)
at Module.require (internal/modules/cjs/loader.js:1125:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client_interceptors.js:144:12)
at Module._compile (internal/modules/cjs/loader.js:1236:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1257:10) {
code: 'MODULE_NOT_FOUND'
Dockerfile(运行我的 gRPC 节点服务器)
FROM node:latest
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
EXPOSE 30043
CMD ["node", "booksServer.js"]
Booksserver.js (GRPC 服务器)
// const PROTO_PATH = "../protos/books.proto";
const path = require('path');
const PROTO_PATH = path.join(__dirname, '../protos/books.proto');
const grpc = require("grpc");
const protoLoader = require("@grpc/proto-loader");
const express = require("express");
const controller = require("./booksController.js");
const app = express();
app.use(express.json());
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
arrays: true,
});
const booksProto = grpc.loadPackageDefinition(packageDefinition);
const { v4: uuidv4 } = require("uuid");
const server = new grpc.Server();
server.addService(booksProto.BooksService.service, {
CreateBook: (call, callback) => {
console.log("call to CreateBook");
//sample will take the call information from the client(stub)
const book = {
title: call.request.title,
author: call.request.author,
numberOfPages: call.request.numberOfPages,
publisher: call.request.publisher,
id: call.request.id,
};
controller.createBook(book);
let meta = new grpc.Metadata();
meta.add("response", "none");
call.sendMetadata(meta);
callback(
null,
//bookmodel.create
{
title: `completed for: ${call.request.title}`,
author: `completed for: ${call.request.author}`,
numberOfPages: `completed for: ${call.request.numberOfPages}`,
publisher: `completed for: ${call.request.publisher}`,
id: `completed for: ${call.request.id}`,
}
);
},
GetBooks: (call, callback) => {
console.log("call to GetBooks");
// read from database
let meta = new grpc.Metadata();
meta.add('response', 'none');
call.sendMetadata(meta);
controller.getBooks(callback);
},
GetBookByID: (call, callback) => {
console.log("call to GetBookByID");
let sampleID = {id: 100};
let meta = new grpc.Metadata();
meta.add('response', 'none');
call.sendMetadata(meta);
controller.getBookByID(sampleID, callback);
},
DeleteBook: (call, callback) => {
//sample will take the call information from the client(stub)
const bookID = {
id: call.request.id,
};
//this actually sends data to booksController.
controller.deleteBook(bookID);
let meta = new grpc.Metadata();
meta.add('response', 'none');
call.sendMetadata(meta);
// delete from database
callback(null, { message: "DELETED" });
},
});
server.bind("127.0.0.1:30043", grpc.ServerCredentials.createInsecure());
console.log("booksServer.js running at http://127.0.0.1:30043");
console.log("call from books server");
server.start();
解决方案
推荐阅读
- r - 根据 Rmarkdown 中 YAML 标头中的参数更改 preamble.tex 的内容?
- ruby-on-rails - Sendgrid 未在 Heroku 生产 Rails 应用程序上发送电子邮件
- node.js - 无法使用 npm 安装 zerorpc
- javascript - React 中的表单问题:受控到不受控
- c# - Azure Blob 存储帐户返回错误 (403) 已禁止
- javascript - 使用 vanilla javascript 在表格中显示带有图像的嵌套 json
- selenium - 通过 Selenium 显式等待
- laravel-5 - 如何在laravel中获取日期之间的所有记录?
- json - 如何从存储在表中的嵌套 JSON 对象返回多行
- android - 在 android canvas 中使用 RectF 创建一行 3 个圆(分段)