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_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
posted on 2021-02-24 11:12 luzhouxiaoshuai 阅读(5159) 评论(0) 收藏 举报
                    
                
                
            
        
浙公网安备 33010602011771号