我尝试执行gRPC服务的HTTP/1.x请求,但出现以下错误。
io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2Exception: Unexpected HTTP/1.x request: GET /author/1
at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2Exception.connectionError(Http2Exception.java:109) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.readClientPrefaceString(Http2ConnectionHandler.java:317) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.decode(Http2ConnectionHandler.java:247) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2ConnectionHandler.decode(Http2ConnectionHandler.java:453) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:449) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[grpc-netty-shaded-1.51.0.jar:1.51.0]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
我的.proto文件如下。
syntax = "proto3";
package com.myapp.grpc_server.proto;
option java_multiple_files = true;
import "google/api/annotations.proto";
message Book {
int32 book_id = 1;
string title = 2;
float book_price = 3;
int32 pages = 4;
int32 author_id = 5;
}
message Author {
int32 author_id = 1;
string first_name = 2;
string last_name = 3;
string gender = 4;
int32 book_id = 5;
}
service BookAuthorService {
//unary - synchronous - client will send one request and one response will be received.
rpc getAuthor(Author) returns (Author) {
option (google.api.http) = {
get: "/author/{id}"
};
}
}
My proto directory containing my .proto file along with annotations.proto and http.proto files copied from googleapis github.com/googleapis/googleapis
generated sources after compiling
GrpcService类
package com.myapp.grpcserver.service;
import com.myapp.grpc_server.proto.Author;
import com.myapp.grpc_server.proto.Book;
import com.myapp.grpc_server.proto.BookAuthorServiceGrpc;
import com.myapp.grpcserver.setup.TempDB;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
public class BookAuthorService extends BookAuthorServiceGrpc.BookAuthorServiceImplBase {
@Override
public void getAuthor(Author request, StreamObserver<Author> responseObserver) {
TempDB.authors.stream()
.filter(author -> author.getAuthorId() == request.getAuthorId())
.findFirst()
.ifPresent(responseObserver::onNext);
responseObserver.onCompleted();
}
}
我在定义的端点上使用浏览器发出GET请求,但出现了上述错误。请建议我错在哪里以及我需要做什么。请分享任何Java中的工作示例,因为grpc-gateway的官方文档在Go中。
1条答案
按热度按时间jhiyze9q1#
Java gRPC不支持任何从http 1转换到http 2的概念,并且您不能通过使用grpc-gateway将其添加到它。您可以使用grpc-gateway创建一个单独的代理来处理此转换,但它将作为自己的单独进程运行,独立于Java应用程序。
你可能已经采用了一些Go特定的指令,这些指令在gRPC服务器中嵌入了grpc-gateway,但这只适用于Go实现。我建议你再看看这些指令,然后构建你的独立网关。