OpenResty 开发环境构建

基于kong环境构建

1、kong安装完成后,停止kong服务;

kong  stop

2、关闭openresty模块下nginx缓存

此操作为了方便后续,更改代码后,无需重启nginx服务;

将  nginx.conf 对应 server 下 lua_code_cache 设置为: off

[root@CentOS02 nginx]# cd /usr/local/openresty/nginx/
[root@CentOS02 nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  lua_tmp  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@CentOS02 nginx]# vi conf/nginx.conf
.....
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        lua_code_cache   off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;

......

nginx.conf添加配置,调用lua文件:

rewrite_by_lua_file  /usr/local/openresty/nginx/lua_tmp/test.lua;

.....
    server {
        listen       80;
        lua_code_cache   off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /lua {
         # MIME type determined by default_type:
         set $my_var 123456;
         default_type 'text/plain';
         rewrite_by_lua_file  /usr/local/openresty/nginx/lua_tmp/test.lua;
       }

.....

test.lua 文件开发参考:

https://www.nginx.com/resources/wiki/modules/lua/

[root@CentOS02 lua_tmp]# cat test.lua 
ngx.header.content_type = 'text/plain';
ngx.header["Hi-World"] = {"hello world !","hello baby!"};


ngx.print(ngx.md5("ddd"));

启动nginx

[root@CentOS02 nginx]# ./sbin/nginx 
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:37

 

调用演示

header 信息:

body信息:

 

引入自定义C++ 代码

 

安装 g++

yum install gcc-c++

 

声明C代码库地址

vi ~/.bash_profile


export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openresty/luajit/lib/lua/5.1/

 

编写C代码:

[root@CentOS02 5.1]# cat fast_sort.c 
/************************************************************
 *
 * gcc -g -o libluacallc.so -fPIC -shared fast_sort.c  
 *
 * *********************************************************/
int fast(int *a,int m,int n){  
	int key;
	int i,j;
	if(m<n){
		i=m;
		j=n;
		key=a[i];
		while(i<j){							
			while(i<j && a[j]>key){			
				j--;
			}
			if(i<j){
				a[i++]=a[j];				
				//i++;
			}
			while(i<j && a[i]<key){			
				i++;
			}
			if(i<j){
				a[j--]=a[i];				
				//j--;
			}
		}
		a[i]=key;
		fast(a,m,i-1);						
		fast(a,i+1,n);						
	}
	
}

编译生成 libluacallc.so

 gcc -g -o libluacallc.so -fPIC -shared fast_sort.c 

将 libluacallc.so 已入库地址: /usr/local/openresty/luajit/lib/lua/5.1/

编写lua脚本

local ffi=require"ffi"  

local myffi=ffi.load("luacallc")  

--lua 数组映射到C层  
ffi.cdef[[  
int fast(int *t,int n,int m);  
]]    
local t={55,29,11,33,222,77,20,55,90,0}  
local len=#t  
local t_c=ffi.new("int[?]",len,t)  
local re=myffi.fast(t_c,0,len-1)  
--因为得到的t_c为cdata数据,需要转换回lua数据  
local sorted_t={}  
for i=1,len do  
    sorted_t[i]=t_c[i-1]  
end  

ngx.header.content_type = 'text/plain';
ngx.header["Hi-World"] = {"hello world !","hello baby!"};


ngx.print("sorted t: ",table.concat(sorted_t,","))

 

重新调用:

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章