gRPC-编译 .proto 文件



我已经使用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 重写客户端以与同一台服务器通信。

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

您似乎正在生成Java Protobuf代码,而不是Java gRPC代码。您需要使用protoc-gen-grpc-java插件来protoc.如果您使用的是 Maven 或 Gradle,请阅读 grpc-java 的自述文件。如果您手动运行 protoc,您可以从 Maven Central 下载预构建的二进制文件,并查看类似问题的答案。

最新更新