PHP中间件--ICE

ICE(Internet Communications Engine)是Zeroc提供的一款高性能的中间件。使用ICE能使得php(或c++,java,python)与java,c++,.net,python等进行交互。基于ICE可以实现电信级的解决方案。

1第一个问题:为什么要使用中间件?

设想一个这样的场景:对于一个大型网站来说,往往有很多个web服务器,每个web服务器都存在很多对于数据库的操作。如果直接在php程序上直接操作数据库,那么势必要在每台web服务器都配置数据库的用户名,密码等信息,这是极度不安全的。并且如果我们要统一对数据库的操作进行管理和修改等,那么久要去每个web服务器上修改。因此,这时候中间件就产生了。它是基于SOA(面向服务架构)的思想,将对数据库的操作统一成一个服务,放置于一台服务机上,每个web服务器要对数据库进行操作,就可以直接访问这个提供中间件服务的服务器。

还有一点,考虑到性能问题,这里的提供服务的机子我们不使用html和xml传输数据,一般使用TCP,UDP这层的通信。

因此ICE就是现在非常流行的网站开发中间件之一。

关于更多理解为何使用中间件的原因这里有两个链接:

http://blog.csdn.net/phphot/archive/2009/04/18/4089806.aspx

http://hi.baidu.com/xdh2571/blog/item/8f01fafc4debfc89b801a04b.html

2 对于ICE,它是怎么样通信结构?

ICE有分为提供服务的一方Server和寻求服务的一方Client,两台机子上都需要安装ICE组件,他们的通信结构如下:

ICE

Client端应该事先知道Server端能提供的服务是什么,有什么格式?这就是图中的Proxy Code,在Proxy Code中定义好了类和接口。Server端中接口定义的就是Skeleton,具体实现接口的是Server Application,Server Application可以是C++,java,C#等写的,但是ICE不提供PHP写Server端。

3 安装ICE

安装环境: CentOS

1)
cd /etc/yum.repos.d/
wget http://www.zeroc.com/download/Ice/3.4/rhel5/zeroc-ice.repo

2)

编辑zeroc-ice.repo:

[zeroc-ice]

name=Ice 3.4 for Red Hat Enterprise Linux releaseverreleasever−basearch

baseurl=http://www.zeroc.com/download/Ice/3.4/rhel5/$basearch

enabled=1

gpgcheck=1

gpgkey=http://www.zeroc.com/download/RPM-GPG-KEY-zeroc-release

3)

使用yum安装

yum install ice* db46* mcpp-devel

4)

确认机子是否安装g++,如果没有,则安装:

yum install gcc-c++ libstdc++-devel

4 写一个ICE例子,目的:Client端每调用一次服务,Server端就打出一个"hello world”;

基本环境:由于是实验目的,我仅仅将Client和Server同当做一台CentOS机子

A 建文件夹: mkdir ice_demo

B 创建文件Printer.ice,这个.ice文件是ICE的slice文件,在其中定义了服务的对象和接口

module Demo { 
interface Printer { 
void printString(string s); 
}; 
};

C #slice2cpp Printer.ice //产生出了Printer.h和Printer.cpp两个文件

D 创建Server.cpp

#include <Ice/Ice.h> 
#include <Printer.h> 
  
using namespace std; 
using namespace Demo; 
  
class PrinterI : public Printer { 
public: 
virtual void printString(const string& s, 
const Ice::Current&); 
}; 
  
void  
PrinterI:: 
printString(const string& s, const Ice::Current&) 

cout << s << endl; 

  
int 
main(int argc, char* argv[]) 

int status = 0; 
Ice::CommunicatorPtr ic; 
try { 
ic = Ice::initialize(argc, argv); 
Ice::ObjectAdapterPtr adapter 
= ic->createObjectAdapterWithEndpoints( 
"SimplePrinterAdapter", "default -p 10000"); 
Ice::ObjectPtr object = new PrinterI; 
adapter->add(object, 
ic->stringToIdentity("SimplePrinter")); 
adapter->activate(); 
ic->waitForShutdown(); 
} catch (const Ice::Exception& e) { 
cerr << e << endl; 
status = 1; 
} catch (const char* msg) { 
cerr << msg << endl; 
status = 1; 

if (ic) { 
try { 
ic->destroy(); 
} catch (const Ice::Exception& e) { 
cerr << e << endl; 
status = 1; 


return status; 
}

E

#c++ -I. -I$ICE_HOME/include -c Printer.cpp Server.cpp

# c++ -o server Printer.o Server.o -L$ICE_HOME/lib -lIce –lIceUtil   //在同文件夹下会出现:server执行文件

F #slice2php Printer.ice

G 创建Client.php

<?php 
require 'Ice.php'; 
require 'Printer.php';

ic = null;
     try

     {ic = null;     try     {ic = Ice_initialize(); 
base=base=ic->stringToProxy("SimplePrinter:default -p 10000"); 
printer=DemoPrinterPrxHelper::checkedCast(printer=DemoPrinterPrxHelper::checkedCast(base); 
if(!$printer) 
throw new RuntimeException("Invalid proxy");

printer->printString("Hello World!");
     }

     catch(Exceptionprinter->printString("Hello World!");     }     catch(Exceptionex) 

echo $ex; 
}

if(ic)
     {

     // Clean up

     try

     {ic)     {     // Clean up     try     {ic->destroy(); 

catch(Exception ex)

     {

     echoex)     {     echoex; 


?>

H

打开一个终端运行 #./server

打开另一个终端运行 php Client.php

发现每运行一次Client.php,第一个终端就打出一个Hello World. ICE 运行成功.

附注:大型的网站对于ICE的使用是很多的。比如需要实现一个分词搜索的功能使用lucence,对数据库的访问,对memcached的访问都可以直接在ICE中写一个服务来提供统一管理和使用

原文地址:https://www.cnblogs.com/liliuguang/p/9603729.html