PHP Remote invocation and code explanation for RPC framework

Source: Internet
Author: User

Objective

A project, from start to release update, is maintained until the final version. Functions are increasing, the corresponding code volume is also increasing, it means that the project becomes more non-maintainable, at this time, we need to split the way to break up a project, so that the development team to better maintain the project.

Sub-module

This stage, is generally also the initial stage of the project, due to insufficient manpower, a service-side interface project only one development for maintenance, according to the habit of development, the project will be divided into several modules for development, under a project deployment.

The disadvantage of this is that the project becomes non-maintainable as the version is updated.

Sub-item

As the functionality of each module continues to evolve, the code becomes more bloated. At this time the project needs to be split, such as the above diagram, divided into user system projects, payment system projects.

CURL

You'll start by using curl to access external resources.

For example, a message Platform SDK, such as the major third-party provided by the SDK, tangled to source discovery is directly using the Curl function to access the way.

The advantage is that there is no environmental requirement and can be used directly.
The disadvantage is the resource consumption problem of concurrent access.

The HTTP request part of the Sina Weibo SDK source code/** * Make an HTTP requests * * @return string API results * @ignore */functio                n http ($url, $method, $postfields = NULL, $headers = Array ()) {$this->http_info = array ();                $ci = Curl_init ();        /* Curl Settings */curl_setopt ($CI, curlopt_http_version, CURL_HTTP_VERSION_1_0);        curl_setopt ($ci, curlopt_useragent, $this->useragent);        curl_setopt ($ci, Curlopt_connecttimeout, $this->connecttimeout);        curl_setopt ($ci, Curlopt_timeout, $this->timeout);        curl_setopt ($ci, Curlopt_returntransfer, TRUE);        curl_setopt ($ci, Curlopt_encoding, "");                curl_setopt ($ci, Curlopt_ssl_verifypeer, $this->ssl_verifypeer);        if (Version_compare (Phpversion (), ' 5.4.0 ', ' < ')) {curl_setopt ($ci, Curlopt_ssl_verifyhost, 1);        } else {curl_setopt ($ci, Curlopt_ssl_verifyhost, 2); } curl_setopt ($ci, curlopt_headerfunction, array($this, ' GetHeader '));                curl_setopt ($ci, Curlopt_header, FALSE);                                Switch ($method) {case ' POST ': curl_setopt ($ci, Curlopt_post, TRUE);                                        if (!empty ($postfields)) {curl_setopt ($ci, Curlopt_postfields, $postfields);                $this->postdata = $postfields;                            } break;                                Case ' Delete ': curl_setopt ($ci, curlopt_customrequest, ' delete '); if (!empty ($postfields)) {$url = "{$url}?{                $postfields} "; }} if (Isset ($this->access_token) && $this->access_token) $headers [] = "                Authorization:oauth2 ". $this->access_token; if (!empty ($this->remote_ip)) {if (Defined (' Sae_accesskey ')) {$headers [] = "SAEREMOTEIP:".            $this->remote_ip; } else {$headers [] = "API-REMOTEIP:".            $this->remote_ip; }} else {if (!defined (' Sae_accesskey ')) {//$headers [] = "Api-remotei P: ".            $_server[' REMOTE_ADDR '];        }} curl_setopt ($ci, Curlopt_url, $url);        curl_setopt ($ci, Curlopt_httpheader, $headers);                curl_setopt ($ci, Curlinfo_header_out, TRUE);                $response = curl_exec ($CI);                $this->http_code = Curl_getinfo ($ci, Curlinfo_http_code);                $this->http_info = Array_merge ($this->http_info, Curl_getinfo ($CI));                $this->url = $url;            if ($this->debug) {echo "=====post data======\r\n";                        Var_dump ($postfields);            echo "=====headers======\r\n";                        Print_r ($headers); Echo ' =====request info===== '.  \ r \ n ";          Print_r (Curl_getinfo ($CI)); Echo ' =====response===== '. "            \ r \ n ";        Print_r ($response);                } curl_close ($CI);    return $response; }

Rpc

Remote Procedure Call Protocol

RPC (remote Procedure call Protocol)-a remoting protocol that requests services from a remote computer program over a network without needing to know the underlying network technology. The RPC protocol assumes that some transport protocols exist, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport and application tiers. RPC makes it easier to develop applications that include distributed, multi-program networks.

RPC takes client/server mode. The requestor is a client, and the service provider is a server. First, the client call process sends a call message with process parameters to the service process, and then waits for the reply message. On the server side, the process stays asleep until the call information arrives. When a call arrives, the server obtains the process parameters, evaluates the result, sends a reply message, and then waits for the next invocation information, and finally, the client invokes the process to receive the reply message, obtains the process result, and then invokes execution to proceed.

Yar

Bird Brother produced the RPC framework, lightweight framework.

<?phpclass API {    /**     * The DOC info would be a generated automatically into service info page.     * @params     * @return * * Public    function API ($parameter, $option = "foo") {    }    protected function Client_can_not_see () {    }} $service = new Yar_server (new API ()); $service->handle (); >

Calling code

<?php$client = new Yar_client ("http://host/api/"), $result = $client->api ("parameter"); >

Note that the bird brother out of something less documents, need more debugging.

Thrift

Thrift is a software framework for the development of extensible, cross-language services. It combines a powerful software stack and code generation engine to build in C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, node. js, Smalltalk, and OCaml the seamless and efficient service between these programming languages.

The meaning of remote invocation is that different sub-projects can be solved in a more appropriate language and more efficient implementation of the requirements.

At the same time, for the development of the team, it can improve the overall technical level.

Soap

Because the XML used is not much described, after all, it is still more json.

Json-rpc

The following is the standard for return values

--[    {"JSONRPC": "2.0", "method": "Sum", "params": [1,2,4], "id": "1"},    {"JSONRPC": "2.0", "Method": "Notify_ Hello "," params ": [7]},    {" JSONRPC ":" 2.0 "," method ":" Subtract "," params ": [42,23]," id ":" 2 "},    {" foo ":" Boo "},< c4/>{"JSONRPC": "2.0", "Method": "Foo.get", "params": {"name": "Myself"}, "id": "5"},    {"JSONRPC": "2.0", "Method": "G Et_data "," id ":" 9 "}     ]<--[    {" JSONRPC ":" 2.0 "," Result ": 7," id ":" 1 "},    {" JSONRPC ":" 2.0 "," result ": +," I D ":" 2 "},    {" JSONRPC ":" 2.0 "," error ": {" code ": -32600," message ":" Invalid Request "}," id ": null},    {" Jsonrpc ":" 2.0 "," error ": {" code ": -32601," message ":" Method Not Found "}," id ":" 5 "},    {" JSONRPC ":" 2.0 "," Result ": [" Hello ", 5] , "id": "9"}    ]

In fact, you will find that we are providing the client with the return value of the interface, which is done according to this standard.

Accordingly, the service side of the service side of the data reception and return, but also according to this standard to do.

Changes in Project splitting

Project Refinement

A module corresponds to a project in which resource-oriented data access is made between projects through the rest-based interface standard.

Staffing requirements

The premise of a project split is that a project is not sufficient to meet the existing business development requirements, which means that the number of developers after the split has been amplified.

Guerrillas to the regular army to compile the leap!

Document

More projects also means more interface calls to documents, and proper processing of documents to better improve team collaboration efficiency.

Postscript

The remote invocation of the service is how to reasonably save a project that is becoming non-maintainable from the tar pits and increase the overall capacity of the project, but there is no silver bullet in the world.

Related Article

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.