Openresty Wechat
使用Nginx-LuaJit-OpenResty-Lapis技術棧搭建的專用於處理微信公眾號回調服務的項目
項目源碼
https://github.com/helloJiu/o...
openresty源碼安裝(ubuntu為例)
apt install gcc libpcre3-dev libssl-dev perl make build-essential zlib1g-dev
wget https://openresty.org/download/openresty-1.19.9.1.tar.gz
tar -zxvf openresty-1.19.9.1.tar.gz
cd openresty-1.19.9.1/
./configure
make && make install
安裝luarocks
wget https://luarocks.github.io/luarocks/releases/luarocks-2.4.3.tar.gz
tar -xzvf luarocks-2.4.3.tar.gz
cd luarocks-2.4.3/
./configure --prefix=/usr/local/openresty/luajit --with-lua=/usr/local/openresty/luajit/ --lua-suffix=jit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make && make install
配置環境變量
vim /etc/profile
export PATH=$PATH:/usr/local/openresty/bin:/usr/local/openresty/luajit/bin
source /etc/profile
# 設置lua軟鏈到luajit
ln -s /usr/local/openresty/luajit/bin/luajit lua
mv lua /usr/bin/
安裝lapis
- https://leafo.net/lapis/
/usr/local/openresty/luajit/bin/luarocks install lapis
安裝redis依賴包和http-client依賴包以及其他依賴
opm install lua-resty-string
opm install openresty/lua-resty-redis
opm install ledgetech/lua-resty-http
微信公眾平台準備
測試號申請
https://mp.weixin.qq.com/debu...
內網穿透工具
https://www.cpolar.com/
cpolar.exe http 8123
配置
# 測試號信息
appID xxx
appsecret xxx
# 接口配置信息修改
url: 設置成 內網穿透得到的地址 如https://444aece.r6.cpolar.top/wechat/accept
Token: 對應配置裏的wechat.verifyToken
配置app/config/config.lua
-- 微信相關配置
wechat = {
appId = "xxx", --公眾號id
appSecret = "xxx", -- 公眾號秘鑰
verifyToken = "helloworld", -- 驗證Token
},
-- redis相關配置
redis = {
host = "127.0.0.1",
port = 6379,
password = "",
db_index = 0,
max_idle_time = 30000,
database = 0,
pool_size = 100,
timeout = 5000,
},
啓動項目
lapis server
- 瀏覽器訪問 127.0.0.1:8123
壓力測試
## autocannon壓測命令需要使用npm安裝
autocannon -c 100 -d 30 -p 2 -t 2 http://127.0.0.1:8123/wechat/checkLogin?scene=NHAK5ElJqz73YHaYhltG
## 運行結果
Running 30s test @ http://10.254.39.195:8123/wechat/checkLogin?scene=NHAK5ElJqz73YHaYhltG
100 connections with 2 pipelining factor
┌─────────┬───────┬────────┬────────┬────────┬───────────┬───────────┬─────────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼───────┼────────┼────────┼────────┼───────────┼───────────┼─────────┤
│ Latency │ 12 ms │ 314 ms │ 652 ms │ 701 ms │ 316.26 ms │ 186.86 ms │ 3094 ms │
└─────────┴───────┴────────┴────────┴────────┴───────────┴───────────┴─────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Req/Sec │ 7259 │ 7259 │ 8807 │ 9207 │ 8714.94 │ 436.3 │ 7258 │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 1.58 MB │ 1.58 MB │ 1.92 MB │ 2.01 MB │ 1.9 MB │ 95.1 kB │ 1.58 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
Req/Bytes counts sampled once per second.
# of samples: 30
267k requests in 30.03s, 57 MB read
55 errors (0 timeouts)
## QPS大概8700+