本文介绍了如何在onNext调用中从上下文/范围中获取变量?(java/grpc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用上下文="https://github.com/grpc/grpc-java" rel ="nofollow noreferrer"> grpc/java .

I am working with contexts within grpc/java.

我最终要尝试

1)从每个传入请求中获取作为标头附加的元数据(cred元数据)

1) grab metadata attached as a header from each incoming request (cred metadata)

2)将其附加到grpc范围/上下文,以便在服务器端出现请求时可以访问它.

2) attach this to the grpc scope/context, so that I have access to it when the request comes in, server side.

这是一种无法解决API的变通方法.尽管如此,我已经定义了一个拦截器,该拦截器将剥离元数据的每个传入请求.

It's kind of a work around for not being able to change the API as much as I would like to. Nonetheless, I have defined an Interceptor that will strip each incoming request of the metadata.

public class OnBehalfOfInterceptor implements ServerInterceptor {

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

        Context ctx = Context.current();

        String mayDelegate = headers.get(
            Metadata.Key.of("may-delegate", Metadata.ASCII_STRING_MARSHALLER)
        );
        String onBehalfOf;

        if (mayDelegate != null && str2bool(mayDelegate)) {
            onBehalfOf = headers.get(
                Metadata.Key.of("on-behalf-of", Metadata.ASCII_STRING_MARSHALLER)
            );
            ctx = ctx.withValue(
                    Context.key("on-behalf-of"),
                    onBehalfOf
            );
        }

        return Contexts.interceptCall(ctx, call, headers, next);
    }

}

此代码尝试查找字符串"on-behalf-of",如果找到它,则将其附加到当前上下文.

This code attempts to find a string "on-behalf-of" and if it finds it, attaches it to the current context.

现在,当我调试当前的Context时,我可以看到以下输出

Now, when I go to debug the current Context, I am able to see the following output

Context.current().keyValueEntries.root.values[1]

result
    key "on-behalf-of"
    value "my id"

这是.onNext()调用中的调试输出,我能够看到上下文变量,正如我认为的那样.但是,当我不在调试运行时环境中时,我无法获取"keyValueEntries".我猜想这是调试器提供的,不一定是Context类的成员.

This was the debug output inside a .onNext() call, I was able to see the context variables as I figured I should be able to. However, I am not able to grab "keyValueEntries" while I am not in the debug run time environment. I'm guessing that it is something that the debugger provides, not necessarily a member of the Context class.

所以...如何正确访问附加到当前上下文的变量?我尝试了一些不同的示例,并调试了它们的方式.

So... How do I properly get access to the variables that I had attached to the current context? I've tried a few different examples and debugged my way through them.

Context.current().get(Context.key("on-behalf-of"))

// this ^ returns null

我只是在想这个错误吗?有没有更简单的方法可以从传入请求中获取元数据?

Am I just thinking about this wrong? Is there an easier way to grab metadata off of the incoming request?

推荐答案

Context.Key 使用引用相等.您应该在两个位置使用相同的 Key 实例.传递的字符串只是调试字符串.如该文档:

Context.Key uses reference equality. You should use the same Key instance in both locations. The String passed is just a debug string. As written in the documentation:

使用引用相等性意味着您可以使用Java可见性限制,以与 ThreadLocal 相同的方式来限制哪些代码可以访问上下文状态.

Using reference equality means you can use Java visibility restrictions to limit what code has access to the context state, in the same way that's possible with ThreadLocal.

这篇关于如何在onNext调用中从上下文/范围中获取变量?(java/grpc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:16