初探api网关--kong

初探api网关--kong

最近误打误撞从java开发转型成了lua脚本开发,写了几年的增删改查,现在接触一点新的语言,也挺有意思的的。工作4年,接触了前端开发、大数据开发到现在的网关开发,都是略懂一些,只是一直没有接触类似商城这种的高并发事物的场景,挺遗憾的,最近也准备自学一些,老本行不能丢。

言归正传,说到网关,大部分人应该想到的都是springcloud中的zuul网关。api网关的出现和如今流行的微服务框架是绑定的,为什么要把网关这块单独拎出来,我觉得主要有以下几点:

1.分布式 微服务的出现使得服务会部署在很多不同的服务器上,不可能让客服端与服务器直接通信。通过域名与管理不同的模块,模块之前相互调用也会产生跨域问题。

2.如果微服务模块重构,难以管理。

3.类似单点登录这样的通用逻辑,完成可以交给网关处理,让模块开发者专注于业务开发。

为什么选择kong做网关,我想主要是因为kong基于openresty,通过lua脚本开发,部署简单快捷,对业务服务的影响小,底层openresty相当于是nginx plus 性能优秀。

 

下面整理了nginx_lua相关的api 还有kong的pkd 也是从别人那里拷贝过来的 自己丰富了一些说明

table说明
ngx.arg指令参数,如跟在content_by_lua_file后面的参数
ngx.var变量,ngx.var.VARIABLE引用某个变量,定义在server或location中
ngx.ctx请求的lua上下文 属于单个location
ngx.header响应头,ngx.header.HEADER引用某个头
ngx.status响应码
  
API说明
ngx.log输出到error.log
print等价于 ngx.log(ngx.NOTICE, ...)
ngx.send_headers发送响应头
ngx.headers_sent响应头是否已发送
ngx.resp.get_headers获取响应头
ngx.timer.at注册定时器事件  ngx.timer.at(delay, 回调函数,param1,param2)
ngx.is_subrequest当前请求是否是子请求  用的不多 一般防止嵌套请求用的
ngx.location.capture发布一个子请求
ngx.location.capture_multi发布多个子请求
ngx.exec内部重定向
ngx.redirect301/302重定向 301永久 302会保存旧地址
ngx.print输出响应
ngx.say输出响应,自动添加'\n'
ngx.flush刷新响应
ngx.exit结束请求 后续lua不执行
ngx.eof结束请求 后续lua继续执行
ngx.sleep无阻塞的休眠(使用定时器实现)
ngx.get_phase 
ngx.on_abort注册client断开请求时的回调函数
ndk.set_var.DIRECTIVE 
ngx.req.start_time请求的开始时间
ngx.req.http_version请求的HTTP版本号
ngx.req.raw_header请求头(包括请求行)
ngx.req.get_method请求方法
ngx.req.set_method请求方法重载
ngx.req.set_uri请求URL重写
ngx.req.set_uri_args 
ngx.req.get_uri_args获取请求参数
ngx.req.get_post_args获取请求表单
ngx.req.get_headers获取请求头
ngx.req.set_header 
ngx.req.clear_header 
ngx.req.read_body读取请求体
ngx.req.discard_body扔掉请求体
ngx.req.get_body_data 
ngx.req.get_body_file 
ngx.req.set_body_data 
ngx.req.set_body_file 
ngx.req.init_body 
ngx.req.append_body 
ngx.req.finish_body 
ngx.req.socket 
ngx.escape_uri字符串的url编码
ngx.unescape_uri字符串url解码
ngx.encode_args将table编码为一个参数字符串
ngx.decode_args将参数字符串编码为一个table
ngx.encode_base64字符串的base64编码
ngx.decode_base64字符串的base64解码
ngx.crc32_short字符串的crs32_short哈希
ngx.crc32_long字符串的crs32_long哈希
ngx.hmac_sha1字符串的hmac_sha1哈希
ngx.md5返回16进制MD5
ngx.md5_bin返回2进制MD5
ngx.sha1_bin返回2进制sha1哈希值
ngx.quote_sql_strSQL语句转义
ngx.today返回当前日期
ngx.time返回UNIX时间戳
ngx.now返回当前时间
ngx.update_time刷新时间后再返回
ngx.localtime 
ngx.utctime 
ngx.cookie_time返回的时间可用于cookie值
ngx.http_time返回的时间可用于HTTP头
ngx.parse_http_time解析HTTP头的时间
ngx.re.match 
ngx.re.find 
ngx.re.gmatch 
ngx.re.sub 
ngx.re.gsub 
ngx.shared.DICT 
ngx.shared.DICT.get 
ngx.shared.DICT.get_stale 
ngx.shared.DICT.set 
ngx.shared.DICT.safe_set 
ngx.shared.DICT.add 
ngx.shared.DICT.safe_add 
ngx.shared.DICT.replace 
ngx.shared.DICT.delete 
ngx.shared.DICT.incr 
ngx.shared.DICT.flush_all 
ngx.shared.DICT.flush_expired 
ngx.shared.DICT.get_keys 
ngx.socket.udp 
udpsock:setpeername 
udpsock:send 
udpsock:receive 
udpsock:close 
udpsock:settimeout 
ngx.socket.tcp 
tcpsock:connect 
tcpsock:sslhandshake 
tcpsock:send 
tcpsock:receive 
tcpsock:receiveuntil 
tcpsock:close 
tcpsock:settimeout 
tcpsock:setoption 
tcpsock:setkeepalive 
tcpsock:getreusedtimes 
ngx.socket.connect 
ngx.thread.spawn 
ngx.thread.wait 
ngx.thread.kill 
coroutine.create 
coroutine.resume 
coroutine.yield 
coroutine.wrap 
coroutine.running 
coroutine.status 
ngx.config.debug编译时是否有 --with-debug选项
ngx.config.prefix编译时的 --prefix选项
ngx.config.nginx_version返回nginx版本号
ngx.config.nginx_configure返回编译时 ./configure的命令行选项
ngx.config.ngx_lua_version返回ngx_lua模块版本号
ngx.worker.exiting当前worker进程是否正在关闭(如reload、shutdown期间)
ngx.worker.pid返回当前worker进程的pid
  
  
  
常量说明
Core constantsngx.OK (0)
ngx.ERROR (-1)
ngx.AGAIN (-2)
ngx.DONE (-4)
ngx.DECLINED (-5)
ngx.nil
HTTP method constantsngx.HTTP_GET
ngx.HTTP_HEAD
ngx.HTTP_PUT
ngx.HTTP_POST
ngx.HTTP_DELETE
ngx.HTTP_OPTIONS  
ngx.HTTP_MKCOL    
ngx.HTTP_COPY      
ngx.HTTP_MOVE     
ngx.HTTP_PROPFIND 
ngx.HTTP_PROPPATCH 
ngx.HTTP_LOCK 
ngx.HTTP_UNLOCK    
ngx.HTTP_PATCH   
ngx.HTTP_TRACE  
HTTP status constantsngx.HTTP_OK (200)
ngx.HTTP_CREATED (201)
ngx.HTTP_SPECIAL_RESPONSE (300)
ngx.HTTP_MOVED_PERMANENTLY (301)
ngx.HTTP_MOVED_TEMPORARILY (302)
ngx.HTTP_SEE_OTHER (303)
ngx.HTTP_NOT_MODIFIED (304)
ngx.HTTP_BAD_REQUEST (400)
ngx.HTTP_UNAUTHORIZED (401)
ngx.HTTP_FORBIDDEN (403)
ngx.HTTP_NOT_FOUND (404)
ngx.HTTP_NOT_ALLOWED (405)
ngx.HTTP_GONE (410)
ngx.HTTP_INTERNAL_SERVER_ERROR (500)
ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
ngx.HTTP_SERVICE_UNAVAILABLE (503)
ngx.HTTP_GATEWAY_TIMEOUT (504) 
Nginx log level constantsngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG

kong pdk 这位大佬总结的很全 包括插件开发的可用段都整理的很好

TO BE CONTINUED...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值