简体   繁体   English

如何使用 Wildfly 上的远程 EJB 将有关调用者/客户端的一些信息发送到服务器端?

[英]How to send some information about caller/client to server side using Remote EJB on Wildfly?

I'm working on an application with a large number of Remote EJB service methods, and I'd like to have some useful information about the client calling the methods (other than very basic information such as IP address...).我正在处理具有大量远程 EJB 服务方法的应用程序,并且我想获得有关调用这些方法的客户端的一些有用信息(除了非常基本的信息,例如 IP 地址...)。

I found this question but it's a bit dated :我发现了这个问题,但它有点过时了:

How can I identify the client or caller of an EJB within the request? 如何在请求中识别 EJB 的客户端或调用者?

Is there some kind of custom client context / work area in which I could put the caller details and receive them on server side inside a thread local ?是否有某种自定义客户端上下文/工作区,我可以在其中放置调用者详细信息并在本地线程内的服务器端接收它们?

Basically do I have another option than adding a parameter to every single method of every service ?基本上,除了向每个服务的每个方法添加参数之外,我还有其他选择吗?

With security enabled you have the possibility to retrieve the current user.启用安全性后,您可以检索当前用户。 This is pretty simple, but probably won't fit all needs.这很简单,但可能无法满足所有需求。

In general, there is nothing you can use out-of-the-box.一般来说,没有什么可以开箱即用的。 Adding some custom parameter is probably the worst option.添加一些自定义参数可能是最糟糕的选择。

JBoss and Wildfly are offering the possibility to use EJB client- and server-side container interceptors. JBoss 和 Wildfly 提供了使用 EJB 客户端和服务器端容器拦截器的可能性。 Usage and implementation details depend on the version of your application server.使用和实现细节取决于您的应用程序服务器的版本。

I implemented this by utilizing the MDC (mapped diagnostic context) of our logging framework to enhance server side logging with caller information.我通过利用我们的日志框架的 MDC(映射诊断上下文)来增强服务器端日志记录与调用者信息来实现这一点。 You can think about this like using a ThreadLocal.您可以将此视为使用 ThreadLocal。 Of course, you need something like a caller context on the client side holding the specific information.当然,您需要在客户端保存特定信息的调用者上下文之类的东西。 Global remote client data (ip address, ...) can be set within the client interceptor, too.全局远程客户端数据(IP 地址,...)也可以在客户端拦截器中设置。

A coarse overview what I did:我所做的粗略概述:

  • Configure client- and server-side logging to use additional MDC data配置客户端和服务器端日志记录以使用其他 MDC 数据
  • Enhance client side MDC with key/value data使用键/值数据增强客户端 MDC
  • Client-side interceptor enxtracts the data from the MDC and puts it on the invocation context客户端拦截器从 MDC 中提取数据并将其放在调用上下文中
  • Server-side interceptor extracts the data from the invocation context and enhances server side MDC服务器端拦截器从调用上下文中提取数据并增强服务器端MDC

This approach is working, but depending on your application complexity (eg with server2server calls, bean2bean calls on local asynch EJBs, ...) complexity increases.这种方法有效,但取决于您的应用程序复杂性(例如,使用 server2server 调用、本地异步 EJB 上的 bean2bean 调用,...)复杂性会增加。 Don't forget to think about cleaning up eg your ThreadLocal data to avoid possible memory leaks.不要忘记考虑清理例如您的 ThreadLocal 数据以避免可能的内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM