问题描述
我最近获取了一个表示gRPC服务的原型文件,并从中生成了Java代码.但是,所有接口都需要com.google.protobuf.BlockingRpcChannel,而且我对如何创建它一无所知.在查看示例时,我看到人们在使用io.grpc.ManagedChannel,但这是因为生成的Java代码使用了该类型.我不确定是否是因为我使用的是Protobuf的特定版本还是什么?
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);
}
以下是我所见过的例子 https://www.programcreek.com/java-api -examples/?api = io.grpc.ManagedChannel
Here are examples of what i've seen https://www.programcreek.com/java-api-examples/?api=io.grpc.ManagedChannel
在本教程中,建议执行以下操作,但是接口不对齐.我有一个BlockRpcChannel,在示例中,他们能够使用ManagedChannel.生成的Java代码不会接受ManagedChannel[![ https://grpc.io/docs/tutorials/basic/java/] [1]] [1]
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";
目标:我想弄清楚如何创建一个可以利用Java为gRPC生成的代码的客户端.
Goal: I want to figure out how to create a client that can utilize the java generated code for the gRPC.
下面是我用来生成原型的build.gradle
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
推荐答案
@creamsoup是正确的.该选项是引发问题并阻止生成gPRC的其他插件正常工作的原因.
@creamsoup was correct. The option is what triggered the issue and prevented the other plugin that generated the gPRC to work properly.
谢谢!
这篇关于如何为gRPC生成的Java代码实现com.google.protobuf.BlockingRpcChannel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!