将 PHP 代码连接到 Java 后端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/891871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect PHP code to Java backend
提问by ThoaiOnline
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,后端使用 Java 服务。两部分如下:
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.
PHP前端监听http请求并与数据库交互。
Java 后端持续运行并响应来自前端的调用。
More specifically, the back end is a daemon that connects and maintain the link to several IM services (AOL, MSN, Yahoo, Jabber...).
更具体地说,后端是一个守护进程,它连接并维护与多个 IM 服务(AOL、MSN、雅虎、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).
这两层都将部署在同一个系统上(我想是 CentOS 机器),引入中间层(例如:使用 XML-RPC)会降低性能(资源也相当有限)。
Question: Is there a way to link the two layers directly? (no more web services in between)
问题:有没有办法直接链接两层?(中间不再有网络服务)
回答by Michael Borgwardt
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 pipesand 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.
由于这是两个独立运行的进程之间的通信,因此“直接”调用(如在JNI 中)是不可能的。进行这种进程间通信的最简单方法可能是命名管道和网络套接字。在这两种情况下,您都必须定义一个通信协议并在双方实施它。使用诸如 XML-RPC 之类的标准协议可以使这更容易,但并不是绝对必要的。
回答by Robert Campbell
There are generally four patterns for application integration:
应用集成一般有四种模式:
- via Filesystem, ie. one producers writes data to a directory monitored by the consumer
- via Database, ie. two applications share a schema or table and use it to swap data
- via RMI/RPC/web service/any blocking, sync call from one app to another. For PHP to Java you can pick from the various integration libraries listed above, or use some web services standards like SOAP.
- via messaging/any non-blocking, async operation where one app sends a message to another app.
- 通过文件系统,即。一个生产者将数据写入由消费者监控的目录
- 通过数据库,即。两个应用程序共享一个模式或表并使用它来交换数据
- 通过 RMI/RPC/Web 服务/任何阻塞,同步从一个应用程序到另一个应用程序的调用。对于 PHP 到 Java,您可以从上面列出的各种集成库中进行选择,或者使用一些 Web 服务标准,例如 SOAP。
- 通过消息传递/任何非阻塞、异步操作,其中一个应用程序向另一个应用程序发送消息。
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.
这些模式中的每一个都有优点和缺点,但一个好的经验法则是选择一个你可以摆脱的最松散的耦合。例如,如果您选择 #4,您的 Java 应用程序可能会崩溃,而不会同时关闭您的 PHP 应用程序。
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.
在查看此处答案中列出的特定库或技术之前,我建议您为自己选择正确的模式,然后调查您的特定选项。
回答by Naveen
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.
我已经尝试过 PHP-Java 桥接器(php-java-bridge.sourceforge.net/pjb/)并且它工作得很好。基本上,我们需要运行一个 jar 文件(JavaBridge.jar)来监听端口(有几个可用的选项,如本地套接字、8080 端口等)。您的 Java 类文件必须可用于类路径中的 JavaBridge。您需要在 php 中包含一个文件 Java.inc 才能访问 Java 类。
回答by Jet
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.
恕我直言,定义您自己的轻量级类似 RPC 的协议并使用 TCP/IP 上的套接字进行通信。实际上,在这种情况下,不需要充分利用 RPC 等的优势……您只需要为这种特殊情况定义 API 并在双方实现它。在这种情况下,您可以将数据包序列化为非常小。您甚至可以为远程方法分配一种 GUID,并使用它们来节省流量并加快相互通信。
The advantage of sockets usage is that your solution will be pretty scalable.
使用套接字的优势在于您的解决方案将具有很强的可扩展性。
回答by Piskvor left the building
You could try the PHP/Java integration.
您可以尝试PHP/Java 集成。
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.
此外,如果通信是单向的(类似于“为 IM 发送邮件”),您可以将 PHP 请求写出到一个文件并在您的 Java 应用程序中监视它。
回答by Ramon
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.
我最近也面临这个问题。上面的 Resin 解决方案实际上是沿 JRuby、Jython 和 Rhino 的路线用 Java 完全重写 PHP。它被称为栎。但我猜对你来说就像对我一样,放弃你的 Apache/PHP 设置并不是一个真正的选择。
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.
此外,Quercus 还存在更多问题:免费版本是 GPL,如果您正在开发商业软件,这会很棘手(虽然不像 Resin 希望您相信的那么棘手(但 IANAL)),而且最重要的是免费版本不支持编译为字节码,所以它基本上是用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-libto encode the messages in JSON (simple, text-based, good match for data model).
我最终决定只通过 HTTP 交换简单的消息。我使用 PHP 的json_encode()/json_decode()和 Java 的json-lib将消息编码为 JSON(简单、基于文本、数据模型的良好匹配)。
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.
另一个有趣且轻量级的选择是让 Java 生成 PHP 代码,然后使用 PHP include() 指令通过 HTTP 获取并执行它。不过这个我没试过。
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).
如果它是您关心的实际 HTTP 调用(为了性能),那么这些解决方案都不会在那里提供帮助。我只能说我在同一个局域网上的 PHP 和 Java 没有问题。我的感觉是,只要您保持 RPC 调用相当粗粒度(无论如何您确实应该这样做),这对于绝大多数应用程序来说都不是问题。
回答by chickeninabiscuit
Sorry, this is a bit of a quick answer but: i heard the Resin app server has support for integrating java and PHP.
抱歉,这是一个快速的回答,但是:我听说 Resin 应用服务器支持集成 java 和 PHP。
They claim they can smash php and java together: http://www.caucho.com/resin-3.0/quercus/
他们声称他们可以将 php 和 java 粉碎在一起:http: //www.caucho.com/resin-3.0/quercus/
I've used resin for serving J2ee applications, but not for its PHP support.
我使用树脂为 J2ee 应用程序提供服务,但没有使用它的 PHP 支持。
I'd be interested to hear of such adventures.
我很想听听这样的冒险。
回答by rresino
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.
创建一个 Java 层并放置一个 ws 访问(Axis、SpringWS 等...),然后 PHP 使用一个 ws 客户端访问 Java 层。
I think it's simple and useful.
我认为它简单而有用。
回答by ThoaiOnline
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.
我遇到了这个页面,它介绍了一种链接两个层的方法。但是,它仍然需要一个中间层(TCP/IP)。此外,其他服务也可能利用 Java 服务,因为它接受所有传入连接。
http://www.devx.com/Java/Article/20509
http://www.devx.com/Java/Article/20509
[Researching...]
[正在研究……]

