【问题标题】:gRPC-java - Compilation of a .proto filegRPC-java - .proto 文件的编译
【发布时间】:2018-01-07 00:41:32
【问题描述】:

我已经使用 protobuf 编译器编译了我的 .proto 文件并收到了一些 Java 文件。我收到了 .proto 文件中每个项目的 proto.java 文件和 .java 文件,包括消息类型和每个 RPC 调用,例如publicKeyRequest.java 和 Quote.java 作为 RPC 和请求参数类型。

这是所有需要的文件吗?因为我似乎仍然无法从服务器获得任何简单的响应?

我想为 PublicKeyRequest RPC 调用生成一个请求。我生成了请求对象,但我不知道如何通过通道实际发送它。

这是完整的 .proto 文件:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.decryptiondevice";
option java_outer_classname = "DecryptionDeviceProto";

package decryptiondevice;

service DecryptionDevice {

    // Decryption Request RPC
    //
    // Request contains ciphertext and proof
    // Returns the plaintext record
    rpc DecryptRecord(DecryptionRequest) returns (Record) {}

    // Get Signed Root Tree Hash RPC
    //
    // Caller provides a nonce
    // Returns a signed RTH and nonce
    rpc GetRootTreeHash(RootTreeHashRequest) returns (RootTreeHash) {}


    // Get Public key RPC
    //
    // Returns a Remote attestation report containing the public key as user        data
       rpc GetPublicKey(PublicKeyRequest) returns (Quote) {}
}


// Decryption Request
// - Byte array containing ciphertext
// - Proofs represented as JSON trees
message DecryptionRequest {
    bytes ciphertext        = 1;
    string proofOfPresence  = 2;
    string proofOfExtension = 3;
}


// A plaintext record
message Record {
    bytes plaintext = 1;
}


// RTH request contains
// - A random nonce
message RootTreeHashRequest {
    bytes nonce = 1;
}


// Root Tree Hash
// Random nonce used as message ID
// Signature over rth and nonce
message RootTreeHash {
    bytes rth   = 1;
    bytes nonce = 2;
    bytes sig   = 3;
}


// Public key request message
message PublicKeyRequest {
    bytes nonce = 1;
}


// Attestation Quote, containing the public key
message Quote {
    string quote = 1; //some format.. to be defined later
    //PEM formatted key
    bytes RSA_EncryptionKey = 2;
    bytes RSA_VerificationKey = 3;
}

这是我试图在客户端运行的代码:

public static void main(String[] args) {

    DeviceClient client = new DeviceClient("localhost", 50051);
    MannagedChanel channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext(true);

    ByteString nonce = ByteString.copyFromUtf8("someRandomString");
    PublicKeyRequest keyRequest = PublicKeyRequest.newBuilder().setNonce(nonce).build();

    // Here I want to send this to the server
    ByteString response = DecryptionDeviceProto.getKey(keyRequest, channel);//this line is not even close to being valid, but this is the sort thing I wish to achieve

    Sys.out.println(response);
}

抱歉,如果这是非常错误的,我是 gRPC 的新手。 关于这个系统的几点:

  • 已经用 Go 编写了一个客户端和服务器,该客户端和服务器已经过测试并且可以使用同一个 .proto 文件。
  • 我正在尝试用 Java 重写客户端以与同一服务器通信。

【问题讨论】:

  • 您应该完成 grpc.io 上的一个或两个 Java grpc 教程。一般来说,您希望使用从 .proto 文件生成的 Stub 类来调用服务器。

标签: java grpc grpc-java


【解决方案1】:

需要生成两组文件:Java Protobuf 和 Java gRPC。据我所知,对于除 Go 之外的所有语言,这是两个独立的生成步骤(可以组合成一个 protoc 调用,但它们在概念上是独立的)。

您似乎正在生成 Java Protobuf 代码,而不是 Java gRPC 代码。您需要使用protoc-gen-grpc-java 插件到protoc。如果您使用的是 Maven 或 Gradle,请阅读 grpc-java's README。如果是手动运行 protoc,可以下载一个pre-built binary from Maven Centralan answer to a similar question

【讨论】:

  • 谢谢,是的,我还没有生成 gRPC 代码。我试图编译和测试 grpc codegen 插件link,但不断收到错误,例如:无法为 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类型的对象获取未知属性“库”。我找不到有关如何使用预构建二进制文件的任何信息,任何指针?对不起,这个没用,谢谢
  • 请提供有关您正在执行的操作的更多详细信息,例如构建系统及其版本、您尝试的代码以及完整的错误详细信息。
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多