gRPC请求中对header进行处理

Posted everlastxgb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gRPC请求中对header进行处理相关的知识,希望对你有一定的参考价值。

gRPC请求设置header(androidios、JavaServer)

1.[Android (Java)]

1.1只设置客户端请求时附带的header

见类 io.grpc.stub.MetadataUtils,其中有个方法:

  /**
   * Attaches a set of request headers to a stub.
   *
   * @param stub to bind the headers to.
   * @param extraHeaders the headers to be passed by each call on the returned stub.
   * @return an implementation of the stub with @code extraHeaders bound to each call.
   */
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
  public static <T extends AbstractStub<T>> T attachHeaders(
      T stub,
      final Metadata extraHeaders) 
    return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
  

稍微自己封装一下的效果,如下:

    private static <T extends AbstractStub<T>> T attachHeaders(T stub, final Map<String, String> headerMap) 
        Metadata extraHeaders = new Metadata();
        if (headerMap != null) 
            for (String key : headerMap.keySet()) 
                Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
                extraHeaders.put(customHeadKey, headerMap.get(key));
            
        
        return MetadataUtils.attachHeaders(stub, extraHeaders);
    

1.2 支持设置客户端请求的header以及获取服务端返回结果中的header

第一步:主要是设置http的拦截器ClientInterceptor,拦截器类大概实现代码如下:


/**
 * A interceptor to handle client header.
 *
 * @author Kido
 * @email everlastxgb@gmail.com
 * @create_time 2016/7/7 11:02
 */

class HeaderClientInterceptor implements ClientInterceptor 

    private static final String TAG = "HeaderClientInterceptor";

    private Map<String, String> mHeaderMap;

    public HeaderClientInterceptor(Map<String, String> headerMap) 
        mHeaderMap = headerMap;
    

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
                                                               CallOptions callOptions, Channel next) 
        return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) 

            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) 
                /* put custom header */
                if (mHeaderMap != null) 
                    for (String key : mHeaderMap.keySet()) 
                        Metadata.Key<String> customHeadKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
                        headers.put(customHeadKey, mHeaderMap.get(key));
                    
                
                Logger.i(TAG, "header send to server:" + headers);
                super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) 
                    @Override
                    public void onHeaders(Metadata headers) 
                        /**
                         * if you don't need receive header from server,
                         * you can use @link io.grpc.stub.MetadataUtils attachHeaders
                         * directly to send header
                         */
                        Logger.i(TAG, "header received from server:" + headers);
                        super.onHeaders(headers);
                    
                , headers);
            
        ;
    

第二步:大概使用代码如下:

Map<String, String> headerMap = new HashMap<>();
//...
ClientInterceptor interceptor = new HeaderClientInterceptor(headerMap);
//...
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
Channel channel = ClientInterceptors.intercept(managedChannel, interceptor);

// then create stub here by this channel

2. [iOS (Objective-C)]

首先,可以看到GRPCCall中有如下属性:

@property(atomic, readonly) NSDictionary *responseHeaders;
@property(atomic, readonly) NSDictionary *responseTrailers;

接着,可以进行如下设置:

call.requestHeaders[@"My-Header"] = @"Value for this header";
call.requestHeaders[@"Another-Header"] = @"Its value";

3.[Server (Java)]

既然是Java,实现方式应该也和上述Android大概一样。
第一步:拦截器

/**
 * A interceptor to handle server header.
 */
public class HeaderServerInterceptor implements ServerInterceptor 

  private static final Logger logger = Logger.getLogger(HeaderServerInterceptor.class.getName());

  private static Metadata.Key<String> customHeadKey =
      Metadata.Key.of("custom_server_header_key", Metadata.ASCII_STRING_MARSHALLER);


  @Override
  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
      ServerCall<ReqT, RespT> call,
      final Metadata requestHeaders,
      ServerCallHandler<ReqT, RespT> next) 
    logger.info("header received from client:" + requestHeaders);
    return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) 
      @Override
      public void sendHeaders(Metadata responseHeaders) 
        responseHeaders.put(customHeadKey, "customRespondValue");
        super.sendHeaders(responseHeaders);
      
    , requestHeaders);
  

第二步:使用示例

//...
Server server = ServerBuilder.forPort(port)
  .addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
  .build()
  .start();
//...

以上是关于gRPC请求中对header进行处理的主要内容,如果未能解决你的问题,请参考以下文章

etcdv3与etcdv2特性比较

gRPC中Header传值与错误拦截处理知识笔记

在Go中对gRPC+ProtoBuf与Http+Json进行基准测试

gRPC请求超时和异常处理

gRPC请求超时和异常处理

javaservlet处理四种常用api请求get,put,post,delete