Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), adjust the default character set to: utf8mb4, the adjustment of the interface response format, the empty array is automatically converted to empty objects, and the request log message is collected in the interface application (1 request corresponds to 1 log message) to the database, and the corresponding interface to implement the log function: log list (set data filter to enable filter processing), log details (2)

在开发环境中,执行数据库迁移命令失败:1071 Specified key was too long; max key length is 767 bytes

Yii2 Advanced in Action Series: RESTful API + RPC Services + Internationalization + Logging & Testing

新建 api 目录、配置和环境、测试、Vagrant 等的支持。

(1) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), support for creating new API directories, configuration and environment, testing, vagrant, etc.

(2) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), implement model hierarchy: data layer, logic layer, clarify common directory, application, module inheritance, reference relationship

在 Postman 中,GET http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users ,200响应,message为简体中文

(3) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), implement RESTful web service, support internationalization (drivingly set the target language, default to simplified Chinese)

运行测试,获取详细的输出,可看到一步一步的行为报告,符合预期

(4) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), prepare some examples of automated tests (API tests) for user-related operations in the tests directory of the API to ensure that the application will not affect existing functions when changing or adding new functions

在 Postman 中,GET http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/logs?filter[level]=4&filter[category][like]=api\behaviors\RequestLogBehavior&filter[prefix][like]=app-api&filter[log_time][gte]=1528090828&filter[log_time][lte]=1529564924.6648 ,200响应,空数组已经被自动转换为空对象,以保证字段格式的统一,GET http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/logs/3

(5) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), adjust the default character set to: utf8mb4, the adjustment of the interface response format, the empty array is automatically converted to empty objects, and the request log message is collected in the interface application (1 request corresponds to 1 log message) to the database, and the corresponding interface to implement the log function: log list (set data filter to enable filter processing), log details

在开发环境中,执行数据库迁移命令失败:1071 Specified key was too long; max key length is 767 bytes

(6) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), adjust the default character set to: utf8mb4, the adjustment of the interface response format, the empty array is automatically converted to empty objects, and the request log message is collected in the interface application (1 request corresponds to 1 log message) to the database, and the corresponding interface to implement the log function: log list (set data filter to enable filter processing), log details (2)

浏览 user 表数据,status、updated_at 已经更新

(7) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), realize the soft deletion of ActiveRecord, generate ActiveQuery, and customize the query class

执行数据库迁移命令,查看数据库表 user 结构,符合预期

(8) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), realize the soft deletion of ActiveRecord, generate ActiveQuery, and customize the query class (2)

在 Postman 中 POST 请求,http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users ,输入格式:application/json

(9) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), application/x-www-form-urlencoded and multipart/form-data input formats, default support, new application/json input format support

打开 Windows PowerShell,执行 init 命令并选择 dev 作为环境,api应用所需环境配置文件自动生成

(10) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new remote procedure call application (implement hprose 2.0 for PHP’s RPC server), support for creating new RPC directories, configuration and environment, testing, Vagrant, etc.

出现以上原因是因为 Windows 下 nginx php 环境,不支持并发的原因,当同时访问多个域名,并且同时指向你本地服务的时候,就不支持并发了。nginx.conf 里面 对不同 server 修改 fastcgi_pass 的端口号,启动多个 php-cgi。

(11) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new remote procedure call application (implement hprose 2.0 for PHP RPC server), implement the corresponding RPC service of the page in the RPC directory, and implement the RPC client in the API directory

执行数据库迁移,查看表结构,基于 Gii 重新生成模型文件

(12) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, the implementation of uuid

由于日志消息是存储于 MySQL 的 log 表中,在生产环境中,累积了大量的数据,log 表的数据长度已经达到 数百 GB ,因此,决定基于控制台命令行实现日志消息的定时删除,开发环境的 log 表的数据长度为 14.64 GB

(13) Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, automatic timing deletion of log messages

1. In the development environment, the execution of the database migration command fails: 1071 Specified key was too long; Max key length is 767 bytes, as shown in Figure 17
在开发环境中,执行数据库迁移命令失败:1071 Specified key was too long; max key length is 767 bytes
Figure 17


[root@45fdb670c7c4 /]# php /sobey/www/pcs-api/yii migrate --interactive=0
Yii Migration Tool (based on Yii v2.0.15.1)

Total 1 new migration to be applied:
        m180620_105204_update_table_options_to_log

*** applying m180620_105204_update_table_options_to_log
    > execute SQL: ALTER TABLE {{%user}} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ...Exception: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
The SQL being executed was: ALTER TABLE `pa_user` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci (/sobey/www/pcs-api/vendor/yiisoft/yii2/db/Schema.php:664)
#0 /sobey/www/pcs-api/vendor/yiisoft/yii2/db/Command.php(1263): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE `pa...')
#1 /sobey/www/pcs-api/vendor/yiisoft/yii2/db/Command.php(1075): yii\db\Command->internalExecute('ALTER TABLE `pa...')
#2 /sobey/www/pcs-api/vendor/yiisoft/yii2/db/Migration.php(219): yii\db\Command->execute()
#3 /sobey/www/pcs-api/console/migrations/m180620_105204_update_table_options_to_log.php(19): yii\db\Migration->execute('ALTER TABLE {{%...')
#4 /sobey/www/pcs-api/vendor/yiisoft/yii2/db/Migration.php(114): m180620_105204_update_table_options_to_log->safeUp()
#5 /sobey/www/pcs-api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(725): yii\db\Migration->up()
#6 /sobey/www/pcs-api/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(199): yii\console\controllers\BaseMigrateController->migrateUp('m180620_105204_...')
#7 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#8 /sobey/www/pcs-api/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#9 /sobey/www/pcs-api/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#10 /sobey/www/pcs-api/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('', Array)
#11 /sobey/www/pcs-api/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('', Array)
#12 /sobey/www/pcs-api/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate', Array)
#13 /sobey/www/pcs-api/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate', Array)
#14 /sobey/www/pcs-api/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#15 /sobey/www/pcs-api/yii(23): yii\base\Application->run()
#16 {main}
*** failed to apply m180620_105204_update_table_options_to_log (time: 0.008s)


0 from 1 migrations were applied.

Migration failed. The rest of the migrations are canceled.


2. Run sql in the database in the development environment, and report an error: #1071 – specific key was too long; max key length is 767 bytes, as shown in Figure 18
在开发环境中的数据库中运行SQL,报错:#1071 - Specified key was too long; max key length is 767 bytes
18
3. If you use utf8mb4 and have a unique index on a varchar column with a length of more than 191 characters, you need to open innod b_large_prefix to allow larger columns in the index, since utf8mb4 requires more storage space than utf8 or latin1. If innodb_large_prefix (default) is enabled, the index key prefix is limited to 3072 bytes for InnoDB tables that use dynamic or compressed row format. If innodb_large_prefix is disabled, the index key prefix of any row format table is limited to 767 bytes. The successful execution of the migration command in the local environment is because the mysql version of the local environment is 5.7.19 (mysql 5.7.7 InnoDB_Large_Prefix is enabled by default, InnoDB_Large_Prefix is deprecated in MySQL 5.7.7 and will be deleted in future releases.), and the development environment is 5.6.16, check the URL: https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html , as shown in Figure 19
如果使用的是utf8mb4,并且在长度超过191个字符的varchar列上有唯一索引,则需要打开innodb_large_prefix以允许索引中的较大列,因为utf8mb4需要比utf8或latin1更多的存储空间。
Figure 19
4. Parameter configuration of Alibaba Cloud RDS, enable InnoDB_Large_Prefix, as shown in Figure 20
阿里云RDS的参数配置,启用Innodb_large_prefix
Figure 20
5. Run sql in the database in the development environment, success, as shown in Figure 21
在开发环境中的数据库中运行SQL,成功
Figure 21


ALTER DATABASE `pcs-api-dev` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci
ALTER TABLE pa_user ROW_FORMAT=DYNAMIC
ALTER TABLE pa_user CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci


Note: Some databases may also need to run


set global innodb_file_format = BARRACUDA;


6. Edit the database migration file, \console\migrations\m180620_105204_update_table_options_to_log.php
<pre class="wp-block-syntaxhighlighter-code">

<?php

use yii\db\Migration;

/**
 * Class m180620_105204_update_table_options_to_log
 */
class m180620_105204_update_table_options_to_log extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
            $this->execute('ALTER TABLE {{%user}} ROW_FORMAT=DYNAMIC');
            $this->execute('ALTER TABLE {{%user}} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
            $this->execute('ALTER TABLE {{%log}} ROW_FORMAT=DYNAMIC');
            $this->execute('ALTER TABLE {{%log}} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
        }

        $this->addCommentOnTable('{{%user}}', '用户', $tableOptions);
        $this->addCommentOnTable('{{%log}}', '日志', $tableOptions);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m180620_105204_update_table_options_to_log cannot be reverted.\n";

        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m180620_105204_update_table_options_to_log cannot be reverted.\n";

        return false;
    }
    */
}


</pre>
7. Configure the request method of the request log to allow records, mainly to ignore the log of the get request, edit \api\config\params.php
<pre class="wp-block-syntaxhighlighter-code">

<?php
return [
    'adminEmail' => 'admin@example.com',
    'requestLog' => [
        'allowMethod' => ['POST', 'PUT', 'DELETE'], //请求日志允许记录的请求方法
    ],
];


</pre>
8. In Postman, get http://api.pcs-api.localhost/v1/logs?login_id=e56db1b43546a110431ac3 8409ed8e9e&login_tid=49117dc410c491af0de08f2948aecd8f&page=2


{
    "code": 10000,
    "message": "获取日志列表成功",
    "data": {
        "items": [
            {
                "id": 21,
                "level": 4,
                "category": "api\\behaviors\\RequestLogBehavior::afterRequest",
                "log_time": 1530000329.6175,
                "prefix": "[app-api][/v1/config-column-users/1][8]",
                "message": {
                    "url": "/v1/config-column-users/1",
                    "request_query_params": {
                        "id": "1",
                        "login_id": "e56db1b43546a110431ac38409ed8e9e",
                        "login_tid": "49117dc410c491af0de08f2948aecd8f"
                    },
                    "request_body_params": {
                        "users": [
                            {
                                "user_pic": "https://cmcconsole.chinamcloud.com/user_pic/1800000005_13281105967_1529552309.png",
                                "user_mobile": "13281105967",
                                "id": "8",
                                "user_nick": "13281105967",
                                "group_id": "015ce30b116ce86058fa6ab4fea4ac63",
                                "user_type": "1",
                                "update_time": "2018-06-26 15:37:52",
                                "add_time": "2018-04-26 10:05:28",
                                "login_name": "13281105967",
                                "user_token": "fb46626f0e71e423ca8ab4c750620a85",
                                "is_open": "1",
                                "user_email": "13281105967@chinamcloud.com"
                            },
                            {
                                "id": "299",
                                "group_id": "015ce30b116ce86058fa6ab4fea4ac63",
                                "login_name": "test11",
                                "user_token": "670d30e8d2d1f994fc0788d4ce95e0f3",
                                "user_nick": "test11",
                                "user_pic": "https://cmcconsole.chinamcloud.com/imgs/default_header.png",
                                "user_mobile": "",
                                "user_email": "",
                                "is_open": "1",
                                "add_time": "2018-05-15 18:47:43",
                                "update_time": "2018-05-18 15:08:11",
                                "user_type": "2"
                            }
                        ]
                    },
                    "user_id": "8",
                    "$_SERVER": {
                        "HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9",
                        "HTTP_ACCEPT": "application/json; version=0.0; cookie=enable",
                        "HTTP_HOST": "api.pcs-api.localhost",
                        "REMOTE_ADDR": "127.0.0.1",
                        "REQUEST_URI": "/v1/config-column-users/1",
                        "REQUEST_METHOD": "PUT",
                        "CONTENT_TYPE": "application/x-www-form-urlencoded; charset=utf-8"
                    }
                }
            },
            {
                "id": 22,
                "level": 4,
                "category": "api\\behaviors\\RequestLogBehavior::afterRequest",
                "log_time": 1530000396.3784,
                "prefix": "[app-api][/v1/config-columns][8]",
                "message": {
                    "url": "/v1/config-columns",
                    "request_query_params": {
                        "login_id": "e56db1b43546a110431ac38409ed8e9e",
                        "login_tid": "49117dc410c491af0de08f2948aecd8f"
                    },
                    "request_body_params": {
                        "status": "1",
                        "code": "wxbj",
                        "name": "无线北京"
                    },
                    "user_id": "8",
                    "$_SERVER": {
                        "HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9",
                        "HTTP_ACCEPT": "application/json; version=0.0; cookie=enable",
                        "HTTP_HOST": "api.pcs-api.localhost",
                        "REMOTE_ADDR": "127.0.0.1",
                        "REQUEST_URI": "/v1/config-columns",
                        "REQUEST_METHOD": "POST",
                        "CONTENT_TYPE": "application/x-www-form-urlencoded; charset=utf-8"
                    }
                }
            },
            {
                "id": 23,
                "level": 4,
                "category": "api\\behaviors\\RequestLogBehavior::afterRequest",
                "log_time": 1530000418.5957,
                "prefix": "[app-api][/v1/config-columns/3][8]",
                "message": {
                    "url": "/v1/config-columns/3",
                    "request_query_params": {
                        "id": "3",
                        "login_id": "e56db1b43546a110431ac38409ed8e9e",
                        "login_tid": "49117dc410c491af0de08f2948aecd8f"
                    },
                    "request_body_params": {},
                    "user_id": "8",
                    "$_SERVER": {
                        "HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9",
                        "HTTP_ACCEPT": "application/json; version=0.0; cookie=enable",
                        "HTTP_HOST": "api.pcs-api.localhost",
                        "REMOTE_ADDR": "127.0.0.1",
                        "REQUEST_URI": "/v1/config-columns/3",
                        "REQUEST_METHOD": "DELETE",
                        "CONTENT_TYPE": "application/json; charset=utf-8"
                    }
                }
            }
        ],
        "_links": {
            "self": {
                "href": "http://api.pcs-api.localhost/v1/logs?login_id=e56db1b43546a110431ac38409ed8e9e&login_tid=49117dc410c491af0de08f2948aecd8f&page=2"
            },
            "first": {
                "href": "http://api.pcs-api.localhost/v1/logs?login_id=e56db1b43546a110431ac38409ed8e9e&login_tid=49117dc410c491af0de08f2948aecd8f&page=1"
            },
            "prev": {
                "href": "http://api.pcs-api.localhost/v1/logs?login_id=e56db1b43546a110431ac38409ed8e9e&login_tid=49117dc410c491af0de08f2948aecd8f&page=1"
            }
        },
        "_meta": {
            "totalCount": 23,
            "pageCount": 2,
            "currentPage": 2,
            "perPage": 20
        }
    }
}


Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), adjust the default character set to: utf8mb4, the adjustment of the interface response format, the empty array is automatically converted to empty objects, and the request log message is collected in the interface application (1 request corresponds to 1 log message) to the database, and the corresponding interface to implement the log function: log list (set data filter to enable filter processing), log details Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), realize the soft deletion of ActiveRecord, generate ActiveQuery, and customize the query class

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.