Preface
The first two chapters have described the installation and use of openresty.
This chapter mainly describes the installation and use of lua-protobuf in the openresty environment.
1: Environment
ubuntu18
lua-protobuf https://github.com/starwing/lua-protobuf
protobuf https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.1 ##The best version choose
openresty http: //openresty.org/cn/download.html
2: Install openresty
1> Openresty
installation official instructions can be installed
http://openresty.org/cn/linux-packages.html
2> Source code installation
Source code download address: http://openresty.org/cn/download.html
Installation instructions: http://openresty.org/cn/installation.html
On Linux, you usually need to use sudo to obtain root privileges to complete the installation.
Install dependencies (first sudo apt-get update to replace the domestic source faster)
sudo apt-get install libpq-dev
sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl
$HOME/software/openssl-OpenSSL_1_1_1u 源码解压缩文件夹 目录下包含 config文件 下面的是 OK 的
ubuntu@ubuntu:~/software/openresty-1.21.4.1$ ./configure --prefix=$HOME/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module --with-http_postgres_module --with-openssl=$HOME/software/openssl-OpenSSL_1_1_1u
Add environment variable
cd ~
vim .bashrc
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
start openresty
cd ~
mkdir work
cd work
mkdir src_lua ### Put all the lua scripts in this directory
mkdir conf The content of
vim nginx.conf is as follows
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存) //生产环境 on #修改lua文件不需要重新,立即生效
keepalive_timeout 60s; # 配置段: http, server, location 指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,
#有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。
client_body_timeout 20s;# 配置段: http, server, location 指定客户端与服务端建立连接后发送 request body 的超时时间。
#如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)
client_header_timeout 10s;# 配置段: http, server, location 客户端向服务端发送一个完整的 request header 的超时时间。
#如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)
location /test {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>");
ngx.say("nginx prefix ==> ",ngx.config.prefix());
}
}
location /testlua {
proxy_set_header X-real-ip $remote_addr; # 可直接获取客户端IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 通过代理服务器获取客户端IP
#lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存) //生产环境 on
content_by_lua_file src_lua/http.lua;
}
location /testproto {
proxy_set_header X-real-ip $remote_addr; # 可直接获取客户端IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 通过代理服务器获取客户端IP
#lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存) //生产环境 on
content_by_lua_file src_lua/testprotobuf.lua;
}
}
}
There are single quotes around the startup command pwd
cd ~/work
nginx -p pwd
/ -c conf/nginx.conf #Start, specify loading script
nginx -p pwd
/ -c conf/nginx.conf -s stop #Stop
nginx -p pwd
/ -c conf/nginx_debug.conf -s reload #reload
or create sh by yourself
, such as starting start.sh, the content is as follows
nginx -p pwd
/ -c conf/nginx.conf
Verify configuration
nginx -t ##Verify default configuration file
nginx -t -c ~/work/conf/nginx.conf ###Verify custom configuration file
2: Install lua-protobuf,
download and decompress https://github.com/starwing/lua-protobuf
gcc -O2 -shared -fPIC pb.c -o pb.so -I /usr/local/openresty/luajit/include/luajit -2.1
/usr/local/openresty/luajit/include/luajit-2.1 Confirm whether the location is the same or change the location
Copy 4 files to lualib directory
sudo cp pb.so /usr/local/openresty/lualib
sudo cp protoc.lua /usr/local/openresty/lualib
sudo cp luaunit.lua /usr/local/openresty/lualib
sudo cp serpent .lua /usr/local/openresty/lualib
4: protobuf use case
protobuf encoding and decoding reference
copying someone’s
login.proto online
syntax = "proto3";
package msg;
import "account.proto"; //proto full name
message Login_C {
Account account = 1;
}
message Login_S {
bool result = 1;
}
account.proto
syntax = "proto3";
package msg;
message Account {
string username = 1;
string password = 2;
}
Compile command
./protoc -I ./ -o proto.pb ./account.proto ./login.proto
Copy the generated proto.pb to the src_lua/pb directory.
The two .proto files are not needed, so they are copied for the convenience of viewing
5: Test
1> test script
testprotobuf.lua
local lu = require "luaunit"
local pb = require "pb"
local pbio = require "pb.io"
local buffer = require "pb.buffer"
local slice = require "pb.slice"
local conv = require "pb.conv"
local protoc = require "protoc"
local serpent = require "serpent"
--ngx.say("123")
local path = ngx.config.prefix() .. "src_lua/pb/proto.pb"
--ngx.config.prefix()); --/home/ubuntu18/work/
--local P = protoc.new()
local file, msg = io.open(path, "rb")
if file ~= nil then
local buffer = file:read("*a")
pb.load(buffer)
file:close()
else
ngx.log(ngx.ERR, "读取文件失败: ", msg)
ngx.say("读取文件失败: ", msg)
return
end
ngx.say("124")
--ngx.say("pb.type",pb.type)
local data = {
account = {
username = "test",
password = "123456"
}
}
local data1 ={
result = 0
}
local bytes = pb.encode("msg.Login_S", data1) --08 01
local bytes1 = assert(pb.encode("msg.Login_C", data)) --0A 0E 0A 04 74 65 73 74 12 06 31 32 33 34 35 36
ngx.say(pb.tohex(bytes))
ngx.say(pb.tohex(bytes1))
local data2 = pb.decode("msg.Login_C", bytes1)
ngx.say(serpent.block(data2))
--[[
{
account = {
password = "123456",
username = "test"
} --table: 0x7fe5b6f2c808
} --table: 0x7fe5b6f2c788
--]]
Lua script writing method can refer to test.lua under lua-protobuf
2> Run
curl http://127.0.0.1:8080/testproto
6: Upload the DEMO project if necessary.
If you find it useful, please like it and add a favorite.