lua-resty-shell 多任务执行
Posted rongfengliang-荣锋亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lua-resty-shell 多任务执行相关的知识,希望对你有一定的参考价值。
已经写过一个openresty 使用lua-resty-shell 执行shell 脚本的demo,但是实际上我们可能是多节点运行,
同时需要负载均衡的机制。
lua-resty-shell 支持unix socket 以及tcp soket 的管理,但是在测试的时候发现tcp 有问题,所以只好
使用unix socket了,通过nginx 的stream 将unix 转为tcp,因为是测试,使用docker-compose 进行缩放
以及负载均衡的处理
说明: 可以同时参考 https://www.cnblogs.com/rongfengliang/p/10079432.html
环境准备
- docker-compose 文件
version: "3"
services:
app:
build: ./
ports:
- "8080:80"
volumes:
- "./app/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
backend:
build: ./
volumes:
- "./nginx-back.conf:/usr/local/openresty/nginx/conf/nginx.conf"
- 代码说明
dockerfile 主要是进行环境的准备,包括镜像的准备&&启动需要的服务
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
WORKDIR /sockproc
COPY ./sockproc/ /sockproc/
RUN make sockproc
COPY entrypoint.sh /entrypoint.sh
COPY sockproc.sh /sockproc.sh
COPY shell.lua /usr/local/openresty/lualib/resty/shell.lua
ENTRYPOINT [ "/entrypoint.sh" ]
nginx 配置要两个,一个是调用端的,一个是后端真正执行服务的
调用端,很简单,就是使用shell.lua 的封装进行调用
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
# resolver 127.0.0.11;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path \'/opt/app/?.lua;;\';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
default_type text/html;
index index.html;
}
location /test {
resolver 127.0.0.11;
content_by_lua_block {
require("app").call();
}
}
location /loop {
content_by_lua_block {
require("app").loop();
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
后端服务nginx配置,就是unix 转tcp 服务
worker_processes 1;
user root;
events {
worker_connections 1024;
}
stream {
server {
listen 13000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass unix:/tmp/shell.sock;
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path \'/opt/app/?.lua;;\';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
default_type text/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- lua 调用代码
app/app.lua
local shell = require("resty.shell")
local log = ngx.log
local ERR = ngx.ERR
local delay = 5
local handler
handler = function (premature,param)
-- do some routine job in Lua just like a cron job
if premature then
return
end
log(ERR, "param is : ", param)
ngx.timer.at(delay, handler,"again run... dalongrong")
end
local args = {
socket = {
host="backend",
port=13000
}
}
function call()
local status, out, err = shell.execute("cat /proc/sys/kernel/random/uuid", args)
ngx.say(out)
end
function loop()
local ok, err = ngx.timer.at(delay, handler,"dalong demo timer init")
end
return {
call=call,
loop=loop
}
启动&&测试
- 启动
docker-compose up -d
- 测试
http://localhost:8080/test
- 扩容
通过内置的基于dns的服务发现机制
docker-compose scale backend=3
参考资料
https://www.cnblogs.com/rongfengliang/p/10079432.html
https://github.com/rongfengliang/lua-resty-shell-docker--multi-running
https://github.com/juce/lua-resty-shell
以上是关于lua-resty-shell 多任务执行的主要内容,如果未能解决你的问题,请参考以下文章