OpenResty实战系列 | 包管理工具OPM和LuaRocks

2024-07-18 12:56:12 浏览数 (2)

概述

OpenResty 是一个基于 Nginx 的全功能 Web 平台,它集成了大量精心设计的 Nginx 模块,以及大量的 Lua 库。在使用 OpenResty 时,有两种主要的包管理工具 OPM 和 LuaRocks。

OPM

OPM(OpenResty Package Manager)是 OpenResty 官方提供的包管理工具,可以用来从中心 OPM 包服务器上面安装社区贡献的第三方模块。专门用于安装和管理 OpenResty 的 Lua 模块。

它是官方推荐使用的工具,因为它确保了模块与 OpenResty 的兼容性,并优化了性能,在你安装好 OpenResty 之后,就可以直接使用。

显示命令帮助

代码语言:javascript复制
# opm --help 
opm [options] command package...

Options:
    -h
    --help              Print this help.

    --install-dir=PATH  Install into the specified PATH directory instead of the system-wide
                        OpenResty installation tree containing this tool.

    --cwd               Install into the current working directory under ./resty_modules/
                        instead of the system-wide OpenResty installation tree containing
                        this tool.

Commands:
    build               Build from the current working directory a package tarball ready
                        for uploading to the server.

    clean ARGUMENT...   Do clean-up work. Currently the valid argument is "dist", which
                        cleans up the temporary files and directories created by the "build"
                        command.

    info PACKAGE...     Output the detailed information (or meta data) about the specified
                        packages.  Short package names like "lua-resty-lock" are acceptable.

    get PACKAGE...      Fetch and install the specified packages. Fully qualified package
                        names like "openresty/lua-resty-lock" are required. One can also
                        specify a version constraint like "=0.05" and ">=0.01".

    list                List all the installed packages. Both the package names and versions
                        are displayed.

    remove PACKAGE...   Remove (or uninstall) the specified packages. Short package names
                        like "lua-resty-lock" are acceptable.

    search QUERY...     Search on the server for packages matching the user queries in their
                        names or abstracts. Multiple queries can be specified and they must
                        fulfilled at the same time.

    server-build        Build a final package tarball ready for distribution on the server.
                        This command is usually used by the server to verify the uploaded
                        package tarball.

    update              Update all the installed packages to their latest version from
                        the server.

    upgrade PACKAGE...  Upgrade the packages specified by names to the latest version from
                        the server. Short package names like "lua-resty-lock" are acceptable.

    upload              Upload the package tarball to the server. This command always invokes
                        the build command automatically right before uploading.

For bug reporting instructions, please see:

    <https://openresty.org/en/community.html>

Copyright (C) Yichun Zhang (agentzh). All rights reserved.

通过opm search [包名]搜索包名和包的简介

代码语言:javascript复制
# opm search lua-resty-http

aptise/peter_sslers-lua-resty                     openresty ssl certificate routines for peter_sslers SSL Certificate manager
aptise/lua-resty-peter_sslers                     openresty ssl certificate routines for peter_sslers SSL Certificate manager
....
tomas/lua-resty-elasticsearch                     ElasticSearch client for OpenResty / ngx_lua.
agentzh/lua-resty-http                            Lua HTTP client cosocket driver for OpenResty/ngx_lua

通过opm get [包名]安装扩展包

代码语言:javascript复制
# opm get ledgetech/lua-resty-http

* Fetching ledgetech/lua-resty-http  
  Downloading https://opm.openresty.org/api/pkg/tarball/ledgetech/lua-resty-http-0.17.1.opm.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed  
100 20622  100 20622    0     0  28133      0 --:--:-- --:--:-- --:--:-- 28095 
Package ledgetech/lua-resty-http 0.17.1 installed successfully under /usr/local/openresty/site/ .

通过opm info [包名]查看已安装包的详细信息

代码语言:javascript复制
# opm info lua-resty-http

Name             : lua-resty-http
Version          : 0.17.1
Abstract         : Lua HTTP client cosocket driver for OpenResty/ngx_lua
Author           : James Hurst
Account          : ledgetech
Code Repo        : https://github.com/ledgetech/lua-resty-http
License          : BSD 2-Clause "Simplified" or "FreeBSD" license
Original Work    : yes

通过opm list查看已经安装的包列表

代码语言:javascript复制
# opm list
agentzh/lua-resty-http                                       0.09

通过opm upgrade [包名]包升级

代码语言:javascript复制
# opm upgrade lua-resty-http

* Fetching agentzh/lua-resty-http > 0.09
Package agentzh/lua-resty-http 0.09 is already the latest version.

通过opm remove [包名]移除已经安装的包

代码语言:javascript复制
# opm remove lua-resty-http

Package agentzh/lua-resty-http 0.09 removed successfully.

安装包存储位置

代码语言:javascript复制
cd /usr/local/openresty/site/lualib/resty

# ls
http.lua  http_headers.lua

使用

lua-resty-http 是一个基于OpenResty的HTTP客户端库,用于在Lua中进行HTTP请求和响应的处理。

openresty.tinywan.com.conf配置文件

代码语言:javascript复制
server {
    listen 80;
    server_name openresty.tinywan.com;

    location /lua_http_test {  
        default_type "text/html";
        lua_code_cache off; 
        content_by_lua_file conf/lua/lua_http_test.lua;
    }
}

lua_http_test.lua 脚本

代码语言:javascript复制
local httpc = require("resty.http").new()

-- Single-shot requests use the `request_uri` interface.
local res, err = httpc:request_uri("https://www.workerman.net/u/Tinywan", {
    method = "GET",
    body = "name=Tinywan&age=24",
    headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded",
    },
    ssl_verify = false,
})
if not res then
    ngx.log(ngx.ERR, "request failed: ", err)
    return
end

local status = res.status
local length = res.headers["Content-Length"]
local body   = res.body
ngx.say(res.body)

通过curl脚本测试请求打印结果

代码语言:javascript复制
$ curl -i http://openresty.tinywan.com/lua_http_test
HTTP/1.1 200 OK
Server: openresty/1.17.8.2
Date: Wed, 17 Jul 2024 09:42:10 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="shortcut icon" href="/favicon.ico" />
    <link href="https://cdn.workerman.net/css/bootstrap.min.css?v=20211126" rel="stylesheet">
    <link href="https://cdn.workerman.net/css/main.css?v=20240705" rel="stylesheet">
    <script src="https://cdn.workerman.net/js/jquery.min.js"></script>
    <script src="https://cdn.workerman.net/js/bootstrap.min.js?v=20211126"></script>
    <script src="https://cdn.workerman.net/js/functions.js?v=20220507"></script>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
    <title>Tinywan的主页-分享-workerman社区</title>
</head>
...
</body>
</html>

LuaRocks

LuaRocks 是一个通用的 Lua 模块管理工具,可以用来安装 Lua 模块。然而,使用 luarocks 与 OpenResty 并不是官方推荐的方式,因为 luarocks 安装的模块可能会阻塞 OpenResty 的事件循环,导致性能下降。

详细使用请看这里 Openresty如何使用lualocks包管理器安装使用Lua包

0 人点赞