gRPC - 找不到方法

gRPC - Method not found

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

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

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

GreeterService:收到来自 GreeterClient 的请求后,为 "greeting" 调用 GreetingProviderService,等待响应。收到响应后,将对 GreeterClient 做出响应并返回。

GreetingProviderService:收到 GreeterService 的调用后,returns 响应。

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

我遇到了一个问题,GreeterService 找不到 GreetingProviderService getGreeting "endpoint"。 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$JumpToApplicationThreadServerStreamListenerHalfClosed@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$JumpToApplicationThreadServerStreamListenerHalfClosed.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();
  }
}

迎宾服务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();
  }
}

GreetingProviderServiceapplication.yml:

...
grpc:
  port: 9310

[捂脸]

事情总是那么简单。我忽略了向 GreetingProviderService 添加 @GRpcService 注释。

添加后,问题解决。