gpt4 book ai didi

java - 尝试在 java 和 Nodejs 中使用 GRPC 时出现警告 : RPC failed: Status{code=NOT_FOUND, 描述=未找到,原因=null}

转载 作者:行者123 更新时间:2023-12-01 17:39:00 27 4
gpt4 key购买 nike

我目前正在尝试从我创建的 java 客户端向 nodejs 服务器发送请求,但收到上面显示的错误。我一直在对此进行一些研究,但似乎可以弄清楚为什么会发生这种情况。我在nodejs中创建的服务器:

var grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader')
const packageDefinition = protoLoader.loadSync('AirConditioningDevice.proto')

var AirConditioningDeviceproto = grpc.loadPackageDefinition(packageDefinition);

var AirConditioningDevice = [{
device_id: 1,
name: 'Device1',
location: 'room1',
status: 'On',
new_tempature: 11
}];

var server = new grpc.Server();
server.addService(AirConditioningDeviceproto.AirConditioningDevice.Airconditioning_service.service,{
currentDetails: function(call, callback){
console.log(call.request.device_id);
for(var i =0; i <AirConditioningDevice.length; i++){
console.log(call.request.device_id);
if(AirConditioningDevice[i].device_id == call.request.device_id){
console.log(call.request.device_id);
return callback(null, AirConditioningDevice [i]);
}
console.log(call.request.device_id);
}
console.log(call.request.device_id);
callback({
code: grpc.status.NOT_FOUND,
details: 'Not found'
});
},
setTemp: function(call, callback){
for(var i =0; i <AirConditioningDevice.length; i++){
if(AirConditioningDevice[i].device_id == call.request.device_id){

AirConditioningDevice[i].new_tempature == call.request.new_tempature;
return callback(null, AirConditioningDevice[i]);
}
}
callback({
code: grpc.status.NOT_FOUND,
details: 'Not found'
});

},
setOff: function(call, callback){
for(var i =0; i <AirConditioningDevice.length; i++){
if(AirConditioningDevice[i].device_id == call.request.device_id && AirConditioningDevice[i].status == 'on'){
AirConditioningDevice[i].status == 'off';
return callback(null, AirConditioningDevice[i]);
}else{
AirConditioningDevice[i].status == 'on';
return callback(null, AirConditioningDevice[i]);
}
}
callback({
code: grpc.status.NOT_FOUND,
details: 'Not found'
});
}
});

server.bind('localhost:3000', grpc.ServerCredentials.createInsecure());
server.start();

这是我用 java 创建的客户端:

package com.air.grpc;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.air.grpc.Airconditioning_serviceGrpc;

import com.air.grpc.GrpcClient;
import com.air.grpc.deviceIDRequest;
import com.air.grpc.ACResponse;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;

public class GrpcClient {
private static final Logger logger = Logger.getLogger(GrpcClient.class.getName());
private final ManagedChannel channel;
private final Airconditioning_serviceGrpc.Airconditioning_serviceBlockingStub blockingStub;
private final Airconditioning_serviceGrpc.Airconditioning_serviceStub asyncStub;

public GrpcClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext()
.build());
}
GrpcClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = Airconditioning_serviceGrpc.newBlockingStub(channel);
asyncStub = Airconditioning_serviceGrpc.newStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

public void currentDetails(int id) {
logger.info("Will try to get device " + id + " ...");
deviceIDRequest deviceid = deviceIDRequest.newBuilder().setDeviceId(id).build();
ACResponse response;
try {
response =blockingStub.currentDetails(deviceid);

}catch(StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Device: " + response.getAirConditioning ());

}
public static void main(String[] args) throws Exception {
GrpcClient client = new GrpcClient("localhost", 3000);
try {
client.currentDetails(1);
}finally {
client.shutdown();
}



}




}

目前我测试过的唯一一个是因为它最基本的一个是 currentdetails。如您所见,我创建了一个 AirConditioningDevice 对象。我试图通过在文本框中输入 1 来获取它的详细信息,该文本框是 id,但就像我说的,当我发送它时,我在标题中收到错误。这是我创建的原型(prototype)文件:

syntax = "proto3";


package AirConditioningDevice;

option java_package = "AircondioningDevice.proto.ac";


service Airconditioning_service{
rpc currentDetails(deviceIDRequest) returns (ACResponse) {};
rpc setTemp( TempRequest ) returns (ACResponse) {};
rpc setOff(deviceIDRequest) returns (ACResponse) {};



}

message AirConditioning{
int32 device_id =1;
string name = 2;
string location = 3;
string status = 4;
int32 new_tempature = 5;
}

message deviceIDRequest{
int32 device_id =1;

}

message TempRequest {
int32 device_id = 1;
int32 new_temp = 2;
}

message ACResponse {
AirConditioning airConditioning = 1;
}

最后,这是我在控制台中返回的所有内容:

Apr 02, 2020 4:23:29 PM AircondioningDevice.proto.ac.AirConClient currentDetails
INFO: Will try to get device 1 ...
Apr 02, 2020 4:23:30 PM AircondioningDevice.proto.ac.AirConClient currentDetails
WARNING: RPC failed: Status{code=NOT_FOUND, description=Not found, cause=null}

不知道是我完全不对还是误差很小。有什么建议么?另一件事是我在 java 客户端和 Node 服务器中使用相同的 proto 文件,我不知道这是否重要。最后一点是,当我运行我的服务器时,我也得到了这个: DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition 相反 我不知道这是否有什么关系有了它。

最佳答案

在您的 .proto 文件中,您使用字段 device_id 声明 deviceIDRequest,但您正在检查 call.request.id currentDetails 处理程序中。如果直接查看call.request.id,它可能是undefined

您还没有达到这一点,但成功回调使用的是 books 数组而不是 AirConditioningDevice 数组。

关于java - 尝试在 java 和 Nodejs 中使用 GRPC 时出现警告 : RPC failed: Status{code=NOT_FOUND, 描述=未找到,原因=null},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60995185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com