gRPC - 找不到方法

Posted

技术标签:

【中文标题】gRPC - 找不到方法【英文标题】:gRPC - Method not found 【发布时间】:2020-01-23 16:38:39 【问题描述】:

我有三个非常简单的微服务(Spring Boot),它们使用 gRPC 进行通信。我最初写了两个服务:一个客户端(GreeterClient)和一个服务(GreeterService)。客户端将请求消息传递给服务,服务将响应返回给客户端。我可以让这两个服务相互配合。

添加第三个服务后,现在的服务组织如下:

GreeterClient:将请求消息传递给 GreeterService,等待响应。

GreeterService:收到来自 GreeterClient 的请求后,调用 GreetingProviderService 进行“问候”,并期待响应。一旦收到响应,就会对 GreeterClient 做出响应并返回。

GreetingProviderService:收到来自 GreeterService 的调用后,返回响应。

每个微服务都包含在自己的项目中。第四个项目仅包含定义 RPC 服务和消息的 protobuf 文件。每个微服务项目都将此作为依赖项。

我遇到了 GreeterService 找不到 GreetingProviderService getGreeting“端点”的问题。 GreeterClient 找到 GreeterService 端点没有问题。不知道怎么回事。

下面是 GreeterService 控制台的一部分:

2020-01-23 10:52:49.671 DEBUG 5108 --- [-worker-ELG-1-3] io.grpc.netty.NettyClientHandler         : [id: 0x758853f0, L:/127.0.0.1:55380 - R:/127.0.0.1:9310] OUTBOUND HEADERS: streamId=3 headers=GrpcHttp2OutboundHeaders[:authority: 127.0.0.1:9310, :path: /edu.mit.ll.GreetingProvider/GetGreeting, :method: POST, :scheme: http, content-type: application/grpc, te: trailers, user-agent: grpc-java-netty/1.25.0, grpc-accept-encoding: gzip] streamDependency=0 weight=16 exclusive=false padding=0 endStream=false 2020-01-23 10:52:49.677 DEBUG 5108 --- [-worker-ELG-1-3] io.grpc.netty.NettyClientHandler         : [id: 0x758853f0, L:/127.0.0.1:55380 - R:/127.0.0.1:9310] OUTBOUND DATA: streamId=3 padding=0 endStream=true length=5 bytes=0000000000
2020-01-23 10:52:49.743 DEBUG 5108 --- [-worker-ELG-1-3] io.grpc.netty.NettyClientHandler         : [id: 0x758853f0, L:/127.0.0.1:55380 - R:/127.0.0.1:9310] INBOUND HEADERS: streamId=3 headers=GrpcHttp2ResponseHeaders[:status: 200, content-type: application/grpc, grpc-status: 12, grpc-message: Method not found: edu.mit.ll.GreetingProvider/GetGreeting] streamDependency=0 weight=16 exclusive=false padding=0 endStream=true
2020-01-23 10:52:49.751 DEBUG 5108 --- [-worker-ELG-1-3] io.grpc.netty.NettyClientHandler         : [id: 0x758853f0, L:/127.0.0.1:55380 - R:/127.0.0.1:9310] OUTBOUND RST_STREAM: streamId=3 errorCode=8
2020-01-23 10:52:49.752 ERROR 5108 --- [ault-executor-0] io.grpc.internal.SerializingExecutor     : Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed@1f8dda93

io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found: edu.mit.ll.GreetingProvider/GetGreeting
        at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:240)
        at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:221)
        at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:140)
        at edu.mit.ll.grpc.GreetingProviderGrpc$GreetingProviderBlockingStub.getGreeting(GreetingProviderGrpc.java:138)
        at edu.mit.ll.service.GreeterService.sayHello(GreeterService.java:46)
        at edu.mit.ll.grpc.GreeterGrpc$MethodHandlers.invoke(GreeterGrpc.java:217)
        at io.grpc.stub.ServerCalls$UnaryServerCallHandler$UnaryServerCallListener.onHalfClose(ServerCalls.java:172)
        at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.halfClosed(ServerCallImpl.java:331)
        at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1HalfClosed.runInContext(ServerImpl.java:814)
        at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
        at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

Protobuf 文件:

greeter.proto:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "edu.mit.ll.grpc";

package edu.mit.ll;

service Greeter 
  rpc SayHello (HelloRequest) returns (HelloReply) 


message HelloRequest 
  string name = 1;


message HelloReply 
  string message = 1;

provider.proto:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "edu.mit.ll.grpc";

import "google/protobuf/empty.proto";

package edu.mit.ll;

service GreetingProvider 
  rpc GetGreeting(google.protobuf.Empty) returns (Greeting) 


message Greeting 
  string greeting = 1;

GreeterClient.java:

@Service
public class GreeterClient 
  ...
  @PostConstruct
  public void init() 
    channel = ManagedChannelBuilder.forAddress("127.0.0.1", 9110).usePlaintext()
        .keepAliveWithoutCalls(true).build();
    greeterStub = GreeterGrpc.newBlockingStub(channel);

    taskExecutor.execute(new Runner());
  

  private class Runner implements Runnable 
    @Override
    public void run() 
        ...
        log.info("making request ...");
        HelloRequest request = HelloRequest.newBuilder().setName(name).build();
        HelloReply reply = greeterStub.sayHello(request);
        log.info("reply: ", reply.getMessage());
        ...
      
    
  

GreeterClient application.yml:

...
grpc:
  port: 9210

GreeterService.java:

@GRpcService
public class GreeterService extends GreeterImplBase 
  ...
  @PostConstruct
  public void init() 
    channel = ManagedChannelBuilder.forAddress("127.0.0.1", 9310).usePlaintext()
        .keepAliveWithoutCalls(true).build();
    providerStub = GreetingProviderGrpc.newBlockingStub(channel);
  

  @Override
  public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) 
    Greeting greeting = providerStub.getGreeting(Empty.newBuilder().build());
    HelloReply reply = HelloReply.newBuilder().setMessage(greeting + " " + request.getName())
        .build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
  

GreeterService application.yml:

...
grpc:
  port: 9110

GreetingProviderService.java:

public class GreetingProviderService extends GreetingProviderImplBase 
  private static final String[] GREETINGS =  ... ;
  ...
  @Override
  public void getGreeting(Empty request, StreamObserver<Greeting> responseObserver) 
    int index = random.nextInt(GREETINGS.length);
    Greeting greeting = Greeting.newBuilder().setGreeting(GREETINGS[index]).build();
    responseObserver.onNext(greeting);
    responseObserver.onCompleted();
  

GreetingProviderService 应用程序.yml:

...
grpc:
  port: 9310

【问题讨论】:

【参考方案1】:

[捂脸]

它总是简单的东西。我忽略了将 @GRpcService 注释添加到 GreetingProviderService。

添加后,问题就解决了。

【讨论】:

以上是关于gRPC - 找不到方法的主要内容,如果未能解决你的问题,请参考以下文章

构建到 IOS 的颤振时找不到 BoringSSL-GRPC.modulemap

Xcode gRPC-Core 错误:找不到“openssl_grpc/ssl.h”文件

找不到模块映射文件'/Users/"my_flutter_project_path"/ios/Pods/Headers/Private/openssl_grpc/BoringSSL

java运行程序找不到main方法

找不到与Java版本匹配的alpn-boot JAR:12.0.2

找不到引用的类/找不到引用的方法