grpc客户端:nodejs实现
grpc服务端:java实现
客户端报错如下:
Error receiving telemetry: Error: 12 UNIMPLEMENTED: Method not found: com.widgets.grpc.api.DeviceService/telemetry
at Object.exports.createStatusError (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\common.js:91:15)
at ClientReadableStream._emitStatusIfDone (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client.js:233:26)
at ClientReadableStream._receiveStatus (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client.js:211:8)
at Object.onReceiveStatus (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client_interceptors.js:1277:15)
at InterceptingListener._callNext (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client_interceptors.js:618:8)
at E:\gitee\sight-front-end\sight-front-end\node_modules\grpc\src\client_interceptors.js:1033:24 {
code: 12,
metadata: Metadata { _internal_repr: {}, flags: 0 },
details: 'Method not found: com.widgets.grpc.api.DeviceService/telemetry'
}
修改前:
# nodejs端proto文件
syntax = "proto3";
package com.widgets.grpc.api;
message TelemetryRequest {
string msg = 1;
}
message TelemetryResponse {
string result = 1;
}
service DeviceService {
rpc telemetry(TelemetryRequest) returns (stream TelemetryResponse){}
}
# java端proto文件
syntax = "proto3";
option java_multiple_files = false;
option java_package = "com.widgets.grpc.api";
option java_outer_classname = "DeviceProto";
message TelemetryRequest {
string msg = 1;
}
message TelemetryResponse {
string result = 1;
}
service DeviceService {
rpc telemetry(TelemetryRequest) returns (stream TelemetryResponse){}
}
报错原因:两边的package不一致,java端proto文件中没有指定package
修改后:
# nodejs端proto文件
syntax = "proto3";
package com.widgets.grpc.api;
message TelemetryRequest {
string msg = 1;
}
message TelemetryResponse {
string result = 1;
}
service DeviceService {
rpc telemetry(TelemetryRequest) returns (stream TelemetryResponse){}
}
# java端proto文件
syntax = "proto3";
option java_multiple_files = false;
option java_package = "com.widgets.grpc.api";
option java_outer_classname = "DeviceProto";
package com.widgets.grpc.api;
message TelemetryRequest {
string msg = 1;
}
message TelemetryResponse {
string result = 1;
}
service DeviceService {
rpc telemetry(TelemetryRequest) returns (stream TelemetryResponse){}
}