我有三个非常简单的微服务(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 application.yml:

...
grpc:
  port: 9310

最佳答案

[facepalm]

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

一旦添加,问题就解决了。

09-26 23:12