轉載自博客:https://blog.csdn.net/qq_24267619/article/details/106622434
skywalking要支持nginx:
skywalking對nginx的采集的agent插件是基於lua來編寫的,所以要采集nginx,nginx安裝的時候必須要支持lua插件
- 默認情況下
Nginx不支持Lua模塊,需要安裝LuaJIT解釋器,並且重新編譯Nginx,或者可使用國人開發的openrestry - 需要的模塊:
LuaJIT,Ngx_devel和lua-nginx-module - Luajit官網:https://luajit.or
第一種方式nginx安裝lua插件
第二種方式使用國人已經編寫好的openrestry,這個openrestry里面就封裝好了nginx+lua
第一種方式nginx安裝支持lua查看:https://blog.csdn.net/qq_31725371/article/details/85226116
Nginx安裝配置Lua支持
- 默認情況下
Nginx不支持Lua模塊,需要安裝LuaJIT解釋器,並且重新編譯Nginx,或者可使用國人開發的openrestry - 需要的模塊:
LuaJIT,Ngx_devel和lua-nginx-module - Luajit官網:https://luajit.org
1. 環境准備
[root@nginx_lua ~]# yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
- 1
2. 下載最新的luajit和ngx_devel_kit以及lua-nginx-module解壓
[root@nginx_lua ~]# mkdir -p /soft/src
[root@nginx_lua ~]# cd /soft/src/
wget https://luajit.org/download/LuaJIT-2.0.4.tar.gz
wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
- 1
- 2
- 3
- 4
- 5
3. 解壓ngx_devel_kit以及lua-nginx-module
[root@nginx_lua src]# tar xf v0.2.19.tar.gz
[root@nginx_lua src]# tar xf v0.10.13.tar.gz
- 1
- 2
4. 編譯安裝LuaJIT,即Lua及時編譯器
[root@nginx_lua src]# tar xf LuaJIT-2.0.4.tar.gz
[root@nginx_lua src]# cd LuaJIT-2.0.4/
[root@nginx_lua LuaJIT-2.0.4]# make && make install
- 1
- 2
- 3
- 4
5. 編譯安裝Nginx
[root@nginx_lua src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@nginx_lua src]# tar xf nginx-1.14.2.tar.gz
[root@nginx_lua src]# cd nginx-1.14.2
[root@nginx_lua nginx-1.14.2]# ./configure --prefix=/soft/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_dav_module --with-file-aio --with-http_dav_module --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.10.13/
[root@nginx_lua nginx-1.14.2]# make && make install
[root@nginx_lua nginx-1.14.2]# ln -s /soft/nginx/sbin/nginx /usr/bin/nginx
[root@nginx_lua conf]# vim nginx.conf #簡單配置寫nginx測試Nginx是否已經支持Lua(lua指令方式)
...
server {
location /test_lua {
default_type text/html;
content_by_lua_block {
ngx.say("Hello Lua!")
}
}
...
}
#lua指令方式
#在server 中添加一個localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#lua文件方式
#在server 中添加一個localtion
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相對於nginx安裝目錄
}
#test.lua文件內容
ngx.say("hello world");
//建立軟連接,如果不建立軟鏈接,則會出現share object錯誤
[root@nginx_lua conf]# nginx -t
/soft/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
[root@nginx_lua conf]#
[root@nginx_lua lib64]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
[root@nginx_lua lib64]# ll libluajit-5.1.so.2
lrwxrwxrwx 1 root root 33 Dec 21 20:52 libluajit-5.1.so.2 -> /usr/local/lib/libluajit-5.1.so.2
[root@nginx_lua lib64]#
#//加載lua庫,加入到ld.so.conf文件(暫時不執行這一步)
#[root@nginx_lua nginx-1.14.2]# echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
[root@nginx_lua conf]# nginx -t
nginx: the configuration file /soft/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /soft/nginx/conf/nginx.conf test is successful
[root@nginx_lua conf]# nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
6. 測試安裝Lua成功

點擊上方藍色字體,開始關注
Skywalking 支持 HTTP 1.1 的 PR 折騰了我好久,E2E 端到端測試是真的把我搞“怕”了···
OpenResty 是什么?
OpenResty是一個基於 Nginx 與 Lua 的高性能 Web 平台,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建能夠處理超高並發、擴展性極高的動態 Web 應用、Web 服務和動態網關。
因此,很多公司都會使用 OpenResty 開發出適合自己業務的高性能 web Server 模塊。
那么它和 Skywalking 有什么關心呢?我們得先知道Skywalking是什么,Skywalking 是由Java 語言開發的一套APM系統,主要監控服務與服務之間的調用鏈路以及服務應用指標。官方的介紹是:分布式系統的應用程序性能監視工具,專為微服務、雲原生架構和基於容器(Docker、K8s、Mesos)架構而設計。具體可以移步 Github: https://github.com/apache/skywalking
假設,當我們的使用Nginx/OpenResty 為我們的微服務做負載均衡的時候,怎樣才能將整條鏈路通過請求串起來,以及怎樣保證整個鏈路的拓撲圖正確呢?目前的解決方案是在 Nginx 中支持鏈路信息的上報,同時能夠將請求鏈路關鍵信息傳遞給上游應用。
Apache Skywalking Nginx Lua 項目
Githu項目地址:https://github.com/apache/skywalking-nginx-lua,該項目是吳晟采用 Lua 實現的 Nginx/OpenResty SDK。主要就是實現將 Nginx 作為一個節點注冊至Skywalking,同時將鏈路 TraceId 傳遞給上游服務,並將鏈路上報給 Skywalking。
Skywalking 7.x 開始支持 HTTP 1.1
具體PR請參考: https://github.com/apache/skywalking/pull/4399
Skywalking 監控 Java、Golang、Node、.NET 語言的鏈路都是采用了 SDK 或者 Agent 的方式將數據上報到 Skyalking 后端。不過都是采用 gRPC 的方式和后端交互,Apache Nginx Lua 考慮到 Nginx 足以勝任 10K 以上並發連接響應,並沒有采用 Nginx gRPC的方式,因此需要 Skywalking 支持 HTTP 1.1 的通信接口。
怎么玩起來?
這里先畫一個怎么玩的邏輯圖:
skywalking_nginx_lua
怎么搭建Skywalkng 的流程就不再贅述了,不是本文的重點,着重介紹怎么跑Skywalking Nginx Lua。 可以查看發布在嗶哩嗶哩上面的教程:https://www.bilibili.com/video/av35990012/
-
首先安裝 Openresty 參照官網:https://openresty.org/cn/download.html
Macos 系統采用 Homebrew 安裝:
-
> brew install openresty/brew/openresty
-
-
==> Summary
-
???? /usr/local/Cellar/openresty/ 1.15.8.2: 303 files, 6.5MB, built in 1 minute
-
==> Caveats
-
==> openresty-openssl
-
openresty-openssl is keg-only, which means it was not symlinked into /usr/local,
-
because only for use with OpenResty.
-
-
If you need to have openresty-openssl first in your PATH run:
-
echo 'export PATH="/usr/local/opt/openresty-openssl/bin:$PATH"' >> ~/.zshrc
-
-
For compilers to find openresty-openssl you may need to set:
-
export LDFLAGS= "-L/usr/local/opt/openresty-openssl/lib"
-
export CPPFLAGS= "-I/usr/local/opt/openresty-openssl/include"
-
-
==> openresty
-
To have launchd start openresty/brew/openresty now and restart at login:
-
brew services start openresty/brew/openresty
-
Or, if you don't want/need a background service you can just run:
-
openresty
-
-
克隆或者下載 Apache Nginx Lua庫
-
git clone https: //github.com/apache/skywalking-nginx-lua.git
-
cd skywalking-nginx-lua/examples/
該目錄存放了關於Nginx的樣例配置,如下是部分內容:
-
nginx.conf:
-
http {
-
# 這里指向剛剛克隆下來的 skywalking nginx lua項目
-
lua_package_path "path/to/skywalking-nginx-lua/lib/skywalking/?.lua;;";
-
# Buffer represents the register inform and the queue of the finished segment
-
lua_shared_dict tracing_buffer 100m;
-
-
init_worker_by_lua_block {
-
local metadata_buffer = ngx.shared.tracing_buffer
-
-
metadata_buffer:set( 'serviceName', 'User Service Name')
-
-- Instance means the number of Nginx deloyment, does not mean the worker instances
-
metadata_buffer:set( 'serviceInstanceName', 'User Service Instance Name')
-
-
# 這里需要指定上報Skywalking 的后端地址。端口默認是 12800
-
require( "client"):startBackendTimer("http://127.0.0.1:12800")
-
}
-
-
server {
-
listen 8080;
-
location /test {
-
default_type text/html;
-
-
rewrite_by_lua_block {
-
require( "tracer"):start("upstream service")
-
}
-
# 這里則是 上圖中 nginx 上游服務的地址
-
proxy_pass http: //127.0.0.1:8080/upstream/test;
-
body_filter_by_lua_block {
-
if ngx.arg[2] then
-
require( "tracer"):finish()
-
end
-
}
-
log_by_lua_block {
-
require( "tracer"):prepareForReport()
-
}
-
}
-
}
-
}
-
直接啟動 OpenResty 即可,並指定配置文件,例如:
openresty -c /Users/tanjian/gitprojects/skywalking-nginx-lua/examples/nginx.conf
