How to implement com.google.protobuf.BlockingRpcChannel for gRPC generated Java code?

风格不统一 提交于 2020-03-25 21:59:29

问题


I recently took a proto file representing a gRPC service and generated Java code from it. However, all the interfaces expect a com.google.protobuf.BlockingRpcChannel and I don't have any idea on how to create it. When I look at example, I see people using io.grpc.ManagedChannel but that is because the generated Java code used that type instead. I'm not sure if it's because i'm using a specific version of protobuf or what?

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

Here are examples of what i've seen https://www.programcreek.com/java-api-examples/?api=io.grpc.ManagedChannel

In the tutorial it suggest to do the following, but the interfaces don't align. I have a BlockRpcChannel and in the example they are able to use a ManagedChannel. The generated java code won't doesn't accept a ManagedChannel [![https://grpc.io/docs/tutorials/basic/java/][1]][1]

In my proto, i'm using the following imports. Not sure if that is effecting the generation

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

Goal: I want to figure out how to create a client that can utilize the java generated code for the gRPC.

Below is my build.gradle used to generate the proto

/*
 * 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
}

Any help appreciated? Thanks, Derek


回答1:


@creamsoup was correct. The option is what triggered the issue and prevented the other plugin that generated the gPRC to work properly.

Thanks!



来源:https://stackoverflow.com/questions/60615761/how-to-implement-com-google-protobuf-blockingrpcchannel-for-grpc-generated-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!