您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

PHP分布式架构RPC介绍以及手写RPC框架

2019/6/8 14:46:44发布92次查看
分布式解决什么问题?
举个例子:当网站有10万个访问,已经没法处理这么多访问请求,通常,我们可以提高服务器的配置,其次我们还可以添加服务器来分流处理,如果一台机器只能处理6万个请求,那么我们在加一台服务器,把请求分配到两台,那么就可以处理10万请求。
加服务器有两种方式实现,一种是用负载均衡的方式,另一种用分布式的方式,负载均衡其实就是把原来的代码复制到另一台服务器,两台服务器的代码是一样的,这也叫水平拆分,分布式是基于业务拆分,把一个项目中的模块分别部署到服务器。是基于服务的拆分,也叫垂直拆分。
rpc介绍
rpc(remote procedure call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。rpc协议假定某些传输协议的存在,如tcp或udp,为通信程序之间携带信息数据。在osi网络通信模型中,rpc跨越了传输层和应用层。rpc使得开发包括网络分布式多程序在内的应用程序更加容易。
rpc 在使用形式上像调用本地函数(或方法)一样去调用远程的函数(或方法)
实现分布式部署 跨语言 。。。
架构演变
mvc架构:当业务规模很小时,将所有功能都不熟在同一个进程中,通过双机或者负载均衡器实现负债分流;此时,分离前后台逻辑的mvc架构是关键。
prc架构:当垂直应用越来越多,应用之间交互不可避免,将核心和公共业务抽取出来,作为独立的服务,实现前后台逻辑分离。此时,用于提高业务复用及拆分的rpc框架是关键。
soa架构:随着业务发展,服务数量越来越多,服务生命周期管控和运行态的治理成为瓶颈,此时用于提升服务质量的soa服务治理是关键。
微服务架构:通过服务的原子化拆分,以及微服务的独立打包、部署和升级,小团队的交付周期将缩短,运维成本也将大幅度下降。
rpc与rest区别
rest,即representational state transfer的缩写。翻译过来是表现层状态转换
rest 通常采用 )、服务消费方(client)调用以本地调用方式调用服务;
2)、client stub接收到调用后负责将方法、参数等组装成能够进行网络传输的消息体;
3)、client stub找到服务地址,通过socket将消息发送到服务端;
4)、server stub收到消息后进行解码;
5)、server stub根据解码结果调用服务端本地的服务;
6)、本地服务执行并将结果返回给server stub;
7)、server stub将返回结果打包成消息;
8)、server stub通过socket将消息发送至客户端;
9)、client stub接收到消息,并进行解码;
10)、服务消费方(rpc client)得到最终的服务调用结果。
手写简单的rpc框架
server.php
class server{
public $socket;
public $class;
public $method;
public $param;
public $servicedir=service;
public function __construct($ip,$port){
$this->create($ip,$port);
while(true){
$consock = socket_accept($this->socket);
$protocol = socket_read($consock,2048);
$this->doprotocol($protocol);
$file = $this->servicedir./.$this->class..php;
if(file_exists($file)){
require_once $file;
$obj = new $this->class;
$method = $this->method;
$res = $obj->$method($this->param);
}else{
$res = $file. not exists!!;
}
socket_write($consock,$res,strlen($res));
socket_close($consock);
}
}
private function doprotocol($protocol){
preg_match(/rpc-class:(.*)/,$protocol,$class);
preg_match(/rpc-method:(.*)/,$protocol,$method);
preg_match(/rpc-param:(.*)/,$protocol,$param);
$this->class = $class[1];
$this->method = $method[1];
$this->param = $param[1];
}
private function create($ip,$port){
$this->socket = socket_create(af_inet,sock_stream,sol_tcp);
socket_set_option($this->socket,sol_socket,so_reuseaddr,true);
socket_bind($this->socket,$ip,$port);
socket_listen($this->socket);
}
}
new server(0,6666);
client.php
class client{
public $host;
public $ip;
public $class;
public $socket;
public $protocol = null;
public function __construct($url){
$url = parse_url($url);
$this->host = $url[host];
$this->port = $url[port];
$this->class = basename($url[path]);
$this->connect();
}
private function connect(){
$this->socket = socket_create(af_inet,sock_stream,sol_tcp);
socket_connect($this->socket,$this->host,$this->port);
}
public function __call($method,$param){
$param = json_encode($param);
$this->protocol .=rpc-class:$this->class\n;
$this->protocol .=rpc-method:$method\n;
$this->protocol .=rpc-param:$param\n;
socket_write($this->socket,$this->protocol,strlen($this->protocol));
$data = socket_read($this->socket,2048);
//socket_close($this->socket);
return $data;
}
}
$param = [name=>lampol,age=>22];
$client = new client(.0.0.1:6666/test);
echo $client->test2($param);
先运行服务端 php server.php
在运行客户端 php client.php
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录