将 PHP 代码连接到 Java 后端
我正在实现一个网站,使用 PHP 作为前端,使用 Java 服务作为后端。 两部分内容如下:
PHP前端监听http请求并与数据库交互。
Java后端持续运行,响应前端的调用。
更具体地说,后端是一个守护程序,用于连接并维护与多个 IM 服务(AOL、MSN、Yahoo、Jabber...)的链接。
这两层将部署在同一个系统(我想是 CentOS 机器)上,引入中间层(例如:使用 XML-RPC)会降低性能(资源也相当有限)。
问题:有没有办法将两层直接链接起来? (中间不再有网络服务)
I am implementing a website using PHP for the front end and a Java service as the back end. The two parts are as follows:
PHP front end listens to http requests and interacts with the database.
The Java back end run continuously and responds to calls from the front end.
More specifically, the back end is a daemon that connects and maintain the link to several IM services (AOL, MSN, Yahoo, Jabber...).
Both of the layers will be deployed on the same system (a CentOS box, I suppose) and introducing a middle layer (for instance: using XML-RPC) will reduce the performance (the resource is also rather limited).
Question: Is there a way to link the two layers directly? (no more web services in between)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于这是两个单独运行的进程之间的通信,因此“直接”调用(如 JNI 中所示)是不可能。 进行此类进程间通信的最简单方法可能是命名管道和网络套接字。 在这两种情况下,您都必须定义通信协议并在双方上实现它。 使用 XML-RPC 等标准协议可以使这变得更容易,但并不是绝对必要的。
Since this is communication between two separate running processes, a "direct" call (as in JNI) is not possible. The easiest ways to do such interprocess communcation are probably named pipes and network sockets. In both cases, you'll have to define a communication protocol and implement it on both sides. Using a standard protocol such as XML-RPC makes this easier, but is not strictly necessary.
应用程序集成通常有四种模式:
这些模式中的每一种都有优点和缺点,但一个好的经验法则是选择耦合最松散的模式。 例如,如果您选择#4,您的 Java 应用程序可能会崩溃,而不会同时关闭 PHP 应用程序。
我建议在查看此处答案中列出的特定库或技术之前,先选择适合您的模式,然后研究您的特定选项。
There are generally four patterns for application integration:
Each of these patterns has pros and cons, but a good rule of thumb is to pick the one with the loosest coupling that you can get away with. For example, if you selected #4 your Java app could crash without also taking down your PHP app.
I'd suggest before looking at specific libraries or technologies listed in the answers here that you pick the right pattern for you, then investigate your specific options.
我尝试过 PHP-Java 桥接(php-java-bridge.sourceforge.net/pjb/),效果很好。 基本上,我们需要运行一个监听端口的 jar 文件(JavaBridge.jar)(有多个选项可用,如本地套接字、8080 端口等)。 您的 java 类文件必须可用于类路径中的 JavaBridge。 您需要在 php 中包含 Java.inc 文件,然后您就可以访问 Java 类。
I have tried PHP-Java bridge(php-java-bridge.sourceforge.net/pjb/) and it works quite well. Basically, we need to run a jar file (JavaBridge.jar) which listens on port(there are several options available like Local socket, 8080 port and so on). Your java class files must be availabe to the JavaBridge in the classpath. You need to include a file Java.inc in your php and you can access the Java classes.
当然,有很多方法,但你提到了有限的资源......
恕我直言,定义你自己的轻量级类似 RPC 的协议并使用 TCP/IP 上的套接字进行通信。 实际上,在这种情况下,不需要充分利用 RPC 等的优势...您只需要为此特定情况定义 API 并在双方实现它。 在这种情况下,您可以将数据包序列化得非常小。 您甚至可以为您的远程方法分配一种 GUID,并使用它们来节省流量并加快相互通信的速度。
使用套接字的优点是您的解决方案具有很好的可扩展性。
Sure, there are lots of ways, but you said about the limited resource...
IMHO define your own lightweight RPC-like protocol and use sockets on TCP/IP to communicate. Actually in this case there's no need to use full advantages of RPC etc... You need only to define API for this particular case and implement it on both sides. In this case you can serialize your packets to quite small. You can even assign a kind of GUIDs to your remote methods and use them to save the traffic and speed-up your intercommunication.
The advantage of sockets usage is that your solution will be pretty scalable.
您可以尝试 PHP/Java 集成。
另外,如果通信是单向的(例如“sendmail for IM”),您可以将 PHP 请求写入文件并在 Java 应用程序中监视该请求。
You could try the PHP/Java integration.
Also, if the communication is one-way (something like "sendmail for IM"), you could write out the PHP requests to a file and monitor that in your Java app.
我最近也面临这个问题。 上面的 Resin 解决方案实际上是沿着 JRuby、Jython 和 Rhino 的路线用 Java 完全重写 PHP。 它被称为栎属。 但我猜对你来说就像对我一样,放弃你的 Apache/PHP 设置并不是一个真正的选择。
除此之外,Quercus 还有更多问题:免费版本是 GPL,如果您正在开发商业软件,这会很棘手(尽管不像 Resin 希望您相信的那么棘手(但 IANAL)),而且最重要的是免费版本不支持编译为字节码,因此它基本上是用 Java 编写的解释器。
我最终决定只通过 HTTP 交换简单的消息。 我使用了 PHP 的
json_encode()
/json_decode()
和 Java 的 json-lib 以 JSON 格式对消息进行编码(简单、基于文本、与数据模型非常匹配)。另一个有趣且轻量级的选项是让 Java 生成 PHP 代码,然后使用 PHP include() 指令通过 HTTP 获取该代码并执行它。 我还没有尝试过这个。
如果您关心的是实际的 HTTP 调用(为了性能),那么这些解决方案都无济于事。 我只能说,我在同一 LAN 上使用 PHP 和 Java 时没有遇到任何问题。 我的感觉是,只要您保持 RPC 调用相当粗粒度(无论如何您确实应该这样做),这对于绝大多数应用程序来说都不会成为问题。
I was also faced with this problem recently. The Resin solution above is actually a complete re-write of PHP in Java along the lines of JRuby, Jython and Rhino. It is called Quercus. But I'm guessing for you as it was for me, tossing out your Apache/PHP setup isn't really an option.
And there are more problems with Quercus besides: the free version is GPL, which is tricky if you're developing commercial software (though not as tricky as Resin would like you to believe (but IANAL)) and on top of that the free version doesn't support compiling to byte code, so its basically an interpreter written in Java.
What I decided on in the end was to just exchange simple messages over HTTP. I used PHP's
json_encode()
/json_decode()
and Java's json-lib to encode the messages in JSON (simple, text-based, good match for data model).Another interesting and light-weight option would be to have Java generate PHP code and then use PHP include() directive to fetch that over HTTP and execute it. I haven't tried this though.
If its the actual HTTP calls you're concerned about (for performance), neither of these solutions will help there. All I can say is that I haven't had problems with the PHP and Java on the same LAN. My feeling is that it won't be a problem for the vast majority of applications as long as you keep your RPC calls fairly course-grained (which you really should do anyway).
抱歉,这是一个快速回答,但是:我听说 Resin 应用服务器支持集成 java 和 PHP。
他们声称他们可以将 php 和 java 结合在一起: http://www.caucho.com/resin -3.0/quercus/
我使用resin 来服务J2ee 应用程序,但不使用它的PHP 支持。
我很想听听这样的冒险经历。
Sorry, this is a bit of a quick answer but: i heard the Resin app server has support for integrating java and PHP.
They claim they can smash php and java together: http://www.caucho.com/resin-3.0/quercus/
I've used resin for serving J2ee applications, but not for its PHP support.
I'd be interested to hear of such adventures.
为什么不使用网络服务?
创建一个 Java 层并放置一个 ws 访问(Axis、SpringWS 等),然后 Php 使用一个 ws 客户端访问 Java 层。
我认为它简单又有用。
Why not use web service?
Make a Java layer and put a ws access(Axis, SpringWS, etc...) and the Php access the Java layer using one ws client.
I think it's simple and useful.
我遇到过这个页面,它介绍了链接两个层的方法。 然而,它仍然需要一个中间层(TCP/IP)。 此外,其他服务也可以利用 Java 服务,因为它接受所有传入连接。
http://www.devx.com/Java/Article/20509
[研究。 ..]
I've come across this page which introduces a means to link the two layers. However, it still requires a middle layer (TCP/IP). Moreover, other services may exploit the Java service as well because it accepts all incoming connections.
http://www.devx.com/Java/Article/20509
[Researching...]