RpcChannel 替代 io.grpc.Channel

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

RpcChannel instead of io.grpc.Channel

问题

我在使用gRPC Java protobuf代码生成时遇到了问题。当我使用protobuf-maven-plugin从我的.proto文件生成Java类时,它创建了已弃用的类,使用RpcChannel而不是io.grpc.Channel。我使用的protobuf-maven-plugin版本是0.6.1,protoc版本是3.23.2,grpc-java版本是1.55.1。

以下是我使用的版本:

Apache Maven 3.9.2
grpc-netty-shaded 1.55.1
grpc-protobuf 1.55.1
grpc-stub 1.55.1
protobuf-java 3.23.2
JDK 11.0.19
macOS 13.4(基于Apple Silicon架构)
我的.proto文件如下:

syntax = "proto3";
package agent;

option java_multiple_files = true;
option java_generic_services = true;
option java_package = "grpc";
option java_outer_classname = "AgentServerProto";
option objc_class_prefix = "Utm";

// 省略了服务和消息定义...

尽管设置了option java_generic_services = true;,生成的Java代码中不包含任何预期的服务实现基类,比如我的服务定义的PanelServiceImplBase。

即使在更新protoc版本并按照正确的安装步骤后,我仍然遇到了这个问题。

是否有人可以提供关于可能导致这个问题以及如何解决它的指导?提前感谢您。

当我尝试使用protobuf-maven-plugin生成gRPC Java类时,我最初遇到了这个问题。我的期望是生成的类是最新的,使用io.grpc.Channel而不是已弃用的RpcChannel。我还期望根据我的.proto文件生成服务实现基类。

我甚至尝试了不同版本的protobuf-maven-plugin、Java SDK,并尝试从Windows笔记本电脑生成代码。

英文:

I'm having issues with gRPC Java protobuf code generation. When I use the protobuf-maven-plugin to generate Java classes from my .proto files, it creates deprecated classes that use RpcChannel instead of io.grpc.Channel. I'm using protobuf-maven-plugin version 0.6.1, protoc version 3.23.2, and grpc-java version 1.55.1.

Here are the versions I'm using:

Apache Maven 3.9.2
grpc-netty-shaded 1.55.1
grpc-protobuf 1.55.1
grpc-stub 1.55.1
protobuf-java 3.23.2
JDK 11.0.19
macOS 13.4 on Apple Silicon architecture
My .proto file looks like this:

syntax = "proto3";
package agent;

option java_multiple_files = true;
option java_generic_services = true;
option java_package = "grpc";
option java_outer_classname = "AgentServerProto";
option objc_class_prefix = "Utm";

// Service and message definitions omitted for brevity...

Despite setting option java_generic_services = true;, the generated Java code does not contain any of the expected service implementation base classes, like PanelServiceImplBase for my service definition.

Even after updating the protoc version and following the appropriate steps for installation, I'm still encountering this issue.

Could someone provide guidance on what could be causing this issue and how to resolve it? Thank you in advance.

I initially encountered the problem when trying to generate gRPC Java classes using the protobuf-maven-plugin. My expectation was to have classes generated that are up-to-date, using io.grpc.Channel instead of the deprecated RpcChannel. I was also expecting to see the service implementation base classes generated based on my .proto file.

I even tried different versions of the protobuf-maven-plugin, java SDK, generate the code from a windows laptop

答案1

得分: 1

option java_generic_services = true 与 gRPC 无关。这就是为什么你得到了 RpcChannel。你可以移除它。

gRPC 代码生成与 protoc 分开,使用一个名为 protoc-gen-grpc-java 的自定义插件。要使用它,你需要为 protobuf-maven-plugincompile-custom 目标进行配置。

来自 grpc-java README 的示例配置:

<build>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>0.6.1</version>
      <configuration>
        <protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
        <!-- IMPORTANT: -->
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.1:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>compile-custom</goal> <!-- IMPORTANT -->
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
英文:

option java_generic_services = true is not related to gRPC. That's why you are getting RpcChannel. You can remove it.

The gRPC code generation is separate from protoc, using a custom plugin named protoc-gen-grpc-java. To use it you need the compile-custom goal for protobuf-maven-plugin.

From the grpc-java README:

<build>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>0.6.1</version>
      <configuration>
        <protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
        <!-- IMPORTANT: -->
        <pluginId>grpc-java</pluginId>
        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.1:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>compile-custom</goal> <!-- IMPORTANT -->
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

huangapple
  • 本文由 发表于 2023年6月5日 20:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406661.html
匿名

发表评论

匿名网友

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

确定