Openresty/Lua + Thrift + HBase

本文介绍了在Openresty环境中,利用Lua通过Thrift访问HBase的详细步骤。由于Openresty的Lua不支持HBase的原生Java API,作者选择了Thrift作为通信方式,详述了从安装Thrift、配置HBase到解决各种编译问题,最后成功测试连接的过程。

1. 问题

在Openresty环境下(也就是lua语言下),如何访问HBase?Lua不方便调用HBase的原生API(Java),所以只能通过别的方式,例如:

  • Rest
  • Thrift

Rest提供的接口不够丰富,比如checkAndPut就没有支持,所以,有必要尝试一下Thrift.

2. Thrift是什么

简单来说,Thrift是一种SOA(Service Oriented Architecture)的实现方式,和WSDL+SOAP那一套类似。过程都是这样的:

  • 以一种接口语言定义接口(对于WSDL+SOAP是.wsdl文件;对于Thrift是.thrift文件);
  • 使用一个编译器,把接口文件(.wsdl或者.thrift)转化为代码(客户端类和服务端虚基类/接口);
  • 继承服务端虚基类/接口,实现服务端逻辑;并部署服务;
  • 基于客户端类,通过少量的开发即可实现客户端,与服务端通信。
Thrift或者WSDL+SOAP框架帮助程序员实现远程方法调用,在整个过程中,程序员不用考虑参数的序列化、网络传输、返回数据的反序列化,甚至包括安全、事务等方面的工作。

3. 环境

  • CentOS 7
  • Luajit:为了很好的和openresty结合,我们使用openresty 1.9.15.1包自带的luajit;它和lua 5.1兼容;
  • Thrift 0.9.2:目前最新版是0.9.3,但是Thrift 0.9.3需要lua 5.2及以上版本,所以我们使用和lua 5.1兼容的Thrift 0.9.2。这为我们带来很多麻烦,因为Thrift 0.9.2有一些bug,见后文Thrift安装部分;
  • HBase 1.2.2:它里面有两个Thrift Server:thrift基于HBase的老API;thrift2基于HBase的新API;我们使用后者。

4. HBase安装与配置

HBase的安装与配置涉及到Hadoop hdfs和zookeeper,这里略去其过程。HBase服务成功启动后,启动Thrift服务:

# /usr/local/hbase-1.2.2/bin/hbase-daemon.sh start thrift2 -p 9090 --infoport 9095

5. Openresty安装

目前我们只需要使用luajit,但为了后续与openresty兼容,干脆这里把openresty安装了,后面实验直接使用它自带的luajit。假定已经安装了PCRE,OpenSSL等依赖:

# cd /tmp/
# wget https://openresty.org/download/openresty-1.9.15.1.tar.gz
# tar zxvf openresty-1.9.15.1.tar.gz
# cd openresty-1.9.15.1/
# ./configure --prefix=/usr/local/openresty-1.9.15.1 --without-lua51 --with-luajit
# gmake
# gmake install
# ln -s /usr/local/openresty-1.9.15.1/luajit/lib/libluajit-5.1.so /usr/lib64/libluajit-5.1.so
# ln -s /usr/local/openresty-1.9.15.1/luajit/lib/libluajit-5.1.so.2 /usr/lib64/libluajit-5.1.so.2

现在,我们就有了luajit:/usr/local/openresty-1.9.15.1/luajit/

6. 安装Thrift

6.1 安装依赖

# yum install readline.x86_64 install readline-devel.x86_64
# yum install libtool.x86_64
# yum install boost.x86_64 boost-devel.x86_64
# yum install byacc.x86_64
# yum install flex.x86_64 flex-devel.x
lua entry thread aborted: runtime error: content_by_lua(nginx.conf:61):16: module 'resty.http' not found: no field package.preload['resty.http'] no file '/opt/homebrew/opt/openresty/lualib/resty/http.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http.ljbc' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http/init.ljbc' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty/http.ljbc' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty/http/init.ljbc' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http/init.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty/http.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty/http/init.lua' no file './resty/http.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/luajit/share/luajit-2.1/resty/http.lua' no file '/usr/local/share/lua/5.1/resty/http.lua' no file '/usr/local/share/lua/5.1/resty/http/init.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/luajit/share/lua/5.1/resty/http.lua' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/luajit/share/lua/5.1/resty/http/init.lua' no file '/opt/homebrew/opt/openresty/lualib/resty/http.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty/http.so' no file './resty/http.so' no file '/usr/local/lib/lua/5.1/resty/http.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/luajit/lib/lua/5.1/resty/http.so' no file '/usr/local/lib/lua/5.1/loadall.so' no file '/opt/homebrew/opt/openresty/lualib/resty.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/lualib/resty.so' no file './resty.so' no file '/usr/local/lib/lua/5.1/resty.so' no file '/opt/homebrew/Cellar/openresty/1.27.1.2_1/luajit/lib/lua/5.1/resty.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: coroutine 0:
最新发布
09-15
<think> 我们已经在OpenResty环境中使用opm安装了ledgetech/lua-resty-http,但运行时仍出现"module 'resty.http' not found"错误。这个问题通常是由于OpenResty的包路径配置不正确导致的。以下是完整的解决方案: ### 一、问题原因分析 1. **路径不匹配**:opm默认安装路径为`/usr/local/openresty/site/lualib`,但OpenResty默认搜索路径可能未包含该目录 2. **环境变量缺失**:未正确设置`lua_package_path`变量 3. **权限问题**:OpenResty进程用户无权限访问模块文件 ### 二、解决方案步骤 #### 步骤1:验证实际安装路径 执行以下命令确认模块位置(注意根据你的OpenResty安装路径调整): ```bash # Homebrew安装路径(macOS) ls /opt/homebrew/opt/openresty/site/lualib/resty/http* # 标准安装路径(Linux) ls /usr/local/openresty/site/lualib/resty/http* ``` ✅ 正确输出应包含: ``` http.lua http_connect.lua http_headers.lua ``` #### 步骤2:配置OpenResty加载路径 在nginx配置的http块中添加路径声明(路径根据实际安装位置调整): ```nginx http { # 添加以下配置(Homebrew安装示例) lua_package_path "/opt/homebrew/opt/openresty/site/lualib/?.lua;;"; # 标准Linux安装使用 # lua_package_path "/usr/local/openresty/site/lualib/?.lua;;"; server { location /test { content_by_lua_block { local http = require "resty.http" -- 现在应能正确加载 -- 测试代码... } } } } ``` #### 步骤3:检查文件权限 确保OpenResty进程用户(通常是nginx用户)有读取权限: ```bash sudo chmod -R o+r /opt/homebrew/opt/openresty/site/lualib/resty ``` #### 步骤4:配置生效验证 1. 测试nginx配置: ```bash sudo openresty -t ``` 2. 重载配置: ```bash sudo openresty -s reload ``` ### 三、快速测试脚本 创建测试接口验证功能: ```nginx location /http-test { content_by_lua_block { local http = require "resty.http" local httpc = http.new() local ok, err = httpc:connect("example.com", 80) if not ok then ngx.say("Connection failed: ", err) return end ngx.say("Successfully loaded lua-resty-http!") httpc:close() } } ``` 访问`/http-test`应输出:`Successfully loaded lua-resty-http!` ### 四、高级配置技巧 1. **多路径配置**(推荐): ```nginx lua_package_path "/usr/local/openresty/lualib/?.lua;/opt/openresty/site/lualib/?.lua;;"; ``` 2. **开发环境调试**: 在nginx启动前设置环境变量: ```bash export LUA_PATH='/opt/homebrew/opt/openresty/site/lualib/?.lua;;' openresty ``` > 注:OpenResty的包加载机制遵循Lua标准,路径配置错误是此问题的最常见原因[^1][^2] --- ### 相关问题 1. 如何为不同server块设置独立的lua_package_path? 2. 使用opm安装的模块在Kubernetes环境中如何持久化? 3. lua-resty-http与OpenResty自带的ngx.location.capture有何区别? 4. 如何验证lua-resty-http的SSL/TLS证书链完整性? [^1]: 路径配置必须包含最后的`;;`,表示保留默认搜索路径 [^2]: macOS系统使用Homebrew安装时,路径通常为`/opt/homebrew/opt/openresty/...` </think> 以下针对 "module 'resty.http' not found" 错误的完整解决方案,结合您已安装 `ledgetech/lua-resty-http` 的情况: ### 一、核心解决方案 **问题根源**:OpenResty 未正确加载模块路径 **解决步骤**: 1. **确认实际安装路径**(替换 `/opt/homebrew/...` 为您的路径): ```bash # 查看安装文件 ls /opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/http* ``` ✅ 应输出三个文件: `http.lua` `http_connect.lua` `http_headers.lua` 2. **修改 Nginx 配置**: 在 `nginx.conf` 的 `http` 块中添加路径声明: ```nginx http { # 添加此行(路径根据实际安装位置调整) lua_package_path "/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/?.lua;;"; server { location / { content_by_lua_block { local http = require "resty.http" -- 现在应正常加载 -- 您的代码... } } } } ``` 3. **验证配置并重启**: ```bash sudo openresty -t # 测试配置 sudo openresty -s reload # 热重载 ``` ### 二、备选方案(若仍失败) #### 方案1:环境变量配置 ```bash # 临时生效(启动前设置) export LUA_PATH="/opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/?.lua;;" sudo openresty ``` #### 方案2:符号链接到默认路径 ```bash sudo ln -s /opt/homebrew/Cellar/openresty/1.27.1.2_1/site/lualib/resty/* /usr/local/openresty/lualib/resty/ ``` ### 三、快速测试脚本 创建测试文件 `test_http.lua`: ```lua local http = require "resty.http" local httpc = http.new() -- 测试连接 local ok, err = httpc:connect("example.com", 80) if not ok then print("ERROR: ", err) return end print("SUCCESS: resty.http loaded!") httpc:close() ``` 运行验证: ```bash resty test_http.lua ``` ✅ 成功输出:`SUCCESS: resty.http loaded!` ### 四、根本原因分析 | 原因 | 发生概率 | 典型场景 | |------|----------|----------| | 路径未配置 | 85% | Homebrew 安装的 OpenResty | | 权限问题 | 10% | 使用 `sudo` 安装但普通用户运行 | | 版本冲突 | 5% | 多版本 OpenResty共存 | > 注:Homebrew 安装的 OpenResty 默认路径与标准路径不同,必须显式配置 `lua_package_path`[^1][^2] --- ### 相关问题 1. 如何为不同项目配置独立的 OpenResty 模块路径? 2. 使用 Docker 部署时如何持久化 lua-resty-http 配置? 3. 如何验证 lua-resty-http 与当前 OpenResty 版本的兼容性? 4. 多版本 Lua 模块共存时如何避免冲突? [^1]: Homebrew 安装路径通常为 `/opt/homebrew/opt/openresty`(Apple Silicon)或 `/usr/local/opt/openresty`(Intel Mac) [^2]: 配置中的双分号 `;;` 表示保留默认搜索路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值