Home > Backend Development > PHP Tutorial > Real-time heartbeat detection using PHP and Keepalive

Real-time heartbeat detection using PHP and Keepalive

WBOY
Release: 2023-06-28 09:06:02
Original
1979 people have browsed it

With the development of Internet technology, computer systems need to be continuously optimized and improved to ensure high availability of the system. Among them, heartbeat detection is a very important function, used to detect whether the server is online and whether the network is normal. In this article, we will introduce how to implement real-time heartbeat detection using PHP and Keepalive.

1. Introduction to Keepalive

Before introducing Keepalive, let us first understand the three-way handshake process of TCP. The establishment of a TCP connection requires a three-way handshake, that is, the client sends a SYN packet to the server, the server sends a SYN ACK packet after receiving it, and finally the client sends another ACK packet, so that the connection is successfully established.

When the client sends a request, if the server does not respond, the client will think that the connection has been disconnected. However, sometimes the server cannot respond due to network fluctuations or busy server. At this time, disconnection will affect the user experience. Therefore, a mechanism is needed to keep the connection alive. This is Keepalive.

Keepalive is a network protocol built on top of TCP that is used to detect whether a connection is still alive. When a connection is idle for a period of time, Keepalive will send a detection packet to the server. If the server still does not respond, it will determine that the connection has been disconnected.

2. Use PHP to implement heartbeat detection

Two scripts are needed to implement heartbeat detection. One is the client script (Client.php), which is used to send heartbeat requests to the server regularly, and the other is A server script (Server.php) that receives heartbeat requests and responds with appropriate responses. Let's take a look at the specific implementation.

Client script (Client.php)

<?php
while(true) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://localhost:8000/');//服务器地址
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_exec($ch);
  curl_close($ch);
  sleep(5);//每 5 秒发送一次心跳请求
}
?>
Copy after login

In the above code, curl is used to send HTTP requests, regularly send heartbeat requests to the server, and the sleep() function is used to implement scheduled sending.

Server script (Server.php)

<?php
header('Content-Type: text/plain');
echo "OK
";//响应成功信息
?>
Copy after login

In the above code, the server script responds with a simple success message after receiving the request sent by the client.

In order to keep the server running, you can use server software similar to Nginx or Apache as the running environment for server scripts.

3. Use Keepalive to implement heartbeat detection

Using Keepalive to implement heartbeat detection requires the installation of Keepalive software and related configurations. Under the CentOS system, you can install Keepalive through the following command:

sudo yum install ipvsadm keepalived -y

The following is an example of the Keepalive configuration file (keepalived.conf):

vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 101
  authentication {
    auth_type PASS
    auth_pass 123456
  }
  virtual_ipaddress {
    192.168.10.10
  }
  track_script {
    chk_http_port
  }
}

vrrp_script chk_http_port {
  script "/usr/local/bin/alive.sh"
  interval 5
  weight 2
}

virtual_server 192.168.10.10 80 {
  delay_loop 6
  lb_algo rr
  lb_kind DR
  protocol TCP
  real_server 192.168.10.11 80 {
    weight 1
    TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 80
    }
  }
}
Copy after login

The above configuration file defines a VRRP instance (VI_1) with the status of MASTER; defines a virtual IP address (192.168.10.10), and sets a tracking script (chk_http_port) as a heartbeat detection script; finally defines a virtual server (IP address is 192.168.10.10, port is 80), and a real server is defined (IP address is 192.168.10.11, port is 80).

The following is an example of a tracking script (alive.sh):

#!/bin/bash
A=`curl -I -s -o /dev/null http://localhost`
if [ "$?" != "0" ] || [ "$A" == "" ]; then
  /etc/init.d/keepalived stop
fi
exit 0
Copy after login

The above script is used to detect whether the server is alive. If the server cannot respond or responds with an error, Keepalive will be stopped.

4. Summary

In this article, we introduced how to use PHP and Keepalive to implement real-time heartbeat detection. PHP can send HTTP requests through curl to simulate heartbeat requests, while Keepalive can implement heartbeat detection through VRRP and IPVS. This method can improve the high availability of network applications and monitor server status in real time. It is a very useful network tool.

The above is the detailed content of Real-time heartbeat detection using PHP and Keepalive. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template