【问题标题】:How to implement com.google.protobuf.BlockingRpcChannel for gRPC generated Java code?如何为 gRPC 生成的 Java 代码实现 com.google.protobuf.BlockingRpcChannel?
【发布时间】:2020-06-22 05:46:29
【问题描述】:

我最近获取了一个代表 gRPC 服务的 proto 文件并从中生成了 Java 代码。但是,所有接口都需要一个 com.google.protobuf.BlockingRpcChannel,我不知道如何创建它。当我查看示例时,我看到人们使用 io.grpc.ManagedChannel 但那是因为生成的 Java 代码使用了该类型。我不确定是因为我使用的是特定版本的 protobuf 还是什么?

 public static BlockingInterface newBlockingStub(com.google.protobuf.BlockingRpcChannel channel) 
 {
   return new BlockingStub(channel);
 }

以下是我所看到的示例 https://www.programcreek.com/java-api-examples/?api=io.grpc.ManagedChannel

在教程中建议执行以下操作,但接口不对齐。我有一个 BlockRpcChannel,在示例中他们可以使用 ManagedChannel。生成的 java 代码不会不接受 ManagedChannel [![https://grpc.io/docs/tutorials/basic/java/][1]][1]

在我的原型中,我使用以下导入。不确定这是否会影响一代

syntax = "proto3";
import "google/api/annotations.proto";
import "google/protobuf/any.proto";

目标:我想弄清楚如何创建一个可以利用 java 生成的 gRPC 代码的客户端。

下面是我用来生成proto的build.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.2.2/userguide/java_library_plugin.html
 */

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
    id 'com.google.protobuf' version '0.8.8'
    id 'idea'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    mavenLocal()
}

def grpcVersion = '1.27.1' // CURRENT_GRPC_VERSION
def protobufVersion = '3.11.0'
def protocVersion = protobufVersion

dependencies {
    compile("io.grpc:grpc-netty:" + grpcVersion)
    compile("io.grpc:grpc-protobuf:" + grpcVersion)
    compile("com.google.protobuf:protobuf-java:3.11.0")
    compile("com.google.protobuf:protobuf-java-util:3.11.0")
    compile("io.grpc:grpc-stub:" + grpcVersion)
    compile("io.envoyproxy.protoc-gen-validate:protoc-gen-validate:0.3.0")
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${protocVersion}"
    }

    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }

    generatedFilesBaseDir = "$projectDir/src"

    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

sourceSets {
    main {
        proto {
            // In addition to the default "src/main/proto"
            srcDir "proto"
            srcDir "src/main/grpc"
        }
    }
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = "sources"
    from sourceSets.main.allSource
}

任何帮助表示赞赏? 谢谢, 德里克

【问题讨论】:

  • 如何从 proto 文件生成 Java 代码?
  • @AnarSultanov 我创建了一个使用 gradle 插件生成原型的 java 项目。我已经附加了 build.gradle。

标签: grpc-java protocol-buffers


【解决方案1】:

@creamsoup 是正确的。该选项是触发问题并阻止生成 gPRC 的其他插件正常工作的原因。

谢谢!

【讨论】:

  • 请在您提供自己的回复时指明确切的解决方案。我不知道是什么选项触发了这个问题并阻止了插件。
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2021-04-24
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多