我正在使用Envoys http-grpc网桥通过HTTP发送application / grpc类型的数据,并调用grpc服务。这就是Lyft内部在其API中使用btw的方式。它必须采用以下格式:
1个字节:grpc数据,grpc数据的0、4字节网络/二进制长度。
这是我的代码:
private static void grpcPostRESTAPI() throws Exception
{
DefaultHttpClient httpClient = new DefaultHttpClient();
String content = "//x00//x00//x00//x00//x15{“message”:”Hello”}";
try
{
//Define a postRequest request
HttpPost postRequest = new HttpPost("http://10.10.xx.xx:31313/com.test.echo.EchoService/echo");
//Set the API media type in http content-type header
postRequest.addHeader("content-type", "application/grpc");
//Set the request post body
StringEntity userEntity = new StringEntity(content);
postRequest.setEntity(userEntity);
//Send the request; It will immediately return the response in HttpResponse object if any
HttpResponse response = httpClient.execute(postRequest);
//verify the valid error code first
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 201)
{
throw new RuntimeException("Failed with HTTP error code : " + statusCode);
}
}
finally
{
//Important: Close the connect
httpClient.getConnectionManager().shutdown();
}
}
在GRPC服务中,我看到以下错误:
io.grpc.netty.shaded.io.grpc.netty.NettyServerStream$TransportState deframeFailed
WARNING: Exception processing message
io.grpc.StatusRuntimeException: INTERNAL: gRPC frame header malformed: reserved bits not zero
at io.grpc.Status.asRuntimeException(Status.java:524)
at io.grpc.internal.MessageDeframer.processHeader(MessageDeframer.java:377)
at io.grpc.internal.MessageDeframer.deliver(MessageDeframer.java:267)
at io.grpc.internal.MessageDeframer.request(MessageDeframer.java:161)
at io.grpc.internal.AbstractStream$TransportState.requestMessagesFromDeframer(AbstractStream.java:205)
以下是正在处理此问题的GRPC服务器代码:
private static final int HEADER_LENGTH = 5;
private static final int COMPRESSED_FLAG_MASK = 1;
private static final int RESERVED_MASK = 0xFE;
引发异常的地方:
private void processHeader() {
int type = nextFrame.readUnsignedByte();
if ((type & RESERVED_MASK) != 0) {
throw Status.INTERNAL.withDescription(
"gRPC frame header malformed: reserved bits not zero")
.asRuntimeException();
}
答案 0 :(得分:0)
您应该使用RPC请求而不是发送字符串:
String content = "//x00//x00//x00//x00//x15{“message”:”Hello”}";