0,背景及网关介绍
1.1 背景
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。来自官网
1.2 API网关功能
API网关是将一个业务系统内部的多个微服务进行封装,对外提供唯一访问入口,实现系统内高内聚,系统间通过网关交互达到松耦合的效果。它可以和Eureka、Ribbon、Hystrix等组件配合使用,实现身份认证与安全、审查与监控、动态路由、压力测试、负载均衡、流量控制等功能
需要具备以下功能:
- 统一访问入口
- 安全:黑名单、权限身份认证
- 限流:实现微服务访问流量计算,基于流量计算分析进行限流,可以定义多种限流规则。
- 缓存:数据缓存
- 日志:日志记录
- 监控:记录请求响应数据,api耗时分析,性能监控
- 重试:异常重试
- 熔断:降级
1,安装
yum -y install readline-devel pcre pcre-devel openssl openssl-devel gcc curl GeoIP-devel
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
./configure --with-luajit --with-pcre --with-http_gzip_static_module --with-http_realip_module --with-http_geoip_module --with-http_ssl_module --with-http_stub_status_module
make
make install
–with-http_gzip_static_module #静态文件压缩
–with-http_stub_status_module #监控nginx状态
–with-http_realip_module #可以改变客户端请求头中客户端IP地址值(X-Real-IP和X-Forwarded-For),后台服务器记录原始客户端的IP地址
–with-pcre #设置PCRE库(pcre pcre-devel)
–with-http_ssl_module #使用https协议模块。(openssl openssl-devel)
–with-http_geoip_module #增加了根据ip获得城市信息,经纬度等模块 (GeoIP-devel)
2,目录
cd /usr/local/openresty
ll
bin #
COPYRIGHT
luajit
lualib
nginx # nginx目录,启动nginx
pod
resty.index
site
nginx操作,安装,高级配置(热升级和日志分割),http配置文件
luajit 采用C语言写的Lua代码的解释器 ----just in time 即时解析
lualib 编辑好的lua类库
3,相关配置
3.0 设置防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent
# –zone #作用域
# –add-port=80/tcp #添加端口,格式为:端口/通讯协议
# –permanent #永久生效,没有此参数重启后失效
systemctl stop firewalld.service
systemctl start firewalld.service
3.1 修改配置文件
vi /etc/profile
export NGINX_HOME=/usr/local/openresty/nginx
export PATH=$PATH:$NGINX_HOME/sbin
source /etc/profile
4,修改openresty配置文件
# 在server模块加上
vi nginx.conf
location /helloworld {
default_type text/html;
content_by_lua 'ngx.say("hello world")';
}
/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf # 检查配置文件
./nginx -s reload # 重新启动nginx
访问http://192.168.35.110/helloworld 输出 hello world
9,压力测试
yum -y install httpd-tools
ab -c10 -n50000 http://localhost:8080/ # -c:每次并发数为10个 -n:共发送50000个请求