easyswoole用ioc重写系统日志类

easyswoole 3.3.7

定义自己的日志类

<?php

namespace App\Ioc;

use EasySwoole\Log\LoggerInterface;
use EasySwoole\Log\Logger;

class Log implements LoggerInterface
{

    private $logger;

    function __construct()
    {
        $this->logger = new Logger(EASYSWOOLE_LOG_DIR);
    }

    function log(?string $msg,int $logLevel = self::LOG_LEVEL_INFO,string $category = 'debug')
    {
        return $this->logger->log($msg, $logLevel, $category);
    }

    function console(?string $msg,int $logLevel = self::LOG_LEVEL_INFO,string $category = 'debug')
    {
        var_dump($msg);
        $this->logger->console($msg, $logLevel, $category);
    }
}

IOC覆盖系统日志类

项目根目录创建文件 bootstrap.php

<?php

use App\Ioc\Log;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\SysConst;

Di::getInstance()->set(SysConst::LOGGER_HANDLER, Log::class);

分析

EasySwoole\EasySwoole\Core.php

查看源码中的registerErrorHandler方法

....
        //初始化配置Logger
        $logger = Di::getInstance()->get(SysConst::LOGGER_HANDLER);
        if(!$logger instanceof LoggerInterface){
            $logger = new DefaultLogger(EASYSWOOLE_LOG_DIR);
        }
        Logger::getInstance($logger);

        //初始化追追踪器
        $trigger = Di::getInstance()->get(SysConst::TRIGGER_HANDLER);
        if(!$trigger instanceof TriggerInterface){
            $trigger = new DefaultTrigger(Logger::getInstance());
        }
        Trigger::getInstance($trigger);

        //在没有配置自定义错误处理器的情况下,转化为trigger处理
        $errorHandler = Di::getInstance()->get(SysConst::ERROR_HANDLER);
        if(!is_callable($errorHandler)){
            $errorHandler = function($errorCode, $description, $file = null, $line = null){
                $l = new Location();
                $l->setFile($file);
                $l->setLine($line);
                Trigger::getInstance()->error($description,$errorCode,$l);
            };
        }
        set_error_handler($errorHandler);
....

查看 easyswoole-事件文档

查看 easyswoole-生命周期

easyswoole之消息队列nqs

easyswoole版本: 3.3.7

文档提供有安装和docker-compose示例代码,启动的时候会报告错误(如果你不调试可能也看不到错误).

错误1: TOPIC_NOT_FOUND 你需要先创建一个topic 登录到nqs中创建

错误2: UPSTREAM_ERROR: Failed to query any nsqlookupd 这个时候应该是没有数据需要执行命令 curl -d 'hello world 2' 'http://127.0.0.1:4151/pub?topic=test'

错误3: [2020-06-24 09:28:50][debug][warning]:[Swoole\Client::recv(): recv() failed. Error: Resource temporarily unavailable [11] at file:/var/www/eseckill/vendor/easyswoole/nsq/src/Connection/AbstractMonitor.php line:52 作者回应是断线问题, 需要自己处理断线重连.

相关资料

官方文档
github-nqs

easyswoole自义命令加载自定义配置文件

需求: 目前框架默认只会加载两个配置文件 dev.php 和 produce.php 只有本地开发和线上生产两个环境, 但是现在需要一个线上测试环境的配置.

版本: easyswoole-3.3.7

loadEnv 加载配置文件的方法

启动命令

启动命令会执行loadEnv方法, 我们自己复制Command/DefaultCommand/Start.php 文件加载自己的配置文件覆盖之前的配置

在自己的项目中 App/Command/test.php 增加一个文件 如何自定义命令点我

<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2019-01-24
 * Time: 23:44
 */

namespace EasySwoole\EasySwoole\Command\DefaultCommand;

use EasySwoole\EasySwoole\Command\CommandInterface;
use EasySwoole\EasySwoole\Command\Utility;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\Core;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\EasySwoole\SysConst;

class Start implements CommandInterface
{

    public function commandName(): string
    {
        // TODO: Implement commandName() method.
        return 'start';
    }

    public function exec(array $args): ?string
    {
        // TODO: Implement exec() method.
        Utility::opCacheClear();
        $response = Utility::easySwooleLog();
        $mode = 'develop';
        if (!Core::getInstance()->isDev()) {
            $mode = 'produce';
        }
        $conf = Config::getInstance();
        if (in_array("d", $args) || in_array("daemonize", $args)) {
            $conf->setConf("MAIN_SERVER.SETTING.daemonize", true);
        }
        //create main Server
        Core::getInstance()->globalInitialize()->createServer();
        //重点这一行
        Config::getInstance()->loadEnv( EASYSWOOLE_ROOT.'/test.php');
        $serverType = $conf->getConf('MAIN_SERVER.SERVER_TYPE');
        switch ($serverType) {
            case EASYSWOOLE_SERVER:
                {
                    $serverType = 'SWOOLE_SERVER';
                    break;
                }
            case EASYSWOOLE_WEB_SERVER:
                {
                    $serverType = 'SWOOLE_WEB';
                    break;
                }
            case EASYSWOOLE_WEB_SOCKET_SERVER:
                {
                    $serverType = 'SWOOLE_WEB_SOCKET';
                    break;
                }
            case EASYSWOOLE_REDIS_SERVER:
                {
                    $serverType = 'SWOOLE_REDIS';
                    break;
                }
            default:
                {
                    $serverType = 'UNKNOWN';
                }
        }
        $response = $response . Utility::displayItem('main server', $serverType) . "\n";
        $response = $response . Utility::displayItem('listen address', $conf->getConf('MAIN_SERVER.LISTEN_ADDRESS')) . "\n";
        $response = $response . Utility::displayItem('listen port', $conf->getConf('MAIN_SERVER.PORT')) . "\n";
        $list = ServerManager::getInstance()->getSubServerRegister();
        $index = 1;
        foreach ($list as $serverName => $item) {
            if (empty($item['setting'])) {
                $type = $serverType;
            } else {
                $type = $item['type'] % 2 > 0 ? 'SWOOLE_TCP' : 'SWOOLE_UDP';
            }
            $response = $response . Utility::displayItem("sub server:{$serverName}", "{$type}@{$item['listenAddress']}:{$item['port']}") . "\n";
            $index++;
        }
        $ips = swoole_get_local_ip();
        foreach ($ips as $eth => $val) {
            $response = $response . Utility::displayItem('ip@' . $eth, $val) . "\n";
        }

        $data = $conf->getConf('MAIN_SERVER.SETTING');
        if(empty($data['user'])){
            $data['user'] = get_current_user();
        }

        if(!isset($data['daemonize'])){
            $data['daemonize'] = false;
        }

        foreach ($data as $key => $datum){
            $response = $response . Utility::displayItem($key,$datum) . "\n";
        }

        $response = $response . Utility::displayItem('swoole version', phpversion('swoole')) . "\n";
        $response = $response . Utility::displayItem('php version', phpversion()) . "\n";
        $response = $response . Utility::displayItem('easy swoole', SysConst::EASYSWOOLE_VERSION) . "\n";
        $response = $response . Utility::displayItem('develop/produce', $mode) . "\n";
        $response = $response . Utility::displayItem('temp dir', EASYSWOOLE_TEMP_DIR) . "\n";
        $response = $response . Utility::displayItem('log dir', EASYSWOOLE_LOG_DIR) . "\n";
        echo $response;
        Core::getInstance()->start();
        return null;
    }

    public function help(array $args): ?string
    {
        // TODO: Implement help() method.
        $logo = Utility::easySwooleLog();
        return $logo . <<<HELP_START
\e[33mOperation:\e[0m
\e[31m  php easyswoole start [arg1] [arg2]\e[0m
\e[33mIntro:\e[0m
\e[36m  to start current easyswoole server \e[0m
\e[33mArg:\e[0m
\e[32m  daemonize \e[0m                   run in daemonize
\e[32m  produce \e[0m                     load produce.php
HELP_START;
    }
}

mvn 编码 GBK 的不可映射字符

问题提示为字符编码错误 要么是文件中的编码问题要么就是IDEA要么就是CMD的编码问题

出现这个问题是自己的mvn没有配置环境变量 那么默认就是使用GBK编码方式. 所以当项目中出现有中文字符就换报这个BGK不可映射字符问题

(方法1)设置环境变量(全局配置)

变量名 MAVEN_OPTS
变量值 -Xms256m -Xmx512m -Dfile.encoding=UTF-8
保存,退出cmd.重新打开cmd 运行mvn -v命令即可

(方法2)配置pom.xml(需要对每个模块配置)

在配置pom.xml加入下面一行配置项

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.hconec.cn</groupId>
        <artifactId>hconec-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxxx.cn</groupId>
    <artifactId>xxxx-common-php</artifactId>
    <name>xxxx-common-php</name>
    <description>sdfsdfsdf</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xxxx.cn</groupId>
            <artifactId>xxxx-common-core</artifactId>
        </dependency>
    </dependencies>

</project>

相关资料

修改maven的编码格式为utf-8
maven编码gbk的不可映射字符解决办法

nginx中使用proxy_pass指向域名缓存问题

如果nginx中的proxy_pass指向的域名然后域名指向的IP发生了变化
那么就需要重启nginx或配置nginx的resolver

server {
    listen   80;
    server_name ligang.gdemo.com;

    resolver 8.8.8.8 valid=60s;
    resolver_timeout 3s;

    set $gproxy "ligang.proxy.gdemo.com";

    location / {
     proxy_pass http://$gproxy;
    }
  }

相关资料

nginx中proxy_pass的使用问题
proxy_pass域名解析引发的故障

kafka

简单使用

  1. 创建一个topic
    kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  2. 创建一个消费者
    kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
  3. 创建一个消息生产者
    kafka-console-producer.sh --broker-list localhost:9092 --topic test
  4. 查看消费组
    kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --list
  5. 查看消费情况
    kafka-consumer-groups.sh --describe --bootstrap-server 127.0.0.1:9092 --group 5f575fb319a4d7.98342076

为什么需要中间件

想象一个场景,你的一个创建订单的操作,在订单创建完成之后,需要触发一系列其他的操作,比如进行用户订单数据的统计、给用户发送短信、给用户发送邮件等等

createOrder(...){
 ...
 statOrderData(...);
 sendSMS();
 sendEmail();
}

官方资料

中文手册
配置说明
命令操作
PHP客户端

相关资料

kafka docker-compose
es-docker-compose
kafka入门使用

Kafka简明教程