laravel测试连接可用性

Posted willem_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel测试连接可用性相关的知识,希望对你有一定的参考价值。

laravel测试连接可用性

创建命令

php artisan make:command Testing

在这里插入图片描述

Testing.php

<?php

namespace App\\Console\\Commands;

use Illuminate\\Console\\Command;
use Illuminate\\Support\\Facades\\Config;
use Illuminate\\Support\\Facades\\Redis;

class Testing extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'connections';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '连接测试';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->testDB();
        $this->info('----------------------- 华丽的分割线 ------------------------');
        $this->testRedis();
    }

    public function testRedis()
    {
        $RedisCons = Config::get("database.redis");
        $this->warn("= = = == = = = = = = =   Redis Start = = = = = = = = = = = = ");
        foreach ($RedisCons as $key => $val) {
            if (is_array($val)) {
                $this->warn($key);
                try {
                    if (isset($val['host']) && isset($val['port'])) {
                        $host = $val['host'];
                        $port = $val['port'];
                        $this->telnetIpPort($host, $port);
                        $this->pingAddress($host);
                        $msg = Redis::connection("{$key}")->ping();
                        $this->info("{$key} ===>>> {$msg}");
                        $this->info("---------------------------------------------------------------------------");
                    }
                }
                catch (\\Exception $e) {
                    $this->error("{$key} ===>>> {$e->getMessage()}");
                }
            }
        }
        $this->warn("= = = == = = = = = = =   Redis End= = = = = = = = = = = = ");
    }

    public function testDB()
    {
        $DbCons = Config::get("database.connections");
        $this->warn("= = = == = =  = = = ==   DB/Mongo Start = = = = = = = = = = = = ");
        foreach ($DbCons as $key => $val) {
            if (is_array($val)) {
                if (isset($val['host']) && isset($val['port'])) {
                    $this->warn($key);
                    $host = $val['host'];
                    $port = $val['port'];
                    try {
                        $this->telnetIpPort($host, $port);
                        $this->pingAddress($host);
                    }
                    catch (\\Exception $e) {
                        $this->error("{$key} ===>>> {$e->getMessage()}");
                    }
                    $this->info("-------------------------------------------------------------------------------");
                }
            }
        }
        $this->warn("= = = == = = = = = = =   DB/Mongo End= = =  = = = == = = = = ");
    }

    /**
     * 使用PHP检测能否ping通IP或域名
     * @param type $address
     * @return boolean
     */
    function pingAddress($address)
    {
        $status     = -1;
        $pingresult = "";
        if (strcasecmp(PHP_OS, 'WINNT') === 0) {
            // Windows 服务器下
            $pingresult = exec("ping -n 1 {$address}", $outcome, $status);
        }
        elseif (strcasecmp(PHP_OS, 'Linux') === 0) {
            // Linux 服务器下
            $pingresult = exec("ping -c 1 {$address}", $outcome, $status);
        }
        else {
            $pingresult = exec("ping -c 1 {$address}", $outcome, $status);
        }
        $this->info("PING ==>> { $address } +++ CODE[{$status}] _ MSG[{$pingresult}] ");
    }

    function telnetIpPort($ip, $port)
    {
        try {
            $socket    = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2, "usec"=>0 ) );
            socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>3, "usec"=>0 ) );
            $errorcode = socket_last_error();
            if ($errorcode > 0) {
                $errormsg = socket_strerror($errorcode);
                throw new \\Exception($errormsg);
            }
            $connect_res = socket_connect($socket, $ip, $port);
            if (!$connect_res) {
                throw new \\Exception("连接失败");
            }
            $send_msg_res = socket_write($socket, "Test", strlen("Test"));
            if ($send_msg_res === false) {
                throw new \\Exception("发送测试数据失败");
            }
            $this->info("Telnet {$ip}:{$port}  is OK ,msgLen:{$send_msg_res}");
            socket_close($socket);
            return;
        }
        catch (\\Exception $e) {
            $this->error("Telnet {$ip}:{$port}  is Err[{$e->getMessage()}]");
            return;
        }
    }
}

执行命令

php artisan connections

在这里插入图片描述

以上是关于laravel测试连接可用性的主要内容,如果未能解决你的问题,请参考以下文章

laravel特殊功能代码片段集合

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

Predis 与 laravel 5.5 “Aggregate/RedisCluster.php:337 中的池中没有可用连接”

如何在新浪云sae上安装部署laravel5.1应用并测试数据库连接

Laravel:如何在控制器的几种方法中重用代码片段