gRPC Kotlin Codegen插件为Protobuf编译器生成的代码只包括一个类。

huangapple go评论59阅读模式
英文:

gRPC Kotlin Codegen Plugin for Protobuf Compiler it generates only one class

问题

我正在尝试在Kotlin中为我的gRPC客户端生成类,但它只生成了一个类。我正在按照这个教程链接进行操作。我已经在我的Ubuntu上安装了protoc。我将发布protoc-gen-grpc-kotlin.sh文件和我的proto文件。如果有人知道我哪里出错了,我将不胜感激。

我的protoc-gen-grpc-kotlin.sh文件:

#!/usr/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
java -jar $DIR/protoc-gen-grpc-kotlin-1.3.0-jdk8.jar $@

我正在使用protoc-gen-grpc-kotlin-1.3.0-jdk8.jar插件。

我的Java版本是17,但我也尝试了版本8,但仍然没有成功。

我的HelloService.proto文件:

syntax = "proto3";
package com.example.grpc;
option java_multiple_files = true;

message HelloRequest {
    string firstName = 1;
    string lastName = 2;
}

message HelloResponse {
    string greeting = 1;
}

service HelloService {
    rpc hello (HelloRequest) returns (HelloResponse) {}
}

protoc命令:

protoc --kotlin_out=. --java_out=. --plugin=protoc-gen-kotlin=protoc-gen-grpc-kotlin.sh --proto_path=. HelloService.proto

Java类通常会生成,但只创建了一个Kotlin服务。

谢谢!

英文:

I'm trying to generate classes for my grpc client in kotlin but it only generates one class I'm following this tutorial link I already have protoc installed on my ubuntu I will post how is the protoc-gen-grpc-kotlin.sh and my proto. If anyone knows where I'm going wrong, I'd appreciate it.

my protoc-gen-grpc-kotlin.sh:

#!/usr/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
java -jar $DIR/protoc-gen-grpc-kotlin-1.3.0-jdk8.jar $@

I am using protoc-gen-grpc-kotlin-1.3.0-jdk8.jar plugin

My java version is 17 but I tested it with version 8 and it didn't work either

My HelloService.proto:

syntax = "proto3";
package com.example.grpc;
option java_multiple_files = true;
//import "google/api/annotations.proto";

message HelloRequest {
    string firstName = 1;
    string lastName = 2;
}

message HelloResponse {
    string greeting =1;
}

service HelloService {
    rpc hello (HelloRequest) returns (HelloResponse) {}
//    rpc hello(HelloRequest) returns (HelloResponse){
//        option (google.api.http) = {
//            post: "/demo/item"
//            body: "*"
//        };
//    }
}

protoc command:

protoc --kotlin_out=. --java_out=. --plugin=protoc-gen-kotlin=protoc-gen-grpc-kotlin.sh --proto_path=. HelloService.proto

Java classes are normally generated but Create only one kotlin service

Thanks!

答案1

得分: 1

你还需要使用 java 插件 运行 protoc,以生成 Java 类。类似于:

# 生成 Java 消息存根
protoc --java_out=. --proto_path=. HelloService.proto

# 生成 Kotlin 消息存根
protoc --kotlin_out=. --proto_path=. HelloService.proto

# 生成 Java 服务存根
protoc --grpc-java_out=. --plugin=protoc-gen-grpc-java=protoc-gen-grpc-java.exe --proto_path=. HelloService.proto

# 生成 Kotlin 服务存根
protoc --grpc-kotlin_out=. --plugin=protoc-gen-grpc-kotlin=protoc-gen-grpc-kotlin.sh --proto_path=. HelloService.proto
英文:

You also need to run protoc with the java plugin to generate the Java classes. Something like:

# generate java message stubs
protoc --java_out=. --proto_path=. HelloService.proto

# generate kotlin message stubs
protoc --kotlin_out=. --proto_path=. HelloService.proto

# generate java service stubs
protoc --grpc-java_out=. --plugin=protoc-gen-grpc-java=protoc-gen-grpc-java.exe --proto_path=. HelloService.proto

# generate kotlin service stubs
protoc --grpc-kotlin_out=. --plugin=protoc-gen-grpc-kotlin=protoc-gen-grpc-kotlin.sh --proto_path=. HelloService.proto

答案2

得分: 0

这听起来好像你没有正确编译或生成Java proto和gRPC类,这些类对于在Kotlin中使用gRPC是必要的。你所展示的代码只应该生成一个Kotlin文件是正常和预期的。

(如果你正在寻找用于你的proto消息等的Kotlin DSL,那是protobuf的一个独立特性,而不是gRPC。)

英文:

It sounds like you're not correctly compiling or generating the Java proto and gRPC classes that are necessary to use gRPC for Kotlin. It is normal and expected that the code you've shown should only generates one Kotlin file.

(If you're looking for Kotlin DSLs for your proto messages etc., that's a separate feature of protobuf, not gRPC.)

huangapple
  • 本文由 发表于 2023年6月22日 07:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定