Hi
章老师,你好,请教您个问题。我先说下,我们的结构!
plugin_conf_path name=test so_path=plugin/libtest.so so_conf=conf/test.conf;
plugin_conf_path name=test1 so_path=plugin/libtest1.so so_conf=conf/test1.conf;
plugin_conf_path name=test2 so_path=plugin/libtest2.so so_conf=conf/test2.conf;
server {
listen 80;
server_name localhost;
location /test {
set $plugin_name "test";
plugin_handler;
}
location /test1 {
set $plugin_name "test1";
plugin_handler;
}
}
nginx 启动的时候,我们会有一个plugin_conf_path模块,会去dlopen 配置的so文件,把so的对象存入到map里面(so文件里面都是c++写的)
我们还有个plugin_handler 就是我们自己开发的一个nginx handler模块,会根据变量$plugin_name的名字去对应的接口函数里面找map里面对应的对象,然后根据这个对象取调用他的exec执行函数。
也就是说业务方只需要把业务逻辑写到so文件里面,并且有个exec执行函数,返回 header头和body信息。 这样不管多少个业务即so文件,只需要按照这种方式写好对应的执行exec函数即可~
我们的plugin_handler称为通用nginx handler模块~ 业务也不需要关注里面的细节~ 我们想做个通用的模块,业务只需要封装好so文件和对应的接口函数即可。
这样不管加多少个so文件都没问题,也不需要改动nginx handler模块的代码。
现在的问题是,我们打开so文件,里面的对象存储在map里面,请问下还有更好的方式吗? 因为一个请求过来了,我们都会根据变量名去map查找一次,能有其他的好办法吗?
或者章老师觉得我们的这种方式有什么可以改进的地方~ 期待您的回复,感谢了~
![]()