每天20分支之java grpc的metadata

2022-09-25 20:07:44 浏览数 (1)

client 的配置

代码语言:txt复制
@Slf4j
@Service
public class Log3Interceptor  implements ClientInterceptor {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel channel) {

        return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(channel.newCall(method, callOptions)) {
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
                headers.put(Metadata.Key.of("META1", ASCII_STRING_MARSHALLER), "meta11111111");
                log.info("headers:{}", headers);
                super.start(responseListener, headers);
            }
        };
    }
}

server的配置

代码语言:txt复制
@GrpcGlobalServerInterceptor
@Component
@Slf4j
public class MDInterceptor implements io.grpc.ServerInterceptor {


    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata metadata, ServerCallHandler<ReqT, RespT> next) {

        String retMd = metadata.get(Metadata.Key.of("META1", ASCII_STRING_MARSHALLER));
        log.info("Metadata Retrived : "   retMd);
        return next.startCall(call, metadata);
    }
}

代码: https://github.com/beckbikang/kgrpc/blob/main/README.md

0 人点赞