はじめに
最初の 2 章では openresty のインストールと使用方法について説明しましたが、
この章では主に openresty 環境での lua-protobuf のインストールと使用方法について説明します。
1: 環境
ubuntu18
lua-protobuf https://github.com/starwing/lua-protobuf
protobuf https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.1 ##最適なバージョンは
openresty http: /を選択してください/openresty.org/en/download.html
2: openresty をインストールします。
1> Openresty
インストールの公式手順をインストールできます。
http://openresty.org/cn/linux-packages.html
2> ソース コードのインストール
ソース コードのダウンロード アドレス: http://openresty.org/cn/download。 html
インストール手順: http://openresty.org/cn/installation.html
Linux では、通常、インストールを完了するには sudo を使用して root 権限を取得する必要があります。
依存関係をインストールします (最初の sudo apt-get アップデートにより、国内のソースをより速く置き換えます)
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
環境変数を追加します
cd ~
vim .bashrc
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
start openresty
cd ~
mkdir work
cd work
mkdir src_lua ### すべての lua スクリプトをこのディレクトリに置きます
mkdir conf内容
vim
のnginx.confは以下の通り
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;
}
}
}
起動コマンド pwd は一重引用符で囲まれています
cd ~/work
nginx -p pwd
/ -c conf/nginx.conf #開始、ロードスクリプトを指定
nginx -p pwd
/ -c conf/nginx.conf -s stop #停止
nginx -p pwd
/ -c conf/nginx_debug.conf -s reload #reload
または自分で sh を作成する
、start.sh を起動するなど、内容は次のとおりです
nginx -p pwd
/ -c conf/nginx.conf
構成を確認する
nginx -t ##デフォルトの構成ファイルを確認する
nginx -t -c ~/work/conf/nginx.conf ###カスタム構成ファイルを確認する
2: lua-protobuf をインストールし、
ダウンロードして解凍します。 https://github.com/starwing/lua-protobuf
gcc -O2 -shared -fPIC pb.c -o pb.so -I /usr/local/openresty/luajit/include /ルアジット -2.1
/usr/local/openresty/luajit/include/luajit-2.1 場所が同じか確認、または場所を変更
4 つのファイルを lualib ディレクトリにコピーします
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 の使用例
protobuf のエンコードとデコードのリファレンス
誰かの
login.proto をオンラインでコピーする
syntax = "proto3";
package msg;
import "account.proto"; //proto full name
message Login_C {
Account account = 1;
}
message Login_S {
bool result = 1;
}
アカウント.プロト
syntax = "proto3";
package msg;
message Account {
string username = 1;
string password = 2;
}
コンパイルコマンド
./protoc -I ./ -o proto.pb ./account.proto ./login.proto
生成された proto.pb を src_lua/pb ディレクトリにコピーします。2
つの .proto ファイルは必要ないため、表示しやすいようにコピーされています。
5: テスト
1> テスト スクリプト
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 スクリプトの記述方法は、lua-protobuf 配下の test.lua を参照できます。
2>
curl http://127.0.0.1:8080/testprotoを実行します。
6: 必要に応じて DEMO プロジェクトをアップロードします。
役に立ったと思われた場合は、「いいね!」を押してお気に入りに追加してください。