PHP Middleware--ice

Source: Internet
Author: User
Tags php write

ICE (Internet Communications Engine) is a high-performance middleware provided by Zeroc. Using ice enables PHP (or C++,java,python) to interact with Java,c++,.net,python and so on. An ice-based solution is available for carrier-class solutions.

1 First question: Why use middleware?

Imagine a scenario where, for a large web site, there are often a lot of Web servers, and each Web server has a lot of operations for the database. If directly in the PHP program directly manipulate the database, it is bound to each Web server to configure the database user name, password and other information, which is extremely disturbed. And if we want to unify the operation of the database management and modification, so long to go to each Web server to modify. Therefore, the middleware is produced at this time. It is based on the idea of SOA (service-oriented architecture), the operation of the database is unified into a service, placed on a server, each Web server to operate the database, you can directly access the service provider of the middleware services.

Also, given the performance issues, we do not use HTML and XML to transmit data, generally using the TCP,UDP layer of communication.

So ice is now one of the most popular web development middleware.

There are two links to more understanding why middleware is used:

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

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

2 for ice, how does it communicate structure?

Ice is divided into the server that provides the service and the client that seeks the service, both of which need to install the ice components, their communication structure is as follows:

The client should know in advance what service the server side can provide and what format it has. This is the proxy code in the diagram, the class and interface are defined in proxy code. The server side of the interface definition is skeleton, the implementation of the interface is the server Application,server application can be c++,java,c# and so on, but ice does not provide PHP write Server side.

3 Installing the Ice

Installation Environment: CentOS

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

2)

Edit Zeroc-ice.repo:

[Zeroc-ice]

Name=ice 3.4 for Red Hat Enterprise linux  R e l e a s e v e r & #x2212; " > Rel ea se ve r? 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)

Installing with Yum

Yum Install ice* db46* mcpp-devel

4)

Verify that the g++ is installed on the machine, and if not, install:

Yum Install gcc-c++ Libstdc++-devel

4 Write an ice example, the purpose: Client side every call service, server side to play a "Hello World";

Basic environment: For experimental purposes, I only use client and server as a CentOS machine

A Build folder: mkdir Ice_demo

B Create file Printer.ice, this. Ice file is an ice slice file that defines the objects and interfaces of the service

Module Demo {
Interface Printer {
void Printstring (string s);
};
};

C #slice2cpp Printer.ice//produced Printer.h and Printer.cpp two files

D Create 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//In the same folder appears: Server execution file

F #slice2php Printer.ice

G Create 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=DEMOPRINTERPrx he lp er ::c Hec ke dc as t ( Printer=demoprinterprxhelper::checkedcast (base);
if (! $printer)
throw new RuntimeException ("Invalid proxy");

Printer->printstring ("Hello world!");
}

catch (Exceptionprinter->printstring ("Hello world!"); } catch (Exceptionex)
{
Echo $ex;
}

IfIc
{

Clean up

Try

{IC) {//Clean up try {Ic->destroy ();
}
catch (Exception ex)

{

Echoex) {echoex;
}
}
?>

H

Open a terminal to run #./server

Open another terminal to run PHP client.php

Discover that each time client.php is run, the first terminal hits a Hello world. ICE runs successfully.

Note: Large websites use a lot of ice. such as the need to implement a Word search function using Lucence, access to the database, access to memcached can be directly in the ice to write a service to provide unified management and use

PHP Middleware--ice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.