集群,分布式,架构,微服务
集群,分布式,微服务
集群:相同的服务;
多台服务器部署相同应用构成一个集群;
作用:通过负载均衡设备共同对外提供服务
1,负载均衡
#定义集群
upstream demo{
server localhost:1111;
server localhost:2222;
}
使用集群(在虚拟空间里)
location / {
proxy_pass http://demo;
}
2,分布式:对业务就行拆分,然后分发到不同的机子上执行
举个例子:当网站有10万个访问,已经没法处理这么多访问请求,通常,我们可以提高服务器的配置,其次我们还可以添加服务器来分流处理,如果一台机器只能处理6万个请求,那么我们在加一台服务器,把请求分配到两台,那么就可以处理10万请求。
加服务器有两种方式实现,一种是用负载均衡的方式,另一种用分布式的方式,负载均衡其实就是把原来的代码复制到另一台服务器,两台服务器的代码是一样的,这也叫水平拆分,分布式是基于业务拆分,把一个项目中的模块分别部署到服务器。是基于服务的拆分,也叫垂直拆分。
PRC架构:当垂直应用越来越多,应用之间交互不可避免,将核心和公共业务抽取出来,作为独立的服务,实现前后台逻辑分离。此时,用于提高业务复用及拆分的RPC框架是关键。rest(http,json),restful(resource内调用接口)是一种特殊的prc,
分布式个节点(服务器)直接以网络进行通讯,可以用tcp协议,http协议,各个节点(服务器)无语言,要求,只需挂起tcp服务器,或http服务器,统一对外服务,响应数据可以是json,xml,序列化,==
过程:
1)、服务消费方(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)得到最终的服务调用结果。
总而言之,一个服务器,对请求进行解析之后,分发到这个请求的业务服务器上,由业务服务器返回数据给分发数据库,再由分发服务器返给用户
代码如下:
server.php
<?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
<?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('http://127.0.0.1:6666/Test');
echo $client->test2($param);
先运行服务端 php server.php
在运行客户端 php client.php
4,微服务:一种特殊的分布式,业务拆分更细,由于粒度更多,导致没有足够的服务器,所以微服务允许把几个业务放到同一个服务器,可以是虚拟空间,容器(docker)等
浙公网安备 33010602011771号