一、环境安装部分

Centos7,Nginx1.14,Redis5.0,luajit-2.1,ngx_devel_kit-0.3.1rc1,lua-nginx-module-0.10.14.

下载安装包:

wget http://nginx.org/download/nginx-1.14.0.tar.gz
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
wget http://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz
wget http://download.redis.io/releases/redis-5.0.4.tar.gz

安装配置过程:

解压ngx_devel_kit和lua-nginx-module

tar xzvf v0.3.1rc1.tar.gz
tar xzvf v0.10.14.tar.gz

解压LuaJIT,进入目录编译安装

make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1

解压Nginx,给编译参数追加上面三个模块的目录再编译

nginx -V #已安装Nginx执行该命令查看现有编译参数
configure arguments:
--prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=www --group=www --with-http_ssl_module --with-pcre --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module #执行./configure命令,在现有编译参数最后追加luajit,ngx_devel_kit和lua-nginx-module
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=www --group=www --with-http_ssl_module --with-pcre --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module--with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/data/lua/ngx_devel_kit-0.3.1rc1 --add-module=/data/lua/lua-nginx-module-0.10.14 service nginx stop
make #编译
cp objs/nginx /usr/sbin/
service nginx start
nginx -V #检查模块是否添加上

解压Redis,进入目录编译

make PREFIX=/usr/local/redis
make install PREFIX=/usr/local/redis
cd /usr/local/redis/bin
./redis-server & #启动
./redis-cli #测试set a a; keys *; flushall


二、Lua代码编写部分

下载插件拷贝lua文件至目录 /usr/local/lib/lua/resty
resty.http:https://github.com/ledgetech/lua-resty-http
resty.redis:https://github.com/openresty/lua-resty-redis
resty.redis_iresty:https://github.com/moonbingbing/9915c66346e8fddcefb5

配置Nginx文件

#http下添加lua路径
lua_package_path "/usr/local/lib/lua/?.lua;;";

#server下添加DNS,字符集和location
resolver 8.8.8.8;
charset utf-8;
#地理解码
location /geocoder/{
  default_type "application/json;charset=utf-8";
  content_by_lua_file "/usr/local/lib/lua/resty/geocoder.lua"
  #也可以用content_by_lua_block{直接将代码写到nginx内部}
}
#鹰眼服务
location /api/ {
  default_type "application/json;charset=utf-8";
  content_by_lua_file "/usr/local/lib/lua/resty/api.lua"
}

编写lua代码文件

地理解码文件/usr/local/lib/lua/resty/geocoder.lua
数据隐私保护:客户端发送请求url可以不需要携带ak和service_id参数
二次请求缓存:第二请求传递坐标数据时,直接从redis拿坐标对应位置

local url = ngx.var.request_uri.."&ak=你的ak参数&service_id=你的鹰眼服务id"
local redis = require "resty.redis_iresty"
local red = redis:new()
local res, err = red:get(url)
if not res or res == ngx.null then
local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://api.map.baidu.com"..url)
if res.status == ngx.HTTP_OK then
red:set(url, res.body)
ngx.print(res.body)
return
else
ngx.exit(res.status)
end
end ngx.print(res)

鹰眼服务API文件/usr/local/lib/lua/resty/api.lua
支持post请求,可编辑post参数内容,对post参数追加ak和service_id
为防止错乱,只对包含参数end_time且值小于今天的请求结果进行缓存
客户端可用http://yourdomain.com/api/* 代替 http://yingyan.baidu.com/api/*

local http = require "resty.http"
local httpc = http.new() if ngx.var.request_method == "POST" then
ngx.req.read_body();
local postargs = ngx.req.get_post_args()
local postdata = "ak=你的ak参数&service_id=你的鹰眼服务id"
for k,v in pairs(postargs) do
postdata = postdata.."&"..k.."="..v
end
local res = httpc:request_uri("http://yingyan.baidu.com"..ngx.var.uri, { method = "POST", body = postdata, headers = { ["Content-Type"] = "application/x-www-form-urlencoded", ["Content-Length"] = nil } })
if res.status == ngx.HTTP_OK then
ngx.print(res.body)
return
else
ngx.exit(res.status)
end
end local end_time = nil
local args = ngx.req.get_uri_args()
local url = ngx.var.request_uri.."&ak=你的ak参数&service_id=你的鹰眼服务id"
for k,v in pairs(args) do
if(k == "end_time") then
end_time = v
break
end
end
if(end_time ~= nil) then
local now_time = os.date("*t")
local today_time = os.time({day=now_time.day, month=now_time.month,year=now_time.year, hour=, minute=, second=})
if(tonumber(end_time) < today_time) then
local redis = require "resty.redis_iresty"
local red = redis:new()
local res = red:get(url)
if not res or res == ngx.null then
local res = httpc:request_uri("http://yingyan.baidu.com"..url)
if res.status == ngx.HTTP_OK then
red:set(url, res.body)
ngx.print(res.body)
return
else
ngx.exit(res.status)
end
end ngx.print(res)
return
end
end local res = httpc:request_uri("http://yingyan.baidu.com"..url)
if res.status == ngx.HTTP_OK then
ngx.print(res.body)
else
ngx.exit(res.status)
end

以上开发调试工具:https://openresty.org/download/openresty-1.13.6.2-win64.zip,https://moonbingbing.gitbooks.io/openresty-best-practices/content/lua/build_env.html

PS:原创文章,转载请标注来源。@轻云科技,by海飞。

用Nginx+Lua+Redis给百度鹰眼API服务搭建缓存服务中间件(记录过程)的更多相关文章

  1. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!

    Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...

  2. 基于nginx+lua+redis高性能api应用实践

    基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...

  3. nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  4. Nginx+Lua+Redis 对请求进行限制

    Nginx+Lua+Redis 对请求进行限制 一.概述 需求:所有访问/myapi/**的请求必须是POST请求,而且根据请求参数过滤不符合规则的非法请求(黑名单), 这些请求一律不转发到后端服务器 ...

  5. nginx lua redis 访问频率限制(转)

    1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的 ...

  6. nginx+lua+redis构建高并发应用(转)

    nginx+lua+redis构建高并发应用 ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求. url请求nginx服务器,然后lua查询redis, ...

  7. nginx+lua+redis 处理APK包替换

    nginx + lua +redis 安装与使用入门: http://huoding.com/2012/08/31/156 nginx httpEchoModule : http://wiki.ngi ...

  8. nginx+lua+redis

    git clone --branch master https://github.com/openresty/lua-resty-redis.git yum install openssl opens ...

  9. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

随机推荐

  1. 两个Integer变量a和b,值相等,a==b等于多少?

    Integer a = Integer.valueOf(127); Integer b = Integer.valueOf(127);   Integer c = Integer.valueOf(12 ...

  2. [总结] Synchronized汇总

    Java中的每一个对象都可以作为锁. 1对于同步方法,锁是当前实例对象. 2对于静态同步方法,锁是当前对象的Class对象. 3对于同步方法块,锁是Synchonized括号里配置的对象. 当一个线程 ...

  3. lnmp一件安装包 搭建laravel 环境(lnmp1.4)(报错500)

    https://blog.csdn.net/huangyuxin_/article/details/78998486

  4. docker 快速部署ES集群 spark集群

    1) 拉下来 ES集群  spark集群 两套快速部署环境, 并只用docker跑起来,并保存到私库. 2)弄清楚怎么样打包 linux镜像(或者说制作). 3)试着改一下,让它们跑在集群里面. 4) ...

  5. RDLC报表系列--------初级报表

    前面记录下了很多平时开发遇到的问题,RLDC之前也是不会,只会水晶报表,后来也慢慢的也上手了.把这些记录下来,以后用的着 1.打开VS添加新建项,选择Reporting,选择报表,后缀名为RLDC的名 ...

  6. 理解OpenShift(7):基于 Prometheus 的集群监控

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  7. 转载:MySQL:亲测备份策略实例(线上真实备份案例)

    是否为线上库的备份而烦恼过,这里提供一个完整的备份从属数据库的备份方案,亲测可用 说明: 备份从库,按周计,每周进行一次全备 每周一的早上六点进行全备,其他时间备份中继日志 在从库上启用rsync服务 ...

  8. mysql集群搭建--韩国庆

    按照我给大家提供的步骤,一步一步来,你就能配好mysql集群环境 什么是mycat 简单的说,MyCAT就是: •一个彻底开源的,面向企业应用开发的“大数据库集群” •支持事务.ACID.可以替代My ...

  9. Spring事务@Transactional标签深入学习

    事务管理是应用系统开发中必不可少的一部分.Spring为事务管理提供了丰富的功能支持.Spring事务管理分为编码式和声明式 两种方式.编码式事务指的是通过编码方式实现事务;声明式事务基于AOP,将具 ...

  10. Python全栈开发记录_第五篇(装饰器)

    单独记录装饰器这个知识点是因为这个知识点是非常重要的,必须掌握的(代码大约150行). 了解装饰器之前要知道三个知识点 作用域,上一篇讲到过顺序是L->E->G->B 高阶函数: 满 ...