PHP中间件--ICE
简介:这是PHP中间件--ICE的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=337545' scrolling='no'>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组件,他们的通信结构如下:
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 $releasever - $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
#include
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 }
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 status = 1;
} catch (const char* msg) {
cerr status = 1;
}
if (ic) {
try {
ic->destroy();
} catch (const Ice::Exception& e) {
cerr 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
require 'Ice.php';
require 'Printer.php';
$ic = null;
try
{
$ic = Ice_initialize();
$base = $ic->stringToProxy("SimplePrinter:default -p 10000");
$printer = Demo_PrinterPrxHelper::checkedCast($base);
if(!$printer)
throw new RuntimeException("Invalid proxy");
$printer->printString("Hello World!");
}
catch(Exception $ex)
{
echo $ex;
}
if($ic)
{
// Clean up
try
{
$ic->destroy();
}
catch(Exception $ex)
{
echo $ex;
}
}
?>
H
打开一个终端运行 #./server
打开另一个终端运行 php Client.php
发现每运行一次Client.php,第一个终端就打出一个Hello World. ICE 运行成功.
附注:大型的网站对于ICE的使用是很多的。比如需要实现一个分词搜索的功能使用lucence,对数据库的访问,对memcached的访问都可以直接在ICE中写一个服务来提供统一管理和使用
作者:轩脉刃(yjf512)
出处:(http://www.cnblogs.com/yjf512/)
版权声明:本文的版权归作者与博客园共有。欢迎转载阅读,转载时须注明本文的详细链接。
[参考文章]
http://blog.csdn.net/phphot/archive/2009/04/18/4089806.aspx
http://hi.baidu.com/xdh2571/blog/item/8f01fafc4debfc89b801a04b.html
http://www.zeroc.com/
(特别是里面的Document)
http://blog.csdn.net/cnhome/archive/2008/11/18/3331279.aspx
爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具
http://biancheng.dnbcw.info/php/337545.html pageNo:9
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
