KOA+egg.js集成kafka消息队列的示例(2)

'use strict'; module.exports = app => { const { STRING, INTEGER, DATE, TEXT } = app.Sequelize; const Log = app.model.define('log', { /** * UUID */ id: { type: STRING(36), primaryKey: true }, /** * 日志类型 */ type: STRING(100), /** * 优先等级(数字越高,优先级越高) */ level: INTEGER, /** * 操作者 */ operator: STRING(50), /** * 日志内容 */ content: TEXT, /** * IP */ ip: STRING(36), /** * 当前用户代理信息 */ user_agent: STRING(150), /** * 错误堆栈 */ error_stack: TEXT, /** * URL */ url: STRING(255), /** * 请求对象 */ request: TEXT, /** * 响应对象 */ response: TEXT, /** * 创建时间 */ created_at: DATE, /** * 更新时间 */ updated_at: DATE, }); return Log; };

6、测试Python脚本:

import requests from multiprocessing import Pool from threading import Thread from multiprocessing import Process def loop(): t = 1000 while t: url = "http://localhost:7001/log/notice" payload = "{\n\t\"type\": \"ERROR\",\n\t\"level\": 1,\n\t\"content\": \"URL send ERROR\",\n\t\"operator\": \"Knove\"\n}" headers = { 'Content-Type': "application/json", 'Cache-Control': "no-cache" } response = requests.request("POST", url, data=payload, headers=headers) print(response.text) if __name__ == '__main__': for i in range(10): t = Thread(target=loop) t.start()

7、建表语句:

SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for logs -- ---------------------------- DROP TABLE IF EXISTS `logs`; CREATE TABLE `logs` ( `id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '日志类型', `level` int(11) NULL DEFAULT NULL COMMENT '优先等级(数字越高,优先级越高)', `operator` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '操作人', `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '日志信息', `ip` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT 'IP\r\nIP', `user_agent` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '当前用户代理信息', `error_stack` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '错误堆栈', `url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '当前URL', `request` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '请求对象', `response` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '响应对象', `created_at` datetime(0) NULL DEFAULT NULL COMMENT '创建时间', `updated_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;

λ.3 后话

网上类似资料甚少,啃各种文档,探寻技术实现方式

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ab95371a26fa1b8cdf30f9fc0257d57f.html