首頁 >後端開發 >php教程 >Java與PHP之間的關聯

Java與PHP之間的關聯

jacklove
jacklove原創
2018-06-15 14:12:235343瀏覽

Java與PHP之間的關聯

驗證兩種各自領域稱王的語言(JAVA /PHP),不同語言、不同機制的組合在一起,PHP負責WEB層,Java負責業務與資料邏輯層,真是一對黃金組合(Java PHP整合=混血新寵兒),發揮各自優勢,適合開發B/S企業程式。

1、技術問題

##兩種不同環境,不同機制的語言結合,首先要解決的是如何通訊?其次是通訊內容如何處理?最後是兩者開發,部署環境整合?

1.1兩種語言比較

Php:直觀,快速,簡單,易學,開發動態網頁效率高,草根文化氣息濃重,是語言世界的平民英雄,注重結果。

Java:強大而複雜,有眾多高階功能,又有IMB、Oracle等企業大顎的追捧,是語言世界的貴族王子。

1.2通訊問題

1.2.1訊息佇列

#從作業系統層面觀察: PHP和Java是系統中執行的不同進程,他們之間溝通屬於進程間通訊技術(IPC):

Java與PHP之間的關聯

在傳統的Unix環境中,IPC技術有:管道、訊息佇列、共享記憶體、訊號量等,但在同一個系統中,IPC通訊比基於TCP的socket通訊在效能、資源佔用方面有相當大的優勢。

在Unix/Linux中,PHP基本上提供了所有IPC的存取接口,因為PHP可以看作是以C語言為核心的一個殼,而IPC是系統核心的組成部分,對外提供了一組C函數接口,因此PHP可以非常順暢的運用IPC技術。而Java為了追求誇平台性(Windows的IPC技術和Unix的不同),沒有提供系統級的IPC訪問,這也體現了Java的文化特色:為追求統一可犧牲效能。

1.2.2     Socket

訊息佇列技術只能適用於Unix/Linux系統,而SOCKET則是基於TCP/IP的通訊機制,進而適應各種平台。

 

Java與PHP之間的關聯

####################### #從上圖看出,PHP承擔HTTP層的職責,而Java承擔業務層的職責,他們透過System V Message Queue(訊息佇列,進程間通訊IPC中的一種)相互溝通,Java需要JNI的支持。 ######使用訊息佇列有以下好處:###

1. 讓php和java保持獨立性

2. 有極高的傳輸速度,大於socket

3. 相對於socket方式,Java服務端只提供服務給本機(沒有對外偵聽埠),相對安全,易於管理。


#


1.3#     通訊內容問題

##通訊問題解決了,通訊內容該如何解決?

PHP和Java各自語言內部定義的資料類型,當兩種語言互相傳輸資料時,資料類型怎麼會轉換呢?

     

(1)Xml: xml確是一種誇張平台、能很好描述物件模型的資料封裝技術,但xml體積大傳輸速率慢,通訊兩端解析也比較麻煩。

     

 

(2)###### 序列化: 在傳輸過程中使用序列化和反序列化物件傳輸資料是一種公認且比較合理的方式,且兩種語言都支援。 ###### ###

2#Java與php  結合選型

#目前已知的PHP與Java在Web間整合技術可分成SOAP(Simple Object Access Protocol)、Quercus、PHP/Java Bridge三種,這些技術可以讓PHP與Java在開發Web應用程式時互相使用彼此的程式庫。


#SOAP

SOAP是IBM、 Microsoft等公司開發、W3C推薦,用來實現分散式物件技術的協定。 SOAP提供了一套以XML來包裝程式呼叫、參數傳遞與資訊回傳的機制,借助XML純文字的特性,可透過HTTP、HTTPS、SMTP等通訊管道穿越企業的防火牆。比起CORBA、Java RMI及DCOM這些以專屬binary格式傳送資料的分散式物件技術協議,SOAP具有與程式語言、平台和硬體無關的特性。

Quercus

#

Quercus是一個開源 PHP 5 引擎,它已經擁有了純 Java 的完整實作。 Quercus 在 Resin 應用伺服器之上運行,利用了負載平衡、代理快取等 Resin 功能。

PHP/Java Bridge

#開源產品有:Php-java-bridge 與LAJP

#


2.1#  選擇條件

ü 免費、開源

ü 簡單易用

##ü 高效穩定

2.2    LAJP

##################################################################################

##2.2.1 Features

Ø Complementary advantages: PHP is a very popular WEB programming script language. It is easy to learn, easy to use, and has high development and deployment efficiency.                                                                                                                           It is suitable for writing programs with complex business functions and data. The combination of the two can give full play to their respective advantages.

Ø

Efficient and Stable: The Apache PHP combination can bring excellent WEB service stability, and JAVA can supplement such as connection pooling, transaction management, distribution, and objects models and other high-end features.

Ø

Innovative communication mechanism: The communication between PHP and Java uses two mechanisms: system message queue and Socket, taking into account communication efficiency and platform compatibility.

Ø

Data type automatic conversion mechanism: PHP data and Java data can be automatically matched and converted accurately, without the need for programmers to write parsing code.

Ø Easy to use: LAJP is easy to install and configure, and PHP and JAVA programming conform to their respective programming habits.

Ø Lightweight: The LAJP architecture is very lightweight. In addition to the most basic PHP and Java environments, it does not require any extended or third-party components. container.

2.2.2​​ Data type conversion

PHP and Java each have their own data types defined internally in the language. When PHP data is transferred to Java, Or when Java data is transmitted to PHP, LAJP automatically and accurately converts them internally, and programmers do not need to perform any decoding work.

Java與PHP之間的關聯

##2.2.3 LALP Running environment

Message queue mode

The environment needs to meet the operation of System V message queue:

  • System Currently common Unix/Linux systems can be used Meet the operation of php (Apache) and java, most of which support System by default V message queue.

  • php PHP needs to communicate with the java process through the message queue. According to PHP instructions, PHP is in 4.3. System V message queue is supported after version 0.

  • apache No special requirements, just meet the PHP requirements.

  • java The java version is after 1.5.

  • In a Unix/Linux environment, it is recommended to use the message queue mode.

##socket mode

  • ##System There are no restrictions and it is difficult to find a system that does not support TCP/IP.

  • ##php

    According to the instructions of php, php version >=4.1.0 supports socket

  • apache No special requirements, just meet the PHP requirements.

  • java The java version is after 1.5.

  • Windows system can only use socket mode

These two modes can be used at the same time during the development process. For example, generally developers use the Windows environment, and the program is deployed in the Linux system. The configuration of LAJP mode has nothing to do with coding.

#2.2.4                                   Deployment

2.2.4.1 Distributed architecture

In important and critical business scenarios, the user interaction layer and the business layer are deployed separately. LAJP This type of architecture can be used like this:

Java與PHP之間的關聯

Java in the WEB server is a very "thin" layer, only used to connect to the subsequent EJB services.

2.2.4.2                                                                                                                  

Java與PHP之間的關聯##2.2.4.3 web cluster

By using LAJP in socket communication mode , you can build a simple distributed WEB cluster architecture:

##2.2.4.4 Complex Architecture ApplicationJava與PHP之間的關聯

The flexibility of using LAJP, the design and selection of the architecture lies more in our own thinking. The following architecture reference uses clusters in both the WEB layer and the EJB layer:

Java與PHP之間的關聯

memcache is a distributed memory object caching technology. In PHP, it is very convenient to store Session data in it to synchronize each node in the WEB cluster. The data can also reflect PHP's "professionalism" compared to Java in the WEB field.

This article introduces the relationship between Java and PHP. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Summary of practical experience with PHP

##Related summary of ThinkPHP

Related content about thinkphp base class

以上是Java與PHP之間的關聯的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn