Joseph Gagnon
Joseph Gagnon

Reputation: 2135

gRPC - Method not found

I have three really simple microservices (Spring Boot) that use gRPC to communicate. I originally wrote two services: a client (GreeterClient) and a service (GreeterService). The client passes a request message to the service, which returns a response to the client. I am able to have these two services work with each other.

After the addition of the third service, the service organization is now as follows:

GreeterClient: passes a request message to GreeterService, expecting a response.

GreeterService: upon receipt of request from GreeterClient, invokes GreetingProviderService for a "greeting", expecting a response. Once the response is received, a response to GreeterClient is made and returned.

GreetingProviderService: upon receipt of call from GreeterService, returns a response.

Each microservice is contained within its own project. There is a fourth project that contains just the protobuf files defining the RPC services and messages. Each microservice project references this as a dependency.

I'm having a problem where the GreeterService cannot find the GreetingProviderService getGreeting "endpoint". The GreeterClient has no problems finding the GreeterService endpoint. I don't know what's wrong.

Below is a portion of the console from the 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 files:

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 application.yml:

...
grpc:
  port: 9310

Upvotes: 2

Views: 7592

Answers (1)

Joseph Gagnon
Joseph Gagnon

Reputation: 2135

[facepalm]

It's always the simple stuff. I had neglected to add the @GRpcService annotation to the GreetingProviderService.

Once that was added, problem resolved.

Upvotes: 1

Related Questions