io.grpc.StatusRuntimeException:已删除:未找到方法

时间:2019-05-18 03:27:56

标签: java spring spring-boot grpc grpc-java

使用Springboot入门应用程序。一切正常。然后,我将客户端和服务器移至新软件包。一切都编译了,但是在运行客户端时,运行时出现错误:找不到UNIMPLEMENTED方法。

我检查了该方法是否已实施。当我启动Springboot应用程序时,如何确认Bean本身已加载?我只是看到确认该应用程序已启动并正在运行,但日志中没有加载的bean列表。

这是我的Springboot应用程序。我所做的就是将客户端和服务器Bean移到名为example.client和example.server的新程序包中。在与Spring Boot com.test.MyApplication

放在同一软件包中之前
io.grpc.StatusRuntimeException: UNIMPLEMENTED: Method not found:  example.GreetingService/greetingWithResponseStream
at io.grpc.Status.asRuntimeException(Status.java:526)
at  io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:434)
at  io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)



package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
}
}

GreetingService.proto:

syntax = "proto3";
package example;

message HelloRequest {
    string name = 1;
}


message HelloResponse {
    string greeting = 1;
}


service GreetingService {
    rpc greeting (HelloRequest) returns (HelloResponse);
    rpc greetingWithResponseStream (HelloRequest) returns (stream  HelloResponse);
  rpc greetingWithRequestStream (stream HelloRequest) returns  (HelloResponse);
rpc greetingWithRequestResponseStream (stream HelloRequest) returns  (stream HelloResponse);

}

import example.GreetingServiceOuterClass;

import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.List;

@GrpcService
public class GreetingServiceImpl extends    GreetingServiceGrpc.GreetingServiceImplBase{

@Override
public void greeting(GreetingServiceOuterClass.HelloRequest request,   StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
    GreetingServiceOuterClass.HelloResponse response =    GreetingServiceOuterClass.HelloResponse.newBuilder()
            .setGreeting("HELLO, THERE, " + request.getName())
            .build();
    responseObserver.onNext(response);
    responseObserver.onCompleted();

}

@Override
public void  greetingWithResponseStream(GreetingServiceOuterClass.HelloRequest request, StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
    GreetingServiceOuterClass.HelloResponse response =  GreetingServiceOuterClass.HelloResponse.newBuilder()
            .setGreeting("(Stream Response) Hello there, " + request.getName())
            .build();
    responseObserver.onNext(response);
    responseObserver.onNext(response);
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}

@Override
public StreamObserver<GreetingServiceOuterClass.HelloRequest>  greetingWithRequestStream(StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
    return new StreamObserver<GreetingServiceOuterClass.HelloRequest>() {
        private List<String> nameList = new ArrayList<>();

        @Override
        public void onNext(GreetingServiceOuterClass.HelloRequest request) {
            nameList.add(request.getName());
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onCompleted() {
            GreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder()
                    .setGreeting("(Stream Request) Hello there, " + String.join(" ", nameList))
                    .build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    };
}

@Override
public StreamObserver<GreetingServiceOuterClass.HelloRequest> greetingWithRequestResponseStream(StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {
    return new StreamObserver<GreetingServiceOuterClass.HelloRequest>() {
        private List<String> nameList = new ArrayList<>();

        @Override
        public void onNext(GreetingServiceOuterClass.HelloRequest request) {
            nameList.add(request.getName());
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onCompleted() {
            nameList.stream()
                    .map(name ->  GreetingServiceOuterClass.HelloResponse.newBuilder().setGreeting("(Stream Request/Response) Hello there, " + name).build())
                    .forEach(responseObserver::onNext);
            responseObserver.onCompleted();
        }
    };
}
}


package example.client;

import example.GreetingServiceGrpc;
import example.GreetingServiceOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.stream.Stream;

public class Client {
public static void main(String[] args) {
    ManagedChannel channel =   ManagedChannelBuilder.forTarget("localhost:8081")
            .usePlaintext()
            .build();

        GreetingServiceGrpc.GreetingServiceBlockingStub stub =    GreetingServiceGrpc.newBlockingStub(channel);
    GreetingServiceOuterClass.HelloRequest request = GreetingServiceOuterClass.HelloRequest.newBuilder().setName("Steve").build();
    GreetingServiceOuterClass.HelloResponse response = stub.greeting(request);
    System.out.println(response);
}


public static class RequestStreamClient {


    public static void main(String[] args) throws InterruptedException  {

        new Thread(() -> {

            ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8081")
                    .usePlaintext()
                    .build();

            GreetingServiceGrpc.GreetingServiceStub stub = GreetingServiceGrpc.newStub(channel);

            StreamObserver<GreetingServiceOuterClass.HelloRequest> requestStream =
                    stub.greetingWithRequestStream(new StreamObserver<GreetingServiceOuterClass.HelloResponse>() {
                        @Override
                        public void onNext(GreetingServiceOuterClass.HelloResponse response) {
                            System.out.println(response);
                        }

                        @Override
                        public void onError(Throwable t) {
                            t.printStackTrace();
                        }

                        @Override
                        public void onCompleted() {
                        }
                    });

            Stream.of("Steve1", "Steve2", "Steve3")
                    .map(name -> GreetingServiceOuterClass.HelloRequest.newBuilder().setName(name).build())
                    .forEach(requestStream::onNext);
            requestStream.onCompleted();



        }).start();

        Thread.sleep(10000);


    }

  }
}

1 个答案:

答案 0 :(得分:0)

@SpringBootApplication javadoc指出:

  

这是一个方便注释,等效于声明   @ Configuration,@ EnableAutoConfiguration和@ComponentScan。

@ComponentScan javadoc指出:

  

如果未定义特定程序包,则将从   声明此批注的类的包。

这意味着默认情况下,@ SpringBootApplication批注会扫描同一包或更低版本中的所有类(有关在Spring Boot应用程序中构建代码的信息,请参见the best practices)。

但是,如果您不想移动您的类,则可以使用主类上的@ComponentScan或@Import批注明确地导入它们:

@SpringBootApplication
@ComponentScan({"example.client","example.server"})
@Import(GreetingServiceImpl.class)
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}