PHP actual combat kong to do microservice architecture four (dynamic load balancing)
Preface
Load balancing is a computer network technology. We use it to distribute load among network resources to optimize resource usage and avoid overload.
Load balancing method provided by kong
Kong provides DNS-based methods and Ring-balancer (ring balancer), which can be configured without a DNS server. This chapter will focus on Ring-balancer.
Ring-balancer
When using a ring equalizer, no DNS server is required, and Kong acts as a service registry, adding and deleting services and requests through API.
This circular equalizer is configured through upsteam and target.
In the business, we can collect multiple service addresses and ports to form a target.
The virtual host name is upsteam. Proxy to these targets through virtual hosts.
例如:
target -> 192.168.1.2:8887 192.168.1.2:8888 192.168.1.6:8889
upsteam -> www.lalahost.com
Balancing algorithm
The ring balancer provides the following algorithms:
round-robin, consistent-hashing, and least-connections
algorithm | default | description |
---|---|---|
round-robin | Yes | Provide uniformly distributed weighted polling for targets |
consistent-hashing | Based on consistent hashing to maximize the target hit rate | |
least-connections | Select the target with the least number of connections and weight it according to the weight of the target |
Load balancing
Nginx implementation example
This example can be compared with kong's code, and the effect is consistent.
upstream upstream01 {
server localhost:8888 weight=10;
server localhost:8889 weight=100;
}
server {
listen 80;
location /api/ {
proxy_pass http://upstream01 ;
}
}
Kong ring equalizer implementation
Add upstrram and target
<?php
/**
* @author: 飘逸的罗伯特
*/
//1. 创建名字为 upstream01 的 upstream
$upstream_data = [
'name' => 'upstream01',
];
http_request('http://hz12.cn:8001/upstreams', $upstream_data);
//2. 创建项目对应的target,此处创建两个
$target_data = [
'target' => 'hz12.cn:8888', //服务地址
'weight' => 10 //权重
];
http_request('http://hz12.cn:8001/upstreams/upstream01/targets', $target_data);
$target_data = [
'target' => 'hz12.cn:8889',
'weight' => 100
];
http_request('http://hz12.cn:8001/upstreams/upstream01/targets', $target_data);
/**
* 发送post请求
* @param [string] $url 请求地址
* @param [array] $postdata post参数
* @return [ar] [description]
*/
function http_request($url, $postdata=[]){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec();
curl_close($curl);
return $data;
}
Equivalent to
upstream upstream01 {
server localhost:8888 weight=10;
server localhost:8889 weight=100;
}
Add service and route
<?php
/**
* @author: 飘逸的罗伯特
*/
//创建service
$services_data = [
'name' => 'service01', //服务名称
'host' => 'upstream01' //设置对应的upstream名字
];
http_request('http://hz12.cn:8001/services', $services_data);
//创建route
$route_data = [
'name' => 'route01', //路由名称
'paths' => [
'/api' //可访问服务的路由地址
]
];
http_request('http://hz12.cn:8001/services/service01/routes', $route_data);
/**
* 发送post请求
* @param [string] $url 请求地址
* @param [array] $postdata post参数
* @return [ar] [description]
*/
function http_request($url, $postdata=[]){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$data = curl_exec();
curl_close($curl);
return $data;
}
Equivalent to
server {
listen 80;
location /api/ {
proxy_pass http://upstream01 ;
}
}
running result
准备测试demo
我分别在hz12.cn:8888与hz12.cn:8889服务器上准备测试demo,站点根目录下新建demo.php,内容分别为8888与8889
访问我的服务器 -> http://hz12.cn:8000/api