PHP middleware-ice

Source: Internet
Author: User
Tags centos server

Introduction: This is the details page of PHP middleware-ice. It introduces the related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 337545 'rolling = 'no'>

Ice (Internet communications engine) is a high-performance middleware provided by zeroc. Using ice allows PHP (or C ++, Java, Python) to interact with Java, C ++,. net, Python, and so on. Based on ice, telecom-level solutions can be implemented.

 

1. Question 1: Why is middleware used?

Imagine a scenario where a large website usually has many web servers and each web server has many database operations. In PHPProgramTo directly operate the database, you must configure the database username, password, and other information on each Web server. This is extremely insecure. And if we want to manage and modify Database Operations in a unified manner, we need to modify the operations on each Web server for a long time. Therefore, middleware is generated at this time. It is based on the SOA (Service-Oriented Architecture) idea. It unifies database operations into a service and places it on a service machine. Each web server must operate the database, you can directly access the server that provides the middleware service.

Another point is that, considering performance issues, we do not use HTML or XML to transmit data for the server that provides services. TCP or UDP communication is generally used.

Therefore, ice is one of the most popular website development middleware.

 

For more information about how to use middleware, there are two links:

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

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

 

2. How is the communication structure of ice?

Ice is divided into a server providing services and a client requesting services. The ice components must be installed on both servers. Their communication structure is as follows:

 

The client should know in advance what services can be provided by the server and what format? This is the proxy code in the figure. Classes and interfaces are defined in the proxy code. The interface on the server is defined as skeleton. The specific implementation interface is server application. The server application can be written in C ++, Java, and C, however, ice does not provide PHP to write data to the server.

 

3. Install ice

Installation environment: centos

 
1)
 
CD/etc/yum. Repos. d/
 
Wget 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 $ 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)

Install with yum

Yum install ice * db46 * MCPP-devel

4)

Check whether the host is installed with G ++. If not, install:

Yum install gcc-C ++ libstdc ++-devel

 

4. Write an ice example. Objective: each time the client calls a service, the server generates a "hello World ";

 

Basic Environment: for experimental purposes, I only use the client and server as a centos server.

 

Create a folder: mkdir ice_demo

 

B. Create the file printer. Ice. The. ICE file is the ice's slice file, in which the service object and interface are defined.

Module demo {

Interface printer {

Void printstring (string S );

};

};

 

C # slice2cpp printer. Ice // generate the printer. h and printer. cpp 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-> createobjectadapterwithenderson points (

"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 // The server execution file appears in the same folder.

 

F # slice2php printer. Ice

G. Create client. php

<? 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

Start a terminal to run #./Server

Open another terminal and run php client. php

 

It is found that every time client. php is run, a hello World. Ice is successfully run on the first terminal.

 

 

Note: large websites use a lot of ice. For example, you need to implement a word segmentation search function using lucence. For database access, access to memcached can directly write a service in ice to provide unified management and use.

 

 

Author: Xuan Mai blade (yjf512)

(Http://www.cnblogs.com/yjf512)

Copyright: The copyright of this article is shared by the author and the blog. Please refer to the following link for more information.

 

[ReferenceArticle]

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

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

Http://www.zeroc.com/

(Especially the document in it)

Http://blog.csdn.net/cnhome/archive/2008/11/18/3331279.aspx

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/337545.html pageno: 9.

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.