gpt4 book ai didi

java - io.grpc.StatusRuntimeException : UNIMPLEMENTED: Method not found

转载 作者:行者123 更新时间:2023-11-30 05:38:20 40 4
gpt4 key购买 nike

使用 Springboot 入门应用程序。一切正常。然后我将客户端和服务器移动到新的包中。一切都会编译,但在运行时,当我运行客户端时,我收到错误:未找到未实现的方法。

我检查了该方法确实已实现。当我启动 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);


}

}
}

最佳答案

@SpringBootApplication javadoc状态:

This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

还有@ComponentScan javadoc状态:

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

这意味着默认情况下 @SpringBootApplication 注释会扫描同一包或以下包中的所有类(请参阅 the best practices 有关在 Spring Boot 应用程序中构造代码的信息)。

但是,如果您不想移动类,则可以使用主类上的 @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);
}
}

关于java - io.grpc.StatusRuntimeException : UNIMPLEMENTED: Method not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195738/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com