skywalking對nginx的支持


轉載自博客: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_devellua-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模塊,需要安裝LuaJIT解釋器,並且重新編譯Nginx,或者可使用國人開發的openrestry
  • 需要的模塊:LuaJIT,Ngx_devellua-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. 下載最新的luajitngx_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 安裝:

  1.  
    > brew install openresty/brew/openresty
  2.  
     
  3.  
    ==> Summary
  4.  
    ???? /usr/local/Cellar/openresty/ 1.15.8.2: 303 files, 6.5MB, built in 1 minute
  5.  
    ==> Caveats
  6.  
    ==> openresty-openssl
  7.  
    openresty-openssl is keg-only, which means it was not symlinked into /usr/local,
  8.  
    because only for use with OpenResty.
  9.  
     
  10.  
    If you need to have openresty-openssl first in your PATH run:
  11.  
    echo 'export PATH="/usr/local/opt/openresty-openssl/bin:$PATH"' >> ~/.zshrc
  12.  
     
  13.  
    For compilers to find openresty-openssl you may need to set:
  14.  
    export LDFLAGS= "-L/usr/local/opt/openresty-openssl/lib"
  15.  
    export CPPFLAGS= "-I/usr/local/opt/openresty-openssl/include"
  16.  
     
  17.  
    ==> openresty
  18.  
    To have launchd start openresty/brew/openresty now and restart at login:
  19.  
    brew services start openresty/brew/openresty
  20.  
    Or, if you don't want/need a background service you can just run:
  21.  
    openresty
  22.  
     
  • 克隆或者下載 Apache Nginx Lua庫

  1.  
    git clone https: //github.com/apache/skywalking-nginx-lua.git
  2.  
    cd skywalking-nginx-lua/examples/

該目錄存放了關於Nginx的樣例配置,如下是部分內容:

  • nginx.conf:

  1.  
    http {
  2.  
    # 這里指向剛剛克隆下來的 skywalking nginx lua項目
  3.  
    lua_package_path "path/to/skywalking-nginx-lua/lib/skywalking/?.lua;;";
  4.  
    # Buffer represents the register inform and the queue of the finished segment
  5.  
    lua_shared_dict tracing_buffer 100m;
  6.  
     
  7.  
    init_worker_by_lua_block {
  8.  
    local metadata_buffer = ngx.shared.tracing_buffer
  9.  
     
  10.  
    metadata_buffer:set( 'serviceName', 'User Service Name')
  11.  
    -- Instance means the number of Nginx deloyment, does not mean the worker instances
  12.  
    metadata_buffer:set( 'serviceInstanceName', 'User Service Instance Name')
  13.  
     
  14.  
    # 這里需要指定上報Skywalking 的后端地址。端口默認是 12800
  15.  
    require( "client"):startBackendTimer("http://127.0.0.1:12800")
  16.  
    }
  17.  
     
  18.  
    server {
  19.  
    listen 8080;
  20.  
    location /test {
  21.  
    default_type text/html;
  22.  
     
  23.  
    rewrite_by_lua_block {
  24.  
    require( "tracer"):start("upstream service")
  25.  
    }
  26.  
    # 這里則是 上圖中 nginx 上游服務的地址
  27.  
    proxy_pass http: //127.0.0.1:8080/upstream/test;
  28.  
    body_filter_by_lua_block {
  29.  
    if ngx.arg[2] then
  30.  
    require( "tracer"):finish()
  31.  
    end
  32.  
    }
  33.  
    log_by_lua_block {
  34.  
    require( "tracer"):prepareForReport()
  35.  
    }
  36.  
    }
  37.  
    }
  38.  
    }
  • 直接啟動 OpenResty 即可,並指定配置文件,例如:

openresty -c /Users/tanjian/gitprojects/skywalking-nginx-lua/examples/nginx.conf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM