我正在尝试使用以下代码来创建存根:
ManagedChannel channel = channel = ManagedChannelBuilder.forAddress("localhost", 41800)
.usePlaintext()
.build();
ObjectDetectionService.Stub stub = ObjectDetectionService.newStub(channel); // fails
但是,
ObjectDetectionService.newStub(channel);
失败并出现以下错误:
error: incompatible types: ManagedChannel cannot be converted to RpcChannel
stub = ObjectDetectionService.newStub(channel);
这是从通道创建存根的正确方法吗?是否可以将 ManagedChannel 用作 RpcChannel?
这是生成的完整 gRPC 代码:
package org.photonvision.vision;
/**
* Protobuf service {@code org.photonvision.vision.ObjectDetectionService}
*/
public abstract class ObjectDetectionService
implements com.google.protobuf.Service {
protected ObjectDetectionService() {}
public interface Interface {
/**
* <code>rpc ObjectDetection(.org.photonvision.vision.ObjectDetectionRequest) returns (.org.photonvision.vision.ObjectDetectionResponse);</code>
*/
public abstract void objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request,
com.google.protobuf.RpcCallback<org.photonvision.vision.ObjectDetectionResponse> done);
}
public static com.google.protobuf.Service newReflectiveService(
final Interface impl) {
return new ObjectDetectionService() {
@java.lang.Override
public void objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request,
com.google.protobuf.RpcCallback<org.photonvision.vision.ObjectDetectionResponse> done) {
impl.objectDetection(controller, request, done);
}
};
}
public static com.google.protobuf.BlockingService
newReflectiveBlockingService(final BlockingInterface impl) {
return new com.google.protobuf.BlockingService() {
public final com.google.protobuf.Descriptors.ServiceDescriptor
getDescriptorForType() {
return getDescriptor();
}
public final com.google.protobuf.Message callBlockingMethod(
com.google.protobuf.Descriptors.MethodDescriptor method,
com.google.protobuf.RpcController controller,
com.google.protobuf.Message request)
throws com.google.protobuf.ServiceException {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.callBlockingMethod() given method descriptor for " +
"wrong service type.");
}
switch(method.getIndex()) {
case 0:
return impl.objectDetection(controller, (org.photonvision.vision.ObjectDetectionRequest)request);
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
public final com.google.protobuf.Message
getRequestPrototype(
com.google.protobuf.Descriptors.MethodDescriptor method) {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.getRequestPrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.getIndex()) {
case 0:
return org.photonvision.vision.ObjectDetectionRequest.getDefaultInstance();
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
public final com.google.protobuf.Message
getResponsePrototype(
com.google.protobuf.Descriptors.MethodDescriptor method) {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.getResponsePrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.getIndex()) {
case 0:
return org.photonvision.vision.ObjectDetectionResponse.getDefaultInstance();
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
};
}
/**
* <code>rpc ObjectDetection(.org.photonvision.vision.ObjectDetectionRequest) returns (.org.photonvision.vision.ObjectDetectionResponse);</code>
*/
public abstract void objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request,
com.google.protobuf.RpcCallback<org.photonvision.vision.ObjectDetectionResponse> done);
public static final
com.google.protobuf.Descriptors.ServiceDescriptor
getDescriptor() {
return org.photonvision.vision.PhotonvisionRemotePipeline.getDescriptor().getServices().get(0);
}
public final com.google.protobuf.Descriptors.ServiceDescriptor
getDescriptorForType() {
return getDescriptor();
}
public final void callMethod(
com.google.protobuf.Descriptors.MethodDescriptor method,
com.google.protobuf.RpcController controller,
com.google.protobuf.Message request,
com.google.protobuf.RpcCallback<
com.google.protobuf.Message> done) {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.callMethod() given method descriptor for wrong " +
"service type.");
}
switch(method.getIndex()) {
case 0:
this.objectDetection(controller, (org.photonvision.vision.ObjectDetectionRequest)request,
com.google.protobuf.RpcUtil.<org.photonvision.vision.ObjectDetectionResponse>specializeCallback(
done));
return;
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
public final com.google.protobuf.Message
getRequestPrototype(
com.google.protobuf.Descriptors.MethodDescriptor method) {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.getRequestPrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.getIndex()) {
case 0:
return org.photonvision.vision.ObjectDetectionRequest.getDefaultInstance();
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
public final com.google.protobuf.Message
getResponsePrototype(
com.google.protobuf.Descriptors.MethodDescriptor method) {
if (method.getService() != getDescriptor()) {
throw new java.lang.IllegalArgumentException(
"Service.getResponsePrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.getIndex()) {
case 0:
return org.photonvision.vision.ObjectDetectionResponse.getDefaultInstance();
default:
throw new java.lang.AssertionError("Can't get here.");
}
}
public static Stub newStub(
com.google.protobuf.RpcChannel channel) {
return new Stub(channel);
}
public static final class Stub extends org.photonvision.vision.ObjectDetectionService implements Interface {
private Stub(com.google.protobuf.RpcChannel channel) {
this.channel = channel;
}
private final com.google.protobuf.RpcChannel channel;
public com.google.protobuf.RpcChannel getChannel() {
return channel;
}
public void objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request,
com.google.protobuf.RpcCallback<org.photonvision.vision.ObjectDetectionResponse> done) {
channel.callMethod(
getDescriptor().getMethods().get(0),
controller,
request,
org.photonvision.vision.ObjectDetectionResponse.getDefaultInstance(),
com.google.protobuf.RpcUtil.generalizeCallback(
done,
org.photonvision.vision.ObjectDetectionResponse.class,
org.photonvision.vision.ObjectDetectionResponse.getDefaultInstance()));
}
}
public static BlockingInterface newBlockingStub(
com.google.protobuf.BlockingRpcChannel channel) {
return new BlockingStub(channel);
}
public interface BlockingInterface {
public org.photonvision.vision.ObjectDetectionResponse objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request)
throws com.google.protobuf.ServiceException;
}
private static final class BlockingStub implements BlockingInterface {
private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {
this.channel = channel;
}
private final com.google.protobuf.BlockingRpcChannel channel;
public org.photonvision.vision.ObjectDetectionResponse objectDetection(
com.google.protobuf.RpcController controller,
org.photonvision.vision.ObjectDetectionRequest request)
throws com.google.protobuf.ServiceException {
return (org.photonvision.vision.ObjectDetectionResponse) channel.callBlockingMethod(
getDescriptor().getMethods().get(0),
controller,
request,
org.photonvision.vision.ObjectDetectionResponse.getDefaultInstance());
}
}
// @@protoc_insertion_point(class_scope:org.photonvision.vision.ObjectDetectionService)
}```
(Somewhat related issue - ObjectDetectionService.BlockingStub is private, even though ObjectDetectionService.Stub is public. Supposedly this is intentional, but it does prevent me from instantiating a BlockingStub. Even if it was public, BlockingStub has the same issue described above.)
那是不是 gRPC 生成的代码。这是旧的 Protobuf RPC API,Protobuf 不鼓励使用。
您需要使用 protoc-gen-grpc-java 构建 gRPC 生成的代码。生成的类名将是带有“Grpc”后缀的服务名称。