eggjs egg-ioredis 中使用lua 脚本 (redis lua 脚本的扫盲帖 )
lopo1983 发表了文章 • 0 个评论 • 82 次浏览 • 2022-03-28 23:47
$ redis-cli --eval path/to/redis.lua numberkeys KEYS[1] KEYS[2] , ARGV[1] ARGV[2] ...
--eval,告诉redis-cli读取并运行后面的lua脚本
path/to/redis.lua,是lua脚本的位置,也可以直接为脚本字符串。是一个Lua 5.1 script。
numberkeys ,指定后续参数有几个key。
KEYS[1] KEYS[2],是要操作的键,可以指定多个,在lua脚本中通过KEYS[1], KEYS[2]获取
ARGV[1] ARGV[2],参数,在lua脚本中通过ARGV[1], ARGV[2]获取。实例脚本 批量查询hash的值 hget 和切换db 以eggjs为例/**
* @summary 竞拍列表
* @description 1.type 【1】检测用户是否已报名 【2】用户报名的拍卖列表
* @router GET /api/v1/user/auction/auction/list
* @request query string page 第几页
* @request query string limit 每页几个
* @request query string type 类型
* @request query string status 状态
* @apikey Authorization
* @response 200 ACRES
*/
async index() {
const {
ctx,
ctx:{
uid,
query:{
page=1,
limit=12,
type,
status
}
}
}= this;
const RDS = await ctx.app.redis.get('auctionRoom')
try {
// 查询符合条件的hash key
const [[],LIST] = await RDS.sscan(`ar:u:${uid}`,(page-1)*limit,'count',limit);
// 注册脚本
// 1 声明输出的类型 rst {} {}对应key 为数组的下标记
// 2 格式化输入的参数KEYS 对应lua的numberkeys 为1
// 3 lua中切换db 也可以做多个参数 如KEYS2 这里我是固定的 所以只传一个 KEYS
// 4,5,6 执行redis的hget脚本 .. v 为lua中的链接符号
// 7 返回值
await RDS.defineCommand("hmgetall", {
lua: `local rst={};
local id=cjson.decode(KEYS[1]);
redis.call('select',13);
for i,v in pairs(id)
do rst[i]=redis.call('hget', 'ar:' .. v,'meta_info')
end;
return rst;`,
})
// 使用脚本并格式化 输出
ctx.body = (await RDS.hmgetall(1,JSON.stringify(LIST.map(e=>`${e}`)))).reduce((a,b)=>{
a['list'].push(JSON.parse(b));
return a
},{list:,page:{limit,page}})
} catch (err) {
console.log(err)
return ctx.body = {
code: 211,
message: ctx.app.config.env === 'local' ? `${(err.message)}`:'服务器忙,请稍后重试!',
data: err.errors
}
}
}[/i]
查看全部
调用Lua脚本的语法:实例脚本 批量查询hash的值 hget 和切换db 以eggjs为例
$ redis-cli --eval path/to/redis.lua numberkeys KEYS[1] KEYS[2] , ARGV[1] ARGV[2] ...
--eval,告诉redis-cli读取并运行后面的lua脚本
path/to/redis.lua,是lua脚本的位置,也可以直接为脚本字符串。是一个Lua 5.1 script。
numberkeys ,指定后续参数有几个key。
KEYS[1] KEYS[2],是要操作的键,可以指定多个,在lua脚本中通过KEYS[1], KEYS[2]获取
ARGV[1] ARGV[2],参数,在lua脚本中通过ARGV[1], ARGV[2]获取。
/**
* @summary 竞拍列表
* @description 1.type 【1】检测用户是否已报名 【2】用户报名的拍卖列表
* @router GET /api/v1/user/auction/auction/list
* @request query string page 第几页
* @request query string limit 每页几个
* @request query string type 类型
* @request query string status 状态
* @apikey Authorization
* @response 200 ACRES
*/
async index() {
const {
ctx,
ctx:{
uid,
query:{
page=1,
limit=12,
type,
status
}
}
}= this;
const RDS = await ctx.app.redis.get('auctionRoom')
try {
// 查询符合条件的hash key
const [[],LIST] = await RDS.sscan(`ar:u:${uid}`,(page-1)*limit,'count',limit);
// 注册脚本
// 1 声明输出的类型 rst {} {}对应key 为数组的下标记
// 2 格式化输入的参数KEYS 对应lua的numberkeys 为1
// 3 lua中切换db 也可以做多个参数 如KEYS2 这里我是固定的 所以只传一个 KEYS
// 4,5,6 执行redis的hget脚本 .. v 为lua中的链接符号
// 7 返回值
await RDS.defineCommand("hmgetall", {
lua: `local rst={};
local id=cjson.decode(KEYS[1]);
redis.call('select',13);
for i,v in pairs(id)
do rst[i]=redis.call('hget', 'ar:' .. v,'meta_info')
end;
return rst;`,
})
// 使用脚本并格式化 输出
ctx.body = (await RDS.hmgetall(1,JSON.stringify(LIST.map(e=>`${e}`)))).reduce((a,b)=>{
a['list'].push(JSON.parse(b));
return a
},{list:,page:{limit,page}})
} catch (err) {
console.log(err)
return ctx.body = {
code: 211,
message: ctx.app.config.env === 'local' ? `${(err.message)}`:'服务器忙,请稍后重试!',
data: err.errors
}
}
}[/i]
centos nginx 启动
lopo1983 发表了文章 • 0 个评论 • 323 次浏览 • 2021-07-30 15:56
nginx -c /etc/nginx/nginx.conf
linux 根据端口查进程id
lopo1983 发表了文章 • 0 个评论 • 861 次浏览 • 2020-07-19 22:04
kill [port]下面是windows的netstat -ano |findstr [port]
taskkill /f /t /im "进程id或者进程名称" 查看全部
netstat -tpln | grep [port]下面是windows的
kill [port]
netstat -ano |findstr [port]
taskkill /f /t /im "进程id或者进程名称"
centos7.x 防火墙常用操作
lopo1983 发表了文章 • 0 个评论 • 961 次浏览 • 2020-03-20 10:43
2、查看防火墙所有开放的端口firewall-cmd --zone=public --list-ports
3.、关闭防火墙
如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估systemctl stop firewalld.service
4、查看防火墙状态 firewall-cmd --state
5、查看监听的端口netstat -lnpt
PS:centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
6、检查端口被哪个进程占用netstat -lnpt |grep 5672
7、查看进程的详细信息
ps 6832
8、中止进程
kill -9 6832 查看全部
firewall-cmd --zone=public --add-port=5672/tcp --permanent# 开放5672端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent#关闭5672端口
firewall-cmd --reload# 配置立即生效
2、查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
3.、关闭防火墙
如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估
systemctl stop firewalld.service
4、查看防火墙状态
firewall-cmd --state
5、查看监听的端口
netstat -lnpt
PS:centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
6、检查端口被哪个进程占用
netstat -lnpt |grep 5672
7、查看进程的详细信息
ps 6832
8、中止进程
kill -9 6832
AMQP
lopo1983 发表了文章 • 0 个评论 • 959 次浏览 • 2020-03-15 23:12
从 AMQP 协议可以看出,Queue、Exchange 和 Binding 构成了 AMQP 协议的核心
Producer:消息生产者,即投递消息的程序Broker:消息队列服务器实体。
Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。Binding:绑定,它的作用就是把 Exchange 和 Queue 按照路由规则绑定起来。Queue:消息队列载体,每个消息都会被投入到一个或多个队列。
Consumer:消息消费者,即接受消息的程序。 查看全部
centos 安装最新版 redis
lopo1983 发表了文章 • 0 个评论 • 1027 次浏览 • 2020-03-14 23:53
yum --enablerepo=remi install redis设置开机启动启动
systemctl enable redis.service修改配置
vim /etc/redis.conf
# 设置可外网访问 DEV 模式用 上线慎用
bind 0.0.0.0
# 设置访问密码
requirepass ***********
:wq
/bin/systemctl restart redis.service
记住默认只有16个 chanceDb 查看全部
yum install -y http://rpms.famillecollet.com/ ... 7.rpm安装
yum --enablerepo=remi install redis设置开机启动启动
systemctl enable redis.service修改配置
vim /etc/redis.conf
# 设置可外网访问 DEV 模式用 上线慎用
bind 0.0.0.0
# 设置访问密码
requirepass ***********
:wq
/bin/systemctl restart redis.service
记住默认只有16个 chanceDb
妈妈再也不用担心我烧CPU 和内存了 Puppeteer 并发
lopo1983 发表了文章 • 0 个评论 • 2247 次浏览 • 2020-03-11 22:29
let WSE_LIST = ; //存储browserWSEndpoint列表
init();
app.get('/', function (req, res) {
let tmp = Math.floor(Math.random()* MAX_WSE);
(async () => {
let browserWSEndpoint = WSE_LIST[tmp];
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
await page.goto('file://code/screen/index.html');
await page.setViewport({
width: 600,
height: 400
});
await page.screenshot({path: 'example.png'});
await page.close();
res.send('Hello World!');
})();
});
function init(){
(async () => {
for(var i=0;i<MAX_WSE;i++){
const browser = await puppeteer.launch({headless:true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]});
browserWSEndpoint = await browser.wsEndpoint();
WSE_LIST[i] = browserWSEndpoint;
}
console.log(WSE_LIST);
})();
}[/i]啰嗦几句
开启几个浏览器 随机在浏览器上打开tab 关闭headless 看更明显 使用express 是为了更方便看到结果 查看全部
const MAX_WSE = 4; //启动几个浏览器啰嗦几句
let WSE_LIST = ; //存储browserWSEndpoint列表
init();
app.get('/', function (req, res) {
let tmp = Math.floor(Math.random()* MAX_WSE);
(async () => {
let browserWSEndpoint = WSE_LIST[tmp];
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
await page.goto('file://code/screen/index.html');
await page.setViewport({
width: 600,
height: 400
});
await page.screenshot({path: 'example.png'});
await page.close();
res.send('Hello World!');
})();
});
function init(){
(async () => {
for(var i=0;i<MAX_WSE;i++){
const browser = await puppeteer.launch({headless:true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]});
browserWSEndpoint = await browser.wsEndpoint();
WSE_LIST[i] = browserWSEndpoint;
}
console.log(WSE_LIST);
})();
}[/i]
开启几个浏览器 随机在浏览器上打开tab 关闭headless 看更明显 使用express 是为了更方便看到结果
vm 退出保存文件(新建)
lopo1983 发表了文章 • 0 个评论 • 825 次浏览 • 2020-03-06 13:31
:wq
:filename:file
:wq
Node+eggjs+mongodb 一步步实现 CRM(2)需求整理
lopo1983 发表了文章 • 0 个评论 • 1584 次浏览 • 2019-03-04 10:00
Node+eggjs+mongodb 一步步实现 CRM(1)环境搭建
lopo1983 发表了文章 • 0 个评论 • 2498 次浏览 • 2019-02-28 11:24
?1.安装nodejs?
?
? ?建议选择?LTS 版本,最低要求 8.x(机器配置不好的,请考虑8.x)。
?
2.egg 安装$ npm i egg-init -g
$ egg-init egg-crm --type=simple
$ cd egg-crm
$ npm inpm run devegg文档地址
?
3.mongoDB安装 (4.x)
?
建议在服务器上安装,以便随时可以开发,这里以centos?安装为例
步骤如下:
创建该路径文件 /etc/yum.repos.d/mongodb-org-4.0.repo[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc安装sudo yum install -y mongodb-org-4.0.6 mongodb-org-server-4.0.6 mongodb-org-shell-4.0.6 mongodb-org-mongos-4.0.6 mongodb-org-tools-4.0.6启动service mongod start?具体可参考官方文档
?
4.相关插件配置 "devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"baidu-aip-sdk": "^2.3.9",
"bce-sdk-js": "^0.2.9",
"decimal": "^0.0.2",
"egg-bin": "^4.11.0",
"egg-ci": "^1.11.0",
"egg-cors": "^2.1.2",
"egg-jwt": "^3.1.6",
"egg-mock": "^3.21.0",
"egg-mongoose": "^3.1.1",
"egg-validate": "^2.0.2",
"eslint": "^5.13.0",
"eslint-config-egg": "^7.1.0",
"lodash": "^4.17.11",
"stream-to-array": "^2.3.0",
"webstorm-disable-index": "^1.2.0",
"xml2js": "^0.4.19"
},
baidu-aip-sdk:百度AI接口用于智能审核 OCR等
bce-sdk-js: 百度BCE 接口 我们会用到百度的BOS 云存储
decimal:处理JS浮点误差
egg-cors:egg跨域
egg-jwt:egg jsonWebToken
egg-mongoose:mongo数据库链接
egg-validate:egg数据校验
lodash:一个十分优秀的函数编程库
stream-to-array:流处理
xml2js:微信支付
4.egg相关配置
?
config.default.js(相关留空的数据请填入自己的数据)'use strict';
module.exports = appInfo => {
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1539582326426_4353';
// csrf配置
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['http://localhost:7001', 'http://192.168.0.123', 'http://localhost]
};
//
config.cors = {
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
credentials: true
};
// // add your config here
// config.middleware = ;
config.middleware = ['errorHandler', 'responseFormatter', 'jwtErrorHandler'];
config.middleware.errorHandler = {
match: '/api',
};
config.middleware.responseFormatter = {
match: '/api',
};
//短信配置
config.sms = {
svip: {
uri: ‘',
name: '',
password: '',
},
};
// 邮箱配置
config.email = {
service: 'QQex',
port: 465,
secureConnection: true,
auth: {
user: '',
pass: '',
}
}
// mongodb配置
config.mongoose = {
url: 'mongodb://127.0.0.1/geecrm',
options: {},
};
// bos云存储
/*****************begin********************/
config.baiduBos = {
endpoint: 'http://bj.bcebos.com',
credentials: {
ak: '',
sk: ''
}
};
config.baiduBosBucket = '';
/*****************end***********************/
// baidu AIP
config.baiduAIP = {
APP_ID: '',
API_KEY: '',
SECRET_KEY: ''
};
// baidu VOD
config.baiduVod = {
endpoint: 'http://vod.bj.baidubce.com',
credentials: {
ak: '',
sk: ''
}
};
// 企业微信配置
config.weWork = {
'corpid': '',
'corpsecret': '',
'agentId': ''
};
// 微信配置
config.wechatApi = {
appId: '',
appSecret: '',
};
// 默认json 设置
config.JSONS = {
'code': 200,
'message': 'success',
'uri': 'https://api.lanjing.xyz',
};
config.alipay = {
appId: "",
rsaPrivate: "",
notifyUrl: "", //异步回调
signType: "RSA2",
rsaPublic: "",
sandbox: false //沙箱环境
}
config.wechatPay = {
partnerKey: "",
appId: "",
mchId: "",
notifyUrl: "http://www.langjing.xyz/wechat/notify", //异步回调,到微信平台设置下
pfx: ""
}
//
return config;
};
??plugin.js ? ? ? ? ?'use strict';
exports.validate = {
enable: true,
package: 'egg-validate',
};
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
};
exports.jwt = {
enable: true,
package: "egg-jwt"
};
exports.cors = {
enable: true,
package: "egg-cors"
};
?
?项目目录结构 (文件夹可按需建立无内容留空即可)
?
? 查看全部
本文章建议有一定Nodejs开发经验和熟悉ES6/7的开发人员查看,文中若有错误望指出!
?1.安装nodejs?
?
? ?建议选择?LTS 版本,最低要求 8.x(机器配置不好的,请考虑8.x)。
?
2.egg 安装
$ npm i egg-init -g
$ egg-init egg-crm --type=simple
$ cd egg-crm
$ npm i
npm run devegg文档地址
?
3.mongoDB安装 (4.x)
?
建议在服务器上安装,以便随时可以开发,这里以centos?安装为例
步骤如下:
创建该路径文件 /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]安装
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
sudo yum install -y mongodb-org-4.0.6 mongodb-org-server-4.0.6 mongodb-org-shell-4.0.6 mongodb-org-mongos-4.0.6 mongodb-org-tools-4.0.6启动
service mongod start?具体可参考官方文档
?
4.相关插件配置
"devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"baidu-aip-sdk": "^2.3.9",
"bce-sdk-js": "^0.2.9",
"decimal": "^0.0.2",
"egg-bin": "^4.11.0",
"egg-ci": "^1.11.0",
"egg-cors": "^2.1.2",
"egg-jwt": "^3.1.6",
"egg-mock": "^3.21.0",
"egg-mongoose": "^3.1.1",
"egg-validate": "^2.0.2",
"eslint": "^5.13.0",
"eslint-config-egg": "^7.1.0",
"lodash": "^4.17.11",
"stream-to-array": "^2.3.0",
"webstorm-disable-index": "^1.2.0",
"xml2js": "^0.4.19"
},
baidu-aip-sdk:百度AI接口用于智能审核 OCR等
bce-sdk-js: 百度BCE 接口 我们会用到百度的BOS 云存储
decimal:处理JS浮点误差
egg-cors:egg跨域
egg-jwt:egg jsonWebToken
egg-mongoose:mongo数据库链接
egg-validate:egg数据校验
lodash:一个十分优秀的函数编程库
stream-to-array:流处理
xml2js:微信支付
4.egg相关配置
?
config.default.js(相关留空的数据请填入自己的数据)
'use strict';
module.exports = appInfo => {
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1539582326426_4353';
// csrf配置
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['http://localhost:7001', 'http://192.168.0.123', 'http://localhost]
};
//
config.cors = {
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
credentials: true
};
// // add your config here
// config.middleware = ;
config.middleware = ['errorHandler', 'responseFormatter', 'jwtErrorHandler'];
config.middleware.errorHandler = {
match: '/api',
};
config.middleware.responseFormatter = {
match: '/api',
};
//短信配置
config.sms = {
svip: {
uri: ‘',
name: '',
password: '',
},
};
// 邮箱配置
config.email = {
service: 'QQex',
port: 465,
secureConnection: true,
auth: {
user: '',
pass: '',
}
}
// mongodb配置
config.mongoose = {
url: 'mongodb://127.0.0.1/geecrm',
options: {},
};
// bos云存储
/*****************begin********************/
config.baiduBos = {
endpoint: 'http://bj.bcebos.com',
credentials: {
ak: '',
sk: ''
}
};
config.baiduBosBucket = '';
/*****************end***********************/
// baidu AIP
config.baiduAIP = {
APP_ID: '',
API_KEY: '',
SECRET_KEY: ''
};
// baidu VOD
config.baiduVod = {
endpoint: 'http://vod.bj.baidubce.com',
credentials: {
ak: '',
sk: ''
}
};
// 企业微信配置
config.weWork = {
'corpid': '',
'corpsecret': '',
'agentId': ''
};
// 微信配置
config.wechatApi = {
appId: '',
appSecret: '',
};
// 默认json 设置
config.JSONS = {
'code': 200,
'message': 'success',
'uri': 'https://api.lanjing.xyz',
};
config.alipay = {
appId: "",
rsaPrivate: "",
notifyUrl: "", //异步回调
signType: "RSA2",
rsaPublic: "",
sandbox: false //沙箱环境
}
config.wechatPay = {
partnerKey: "",
appId: "",
mchId: "",
notifyUrl: "http://www.langjing.xyz/wechat/notify", //异步回调,到微信平台设置下
pfx: ""
}
//
return config;
};
??plugin.js ? ? ? ? ?
'use strict';
exports.validate = {
enable: true,
package: 'egg-validate',
};
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
};
exports.jwt = {
enable: true,
package: "egg-jwt"
};
exports.cors = {
enable: true,
package: "egg-cors"
};
?
?项目目录结构 (文件夹可按需建立无内容留空即可)
?
?