我想将 spring boot 与 protobuf 一起使用。简而言之,我编写了具有以下结构的演示代码;
RestController->获取实体->Postgres DB Repo->实体到protobuf对象->返回protobuf对象
pom 原型依赖;
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.12.4</version>
</dependency>
<dependency>
<groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.13.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
原型文件
syntax = "proto3";
package demo;
option java_package = "demo.model";
option java_outer_classname = "DemoProtos";
message DemoDto {
int64 id = 1;
string description = 2;
}
Run Code Online (Sandbox Code Playgroud)
gen.sh 文件
#!/usr/bin/env bash
SRC_DIR=`pwd`
DST_DIR=`pwd`/../src/main/
echo source: $SRC_DIR
echo destination root: $DST_DIR
function gen(){
D=$1
echo $D
OUT=$DST_DIR/$D
mkdir -p $OUT
sudo protoc -I=$SRC_DIR --${D}_out=$OUT $SRC_DIR/demo.proto
} …Run Code Online (Sandbox Code Playgroud) 使用Grpc双向流,当我尝试运行grpc时,低于错误
Connection Error
io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: at io.netty.handler.codec.http2.Http2Exception.connectionError(Http2Exception.java:82)
at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.readClientPrefaceString(Http2ConnectionHandler.java:322)
at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.decode(Http2ConnectionHandler.java:263)
at io.netty.handler.codec.http2.Http2ConnectionHandler.decode(Http2ConnectionHandler.java:445)
at io.netty.handler.codec.ByteToMessageDecoder.decodeLast(ByteToMessageDecoder.java:382)
at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:286)
at io.netty.handler.codec.http2.Http2ConnectionHandler.channelInactive(Http2ConnectionHandler.java:421)
at io.grpc.netty.NettyServerHandler.channelInactive(NettyServerHandler.java:227)
at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelInactiveNow(ChannelHandlerInvokerUtil.java:56)
at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelInactive(DefaultChannelHandlerInvoker.java:92)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:135)
at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:928)
at io.netty.channel.AbstractChannel$AbstractUnsafe$7.run(AbstractChannel.java:674)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:339)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:356)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?
我的问题与this问题非常相似,但是针对maven和java。
我正在测试 grpc,并想将一个简单的 helloworld.proto 放入 test/proto 文件夹中。
但是该文件不会生成 java 文件(与 /src/main/proto 中的 proto 文件不同)。
所以我的问题是如何在测试文件夹中生成 proto 代码?
在 io.grpc.internal.AbstractServerImplBuilder 中,您可以将 ServerInterceptor 的实现添加到最终列表中
final List<ServerInterceptor> interceptors = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
然后在构建器的默认服务器实现中 - io.grpc.internal.ServerImpl 它在 foreach 循环中调用它们
/** Never returns {@code null}. */
private <ReqT, RespT> ServerStreamListener startCall(ServerStream stream, String fullMethodName,
ServerMethodDefinition<ReqT, RespT> methodDef, Metadata headers,
Context.CancellableContext context, StatsTraceContext statsTraceCtx, Tag tag) {
// TODO(ejona86): should we update fullMethodName to have the canonical path of the method?
statsTraceCtx.serverCallStarted(
new ServerCallInfoImpl<>(
methodDef.getMethodDescriptor(), // notify with original method descriptor
stream.getAttributes(),
stream.getAuthority()));
ServerCallHandler<ReqT, RespT> handler = methodDef.getServerCallHandler();
for (ServerInterceptor interceptor : interceptors) { …Run Code Online (Sandbox Code Playgroud)