0

我目前正在尝试从我创建的 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 对象。我试图通过在作为 id 的文本框中输入 1 来获取它的详细信息,但就像我在发送它时所说的那样,我在标题中得到了错误。这是我创建的原始文件:

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 客户端和节点服务器中有相同的 proto 文件,我不知道这是否重要。最后一个是当我运行我的服务器时我也得到这个:DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition我不知道这是否与它有关。

4

1 回答 1

0

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

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

于 2020-04-02T22:22:33.717 回答