Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

ProtoBuf gRPC analyzes how to configure the request header

Shulou Source: shulou.com Published: 2022-05-31 14:18:04 09月30日 Update

Today, the editor will share with you the relevant knowledge points about how to configure the ProtoBuf gRPC analysis request header. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

ProtoBuf + gRPC Analysis request header concept

Protobuf, the abbreviation of Google protocol buffer, is a language-neutral, platform-independent, easy-to-expand structured data serialization technology, which can be used in data transmission, storage and other fields. Other serialization technologies similar to Protoful include XML, JSON, Thrift, etc., but Protoful is faster, smaller, simpler, and has good compatibility.

At present, it is often used in business scenarios such as Android live barrage.

Configure request header method 1

Set only the header that comes with the client request

Class io.grpc.stub.MetadataUtils, where there is a method

@ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/1789") public static T attachHeaders (T stub, final Metadata extraHeaders) {return stub.withInterceptors (newAttachHeadersInterceptor (extraHeaders));}

**

After self-packaging

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

**

Method 2

Support setting the header requested by the client and getting the header in the result returned by the server

Official demo

Official complete Demo

1. Set interceptor class HeaderClientInterceptor implements ClientInterceptor {private static final String TAG = "HeaderClientInterceptor"; private Map mHeaderMap; public HeaderClientInterceptor (Map headerMap) {mHeaderMap = headerMap } @ Override public ClientCall interceptCall (MethodDescriptor method, CallOptions callOptions, Channel next) {return new SimpleForwardingClientCall (next.newCall (method, callOptions)) {@ Override public void start (Listener responseListener) Metadata headers) {/ * put custom header * / if (mHeaderMap! = null) {for (String key: mHeaderMap.keySet ()) {Metadata.Key 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 (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);}

_

two。 Use Map 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

**

So

To add header, you must implement the method of interceptCall in the ClientInterceptor interface class

And there needs to be a specific way to add Header

No matter how confused grpc is, it can't be separated from this configuration mode.

For example, this

Pay attention to their types, it is obvious that this situation can be quickly inferred

The high probability of z1.c.v.p.a.d.b.f.a.a () is the confused interceptCall

Z1.c.v.p.a.d.b.f.a.c () is a function to add request headers

These are all the contents of the article "how to configure ProtoBuf gRPC Analysis request headers". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Method configuration knowledge article analysis content official client client that is sequence technology data different large obvious good business compatibility function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi OPPO Reno Microsoft Apple