两个 Web 服务之间的通信
我的网络服务有问题。它们用 Java 编程并在 WASCE 服务器上运行(两者都在同一服务器上)。 我想解决的问题: 我们有两个 Web 服务:App1 和 App2
在 App1 中我想调用 App2 中的函数。我该怎么做?这可能吗? 我尝试在 App1 内创建一个soapClient,以便我可以连接到 App2,但这不起作用。 经验: 我有一个调用 app1 的客户端从 app1 获取数据并将其发送到 app2,然后从 app2 取回响应数据并将其发送到 app1 中的其他函数。
我想要做的是跳过客户端部分并直接执行,以便 app1 可以直接将数据发送到 app2,然后接收答案执行任何需要执行的操作。
注意:这两个 Web 服务都使用到数据库的连接。
先感谢您。 (已用附加数据进行编辑)
I have a problem with web services. They are programed in Java and are running on a WASCE server ( both are on the same server).
My problem that i want to solve:
We have two Web services: App1 and App2
In App1 i want to call a function that is in App2. How can i do this? Is this even possible?
I tried creating a soapClient inside the App1 so i can connect to the App2 but that doesn't work.
exp:
I have a client that calls app1 gets data from app1 and send it to app2 then get back the response data from app2 and send it to an other function into the app1.
What i want to do is to skip the client part and do it directly so that app1 can send directly the data to the app2 and then receive an answer do whatever it needs to do.
For the note: Both of the web services use the connection to the database.
Thank you in advance.
(it has been edited with additional data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“不起作用”是什么意思?究竟发生了什么?
首先为 App2 生成一些客户端代码。您可以在一些简单的 Java 环境(或者说 Servlet)中使用它吗?如果可行,当您尝试从 App 1 Service 实现代码内部调用它时会发生什么?
但是:如果这些是在同一个 JVM 中运行的相关服务,您不能使用 java 库建立一些更简单的关系。我开发服务的首选方法是首先开发一些有用的 Java 代码,并确保其有效,然后将其“包装”为 Web 服务。在这种情况下,我有一个可调用例程,只能作为 Java 调用。
What does "doesn't work" mean? Exactly what happens?
Start by generating some client code for App2. Can you use that from some simple Java environment, or say a Servlet. If that works, what happens when you try to call it from inside your App 1 Service implementation code?
However: if these are related services running in the same JVM can you not set up some simpler relationship using java libraries. My preferred way of developing a service is first to develop some useful Java code, and make sure that works, then "wrap" it as a Web Service. In which case I have a callable routine that can just be invoked as Java.
这绝对是可能的,但复杂性和可行性的程度不同,具体取决于您想要什么以及您对其施加的限制。
如果您对 App2 中的方法公开没有问题,那么解决此问题的最简单方法之一可能是简单地创建一个公开该方法的 Web 服务并从 App1 调用 if 。
如果您希望 App2 的方法本质上受到“保护”,以便 App1 可以调用它,但公共客户端不能调用它,那么有几种替代选项。首先,您可以使用防火墙或等效设备来阻止对服务 URL 的外部请求。或者,您可以通过某种形式的进程间通信公开该方法; RMI 显然是 Java 的原生 RMI(设置App2 中的 RMI 方法并通过管理器导出该方法,然后获取 App1 中的引用并远程调用该方法)。根据您具体想要做什么,您可能会更好地使用一个在幕后完成所有这些工作的框架;例如,通过 Terracotta 之类的方式分发对象。
不过,您应该在问题中提供更多细节 - 目前您真正指定的唯一一件事是您想从 App1 调用 App2 中的“函数”。有数十种(如果不是数百种)方法可以解决此问题,最好的方法将取决于您想要做的事情的细节。
编辑(根据评论):缺少的并不是您想要执行的操作的细节 - 我很好地理解您想从内部调用 App2 中的某些方法应用程序1。更多的是架构细节 - 两个客户端都用什么语言编码,您使用什么库来执行 Web 服务,两个客户端是在同一台机器上还是单独的机器上(如果是同一台机器,是否相同 JVM),是否有任何可能会抑制某些类型连接的防火墙问题,是否有任何办公室政治限制可能会抑制您的选择,是否有任何安全限制可能会起到同样的作用(例如您是否可以公开公开 App2 方法的功能)。所有这些都将塑造什么是可能的以及什么是最佳的 - 因为在一天结束时,所有网络基本上都是我想使用这里远程计算机上的资源 /em>。如果没有更多的架构细节,实际上有数十种方法可以实现这一目标。
关于说明:您将创建一个 Web 服务来公开 App2 的功能,就像创建任何其他 Web 服务一样(详细信息取决于您正在使用的工具/框架)。例如,如果您使用支持 JSR-181 注释的工具,您可以在 App2 中编写一个执行此功能的方法,并使用
@WebMethod
对其进行注释。然后,您需要确保,如果此方法不是现有 Webservice 类的一部分,您将使用@WebService
注释其类。我认为既然您已经拥有一些 Web 服务,您就会知道如何编写/定义它们。至于从 App1 访问 Web 服务,可以通过 Java SOAP 客户端非常简单地完成。 WSDL2Java 等工具可以创建一个存根类来建模您可以调用的远程服务;或者,您可以获得更丰富的界面,例如 CXF< /a>.
您当前使用什么 WS 库?在尝试使用它执行此交互时遇到了哪些错误?
It's definitely possible, with differing levels of complexity and feasibility depending on exactly what it is you want, and the restrictions you place on it.
Probably one of the simplest ways to go about this, if you don't have a problem with the method in App2 being public, is to simply create a web service exposing that method and call if from App1.
If you want App2's method to be essentially "protected", so that it can be called by App1 but not by public clients, then there are several alternative options. Firstly, you could use firewalls or equivalent to prevent external requests to the service URL. Alternatively, you could expose the method through some form of interprocess communication; RMI would be the obvious native one for Java (set up an RMI method in App2 and export this through a manager, then obtain the reference in App1 and invoke the method remotely). Depending on exactly what it is you want to do, you may be better off with a framework that does all this under the covers; e.g. distributed objects through something like Terracotta.
You should give more detail in your question, though - currently the only thing you've really specified is that you want to call "a function" in App2 from App1. There are dozens (if not hundreds) of ways to go about this and the best one(s) will depend on the details of what you're trying to do.
EDIT (in light of comments): It's not the details of what you want to do that are lacking - I understand fine that you want to call some method in App2 from within App1. It's more the architectural details - what languages are both clients coded in, what libraries are you using to do the web services, are both clients on the same machine or separate ones (and if same machine, same JVM or not), are there any firewall issues that could inhibit certain kinds of connectivity, are there any office-political restrictions that could inhibit your options, are there any security restrictions that could do the same (such as whether you can expose the functionality of App2's method publically or not). All of these will shape what is possible and what is optimal - because at the end of the day, all networking is basically I want to use resources on that remote computer from here. Without more architectural specifics, there are literally dozens of ways that you could achieve this.
Regarding exposition: You would create a web service to expose App2's function in the same way you would create any other web service (with the details being dependent on the tool/framework you're using). As an example if you're using a tool that supports the JSR-181 annotations, you'd write a method in App2 that performs this function, and annotate it with
@WebMethod
. Then you'd ensure that if this method is not part of an existing webservice class you'd annotate its class with@WebService
. I was presuming that since you already have a couple of web services, you'd know how to write/define them.As for accessing the web service from App1, this can be done quite simply by a Java SOAP client. A tool such as WSDL2Java can create a stub class modelling the remote service that you can call; alternatively you can get a richer interface with something like CXF.
What WS library are you using currently, and what errors have you encountered when trying to use it to perform this interaction?