前言
一个项目,从开始到版本更新,一直到最后的版本维护。功能在不断增多,对应的代码量也在不断增加,也就意味着项目变得更不可维护,这时候,我们需要用拆分的方式将一个项目打散,以便开发团队更好的对项目进行维护。
分模块这个阶段,一般也是项目的初级阶段,由于人手不够,一个服务端的接口项目只有一个开发进行维护,根据开发的习惯,会把项目分成若干个模块进行开发,在一个项目下进行部署。
这样做的缺点在于项目会随着版本更新而变得不可维护。
分项目随着每个模块功能的不断完善,代码变得更加臃肿。这时候需要对项目进行拆分,比如上面的图,分成用户体系项目、支付体系项目。
curl开始大家会采用curl的方式对外部资源进行访问。
比如某短信平台sdk,比如各大第三方提供的sdk,纠结到源码发现都是直接采用curl函数的方式进行访问。
优点在于没有环境要求,能直接用。
缺点在于并发访问的资源占用问题。
//新浪微博sdk的http请求部分源码
/**
* make an http request
*
* @return string api results
* @ignore
*/
function 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-remoteip: " . $_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远程过程调用协议
rpc(remote procedure call protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。rpc协议假定某些传输协议的存在,如tcp或udp,为通信程序之间携带信息数据。在osi网络通信模型中,rpc跨越了传输层和应用层。rpc使得开发包括网络分布式多程序在内的应用程序更加容易。
rpc采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。
yar鸟哥出品的rpc框架,轻量级框架。
<?phpclass api {
/**
* the doc info will be 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();?>
调用代码
<?php$client = new yar_client("http://host/api/");$result = $client->api("parameter);
?>
注意的是鸟哥出的东西文档比较少,需要多调试。
thriftthrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 c++, java, python, php, ruby, erlang, perl, haskell, c#, cocoa, javascript, node.js, smalltalk, and ocaml 这些编程语言间无缝结合的、高效的服务。
远程调用的意义在于,不同的子项目可以用更适合自己的语言来解决,更有效率的实现需求。
同时,对团队的开发来讲,更能提高整体的技术水平。
soap由于用的xml就不多描述了,毕竟还是json用的多。
json-rpc下面是返回值的标准
--> [
{"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"},
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
]
<-- [
{"jsonrpc": "2.0", "result": 7, "id": "1"},
{"jsonrpc": "2.0", "result": 19, "id": "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"}
]
实际上你会发现我们在给客户端提供接口的返回值,就是按照这个标准来做的。
相应的,服务端对服务端的数据接收和返回,也要同样按照这个标准来做。
项目拆分带来的变化项目细化一个模块对应一个项目,项目之间通过基于rest的接口标准进行面向资源的数据访问。
人员需求项目拆分的前提是一个项目不足以满足现有的业务发展要求,也就意味着拆分之后的开发人员数量的扩增。
游击队向正规军编制的跨越!
文档更多的项目也就意味着更多的接口调用文档,适当的处理文档才能更好的提高团队协作效率。
后记服务的远程调用在于怎么合理的把一个正在变得不可维护的项目从焦油坑中解救出来,并提高项目整体能承载的业务量,不过,世界上没有银弹。
以上就是php远程调用以及rpc框架的代码详解(图)的详细内容。