我在互联网上看到了这个示例,并且想知道,为什么您要使用流仅向回发1条消息?一元返回,不是更有效吗?
@Override
public void getFeature(Point request, StreamObserver<Feature> responseObserver) {
responseObserver.onNext(checkFeature(request));
responseObserver.onCompleted();
}
...
private Feature checkFeature(Point location) {
for (Feature feature : features) {
if (feature.getLocation().getLatitude() == location.getLatitude()
&& feature.getLocation().getLongitude() == location.getLongitude()) {
return feature;
}
}
// No feature was found, return an unnamed feature.
return Feature.newBuilder().setName("").setLocation(location).build();
}
答案 0 :(得分:0)
首先,这里有示例来说明流媒体概念。在这种特殊情况下,碰巧它最终只回发了一条消息。但是,需要针对最普遍的情况设计API,因为对于给定的位置,该API可能具有多个功能,并且由于某种原因,它们无法在一元响应中组合成一个数组。
答案 1 :(得分:0)
好吧,显然,我对Java和C#中gRPC的区别感到困惑。我曾经使用C#进行开发,但是在Java中,对于Unary gRPC,他们使用streamObserver对象进行响应。这很令人困惑...