对请求数据的格式化

例如

{body:{}}--->{data:{}}

执行阶段概念 · OpenResty最佳实践 · 看云 https://www.kancloud.cn/kancloud/openresty-best-practices/50451

我们OpenResty做个测试,示例代码如下:

location /mixed {
set_by_lua $a 'ngx.log(ngx.ERR, "set_by_lua")';
rewrite_by_lua 'ngx.log(ngx.ERR, "rewrite_by_lua")';
access_by_lua 'ngx.log(ngx.ERR, "access_by_lua")';
header_filter_by_lua 'ngx.log(ngx.ERR, "header_filter_by_lua")';
body_filter_by_lua 'ngx.log(ngx.ERR, "body_filter_by_lua")';
log_by_lua 'ngx.log(ngx.ERR, "log_by_lua")';
content_by_lua 'ngx.log(ngx.ERR, "content_by_lua")';
}

执行结果日志(截取了一下):

set_by_lua
rewrite_by_lua
access_by_lua
content_by_lua
header_filter_by_lua
body_filter_by_lua
log_by_lua

这几个阶段的存在,应该是openresty不同于其他多数web server编程的最明显特征了。由于nginx把一个会话分成了很多阶段,这样第三方模块就可以根据自己行为,挂载到不同阶段进行处理达到目的。

这样我们就可以根据我们的需要,在不同的阶段直接完成大部分典型处理了。

  • set_by_lua: 流程分之处理判断变量初始化
  • rewrite_by_lua: 转发、重定向、缓存等功能(例如特定请求代理到外网)
  • access_by_lua: IP准入、接口权限等情况集中处理(例如配合iptable完成简单防火墙)
  • content_by_lua: 内容生成
  • header_filter_by_lua: 应答HTTP过滤处理(例如添加头部信息)
  • body_filter_by_lua: 应答BODY过滤处理(例如完成应答内容统一成大写)
  • log_by_lua: 回话完成后本地异步完成日志记录(日志可以记录在本地,还可以同步到其他机器)

实际上我们只使用其中一个阶段content_by_lua,也可以完成所有的处理。但这样做,会让我们的代码比较臃肿,越到后期越发难以维护。把我们的逻辑放在不同阶段,分工明确,代码独立,后期发力可以有很多有意思的玩法。

列举360企业版的一个例子:

# 明文协议版本
location /mixed {
content_by_lua '...'; # 请求处理
} # 加密协议版本
location /mixed {
access_by_lua '...'; # 请求加密解码
content_by_lua '...'; # 请求处理,不需要关心通信协议
body_filter_by_lua '...'; # 应答加密编码
}

内容处理部分都是在content_by_lua阶段完成,第一版本API接口开发都是基于明文。为了传输体积、安全等要求,我们设计了支持压缩、加密的密文协议(上下行),痛点就来了,我们要更改所有API的入口、出口么?

最后我们是在access_by_lua完成密文协议解码,body_filter_by_lua完成应答加密编码。如此一来世界都宁静了,我们没有更改已实现功能的一行代码,只是利用ngx-lua的阶段处理特性,非常优雅的解决了这个问题。

前两天看到春哥的微博,里面说到github的某个应用里面也使用了openresty做了一些东西。发现他们也是利用阶段特性+lua脚本处理了很多用户证书方面的东东。最终在性能、稳定性都十分让人满意。使用者选型很准,不愧是github的工程师。

不同的阶段,有不同的处理行为,这是openresty的一大特色。学会他,适应他,会给你打开新的一扇门。这些东西不是openresty自身所创,而是nginx c module对外开放的处理阶段。理解了他,也能更好的理解nginx的设计思维。

ngx lua模块之ngx.location.capture子请求学习-坏男孩-51CTO博客 https://blog.51cto.com/5ydycm/1900279

#user  nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
location / { # any query
# default_type text/html;
content_by_lua_file lualib/app001.lua;
}
}
} --学习body&args
--ngx.location.capture 返回res.status res.body res.header res.truncated
ngx.say("First")
res = ngx.location.capture("/flumelog",{method=ngx.HTTP_GET,body="name=zzj&age=33&name=badboy",args={arg_a=2,arg_b=3}})
for key,val in pairs(res) do
if type(val) == "table" then
ngx.say(key,"=>",table.concat(val,","))
else
ngx.say(key,"=>",val)
end
end
ngx.exit(123789) # curl http://localhost:8080/flumelog?name=zj&age=3&name=ba

  

2019/05/25 13:17:13 [error] 9063#0: *4 attempt to set status 123789 via ngx.exit after sending out the response status 200, client: 127.0.0.1, server: , request: "GET /flumelog?name=zj HTTP/1.1", host: "localhost:8080"
[root@flink logs]# tail error.log

Spring Cloud Gateway https://spring.io/projects/spring-cloud-gateway

Spring Cloud Gateway features:

  • Built on Spring Framework 5, Project Reactor and Spring Boot 2.0

  • Able to match routes on any request attribute.

  • Predicates and filters are specific to routes.

  • Hystrix Circuit Breaker integration.

  • Spring Cloud DiscoveryClient integration

  • Easy to write Predicates and Filters

  • Request Rate Limiting

  • Path Rewriting

对请求数据的格式化 方案 Spring Cloud Gateway features:的更多相关文章

  1. Spring Cloud gateway 网关服务二 断言、过滤器

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  2. Spring Cloud Gateway入坑记

    Spring Cloud Gateway入坑记 前提 最近在做老系统的重构,重构完成后新系统中需要引入一个网关服务,作为新系统和老系统接口的适配和代理.之前,很多网关应用使用的是Spring-Clou ...

  3. spring cloud:服务网关 Spring Cloud GateWay 入门

    Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...

  4. 快速突击 Spring Cloud Gateway

    认识 Spring Cloud Gateway Spring Cloud Gateway 是一款基于 Spring 5,Project Reactor 以及 Spring Boot 2 构建的 API ...

  5. 看完就会的Spring Cloud Gateway

    在前面几节,我给大家介绍了当一个系统拆分成微服务后,会产生的问题与解决方案:服务如何发现与管理(Nacos注册中心实战),服务与服务如何通信(Ribbon, Feign实战) 今天我们就来聊一聊另一个 ...

  6. Spring Cloud Gateway GatewayFilter的使用

    Spring Cloud Gateway GatewayFilter的使用 一.GatewayFilter的作用 二.Spring Cloud Gateway内置的 GatewayFilter 1.A ...

  7. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

  8. Spring Cloud Gateway - 路由法则

    1. After Route Predicate Factory 输入一个参数:时间,匹配该时间之后的请求,示例配置: spring: cloud: gateway: routes: - id: af ...

  9. spring cloud gateway 深入了解 - Predicate

    文章来源 spring cloud gateway 通过谓词(Predicate)来匹配来自用户的请求 为了方便,使用postman测试不同的谓词的效果 路径谓词(Predicate)—— 最简单的谓 ...

随机推荐

  1. 关于unity中的update、Lateupdate和FixedUpdate

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  2. MySql 安装及实用笔记

    安装 更新 rpm 包 rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 安装MySql yum - ...

  3. JVM系列一:虚拟机内存区域

    虚拟机栈 1.虚拟机栈维护一个线程中所有方法的栈帧,每个栈帧中保存着这个方法中用到的局部变量表,操作数栈,常量引用 2.可以用-Xss来设置每个线程中虚拟机栈的大小,在jdk1.4之前默认虚拟机栈大小 ...

  4. MySQL 空间数据 简单操作

    在做的项目中需要,自己绘制区域图形,并存储起来,后面还有更新的需要,存文件不方面,想到现在数据库都支持空间数据库. 现在用的就是 MySQL ,就继续用 MySQL 来存储.管理空间数据.下面就做一些 ...

  5. flutter,flutter版本version/channel切换问题

    flutter go,官方的指南版本如下: 如何设置版本和channel,尝试 flutter help,发现原来flutter version不单是可以查所有版本(--version查当前版本)还可 ...

  6. 10 loader - 配置处理less文件的loader

    第一步:装包 cnpm i less-loader -D 安装完提示警告 peerDependencies WARNING less-loader@* requires a peer of less@ ...

  7. Linux Redirecting to /bin/systemctl restart iptables.service

    方案 一 1.昨天碰到一个错误,linux下输入命令:service iptables restart时,总会报下面一个提示很简单,这句话的意思就是让你重定向到systemctl然后再启用,白话就是说 ...

  8. javascript权威指南第22章高级技巧

    HTML <!DOCTYPE html> <html> <head> </head> <body> <div style=" ...

  9. idea项目打包和在linux的部署

    1.将项目打包 2.先clean,后package 3.将打好的包上传到linux 使用cd指令到要上传的文件的目录 4.输入rz -y 5.解压tar.gz格式: tar -zxvf filenam ...

  10. MongoDB 查看集合是否分片

    MongoDB会把分片过的集合保存在config.collection集合中,若需要查看分片键,则需要根据该集合进行查找.官方的其他很多分片快捷命令也都处于config库 三种方式 1.去config ...