经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Lua » 查看文章
Openresty 进行路由系统设计
来源:cnblogs  作者:宇宙中漂浮的猫  时间:2019/2/13 9:20:52  对本文有异议

1、系统基础设计图为:

用户通过Http访问Openresty(Nginx + Lua), 其中Nginx虚拟主机中配置文件进行Lua脚本加载。

LUA通过nginx内置变量或者http请求中变量来区分不同集群,使用LUA脚本从redis中获取所属集群。

然后通过反向代理到对应集群。

2、 Openresty配置

Nginx主配置文件:

  1. 1 user nginx;
  2. 2 worker_processes 4;
  3. 3 pid logs/nginx.pid;
  4. 4
  5. 5 worker_rlimit_nofile 1000000;
  6. 6
  7. 7 events {
  8. 8 use epoll;
  9. 9 worker_connections 65535;
  10. 10 }
  11. 11
  12. 12 http {
  13. 13 lua_package_path "/usr/local/openresty/lualib/resty/lrucache/?.lua;;"; # 加载lua自定义模块。
  14. 14 include mime.types;
  15. 15 default_type application/octet-stream;
  16. 16 log_format main '$remote_addr - $remote_user [$time_local] $request $http_host '
  17. 17 '$status $body_bytes_sent $http_referer '
  18. 18 '"$http_user_agent" $http_x_forwarded_for '
  19. 19 '$upstream_addr $upstream_status $upstream_cache_status '
  20. 20 '$upstream_response_time $request_time';
  21. 21 access_log logs/access.log main;
  22. 22 error_log logs/error.log;
  23. 23 sendfile on;
  24. 24 tcp_nopush on;
  25. 25 max_ranges 1;
  26. 26 keepalive_timeout 120;
  27. 27 gzip on;
  28. 28 gzip_min_length 1k;
  29. 29 gzip_buffers 4 16k;
  30. 30 gzip_comp_level 2;
  31. 31 gzip_types application/x-www-form-urlencoded application/pdf text/plain application/x-javascript application/javascript text/css application/xml application/json;
  32. 32 client_max_body_size 22m;
  33. 33 client_body_buffer_size 1024k;
  34. 34 client_header_buffer_size 1024k;
  35. 35 underscores_in_headers on;
  36. 36
  37. 37 location /status {
  38. 38 stub_status on;
  39. 39 access_log off;
  40. 40 allow 127.0.0.1;
  41. 41 deny all;
  42. 42 }
  43. 43 }
  44. 44 include /data/work/conf/conf.d/*/*.conf;

虚拟主机的配置:

  1. 1 server {
  2. 2 listen 80;
  3. 3 server_name 域名;
  4. 4 access_log logs/access_80.log main;
  5. 5 error_log logs/error_80.log;
  6. 6
  7. 7
  8. 8 location / {
  9. 9 resolver DNSIP地址 valid=3600; 如果配置文件中使用了域名,需要此配置,不然无法解析DNS
  10. 10 set $url ''; 定义变量
  11. 11 set $upstream_port '80'; 变量赋值
  12. 12 rewrite_by_lua_file lua/get_upstream_url.lua; 加载lua脚本
  13. 13 proxy_pass http://$url$request_uri;
  14. 14 proxy_http_version 1.1;
  15. 15 proxy_set_header Upgrade $http_upgrade;
  16. 16 proxy_set_header connection "keep-alive";
  17. 17 proxy_set_header Host $host;
  18. 18 proxy_set_header X-Forwarded-Port $server_port;
  19. 19 proxy_set_header X-Forwarded-For $remote_addr;21 proxy_set_header X-Real-IP $remote_addr;
  20. 22 proxy_read_timeout 300;
  21. 23 proxy_send_timeout 300;
  22. 24 proxy_connect_timeout 90;
  23. 25 proxy_temp_file_write_size 10240k;
  24. 26 }
  25. 27 }

lua 脚本

  1. local localCache = require("mycache") 本地缓存配置
  2. local localRes, err = localCache.get(ngx.var.host)
  3. if localRes ~= nil then
  4. ngx.var.url = localRes .. ":" .. ngx.var.upstream_port
  5. return
  6. else
  7. local redis = require "resty.redis"
  8. local red = redis:new()
  9. red:set_timeout(1000)
  10. local ok, err = red:connect("redis地址", 6379)
  11. if not ok then
  12. ngx.log(ngx.ERR, "failed to connect: ", err)
  13. return
  14. end
  15. local res, err = red:hget("hosts", ngx.var.host)
  16. if not res then
  17. ngx.log(ngx.ERR, "failed to get host: ", err)
  18. return
  19. end
  20. if res == ngx.null then
  21. ngx.log(ngx.ERR, "host not found.")
  22. ngx.var.url = "异常网址"
  23. return
  24. else
  25. localCache.set(ngx.var.host, res)
  26. ngx.var.url = res .. ":" .. ngx.var.upstream_port
  27. end
  28. local ok, err = red:set_keepalive(10000, 100)
  29. if not ok then
  30. ngx.log(ngx.ERR, "failed to set keepalive: ", err)
  31. return
  32. end
  33. end

自定义本地缓存的lua模块脚本

  1. 1 local _M = {}
  2. 2
  3. 3 local lrucache = require "resty.lrucache"
  4. 4
  5. 5 local c, err = lrucache.new(10000)
  6. 6 if not c then
  7. 7 return error("failed to create the cache: " .. (err or "unknown"))
  8. 8 end
  9. 9
  10. 10 function _M.get(host)
  11. 11 local key = c:get(host)
  12. 12 return key
  13. 13 end
  14. 14
  15. 15 function _M.set(host, key)
  16. 16 c:set(host, key, 3600)
  17. 17 local res = c:get(host)
  18. 18 end
  19. 19
  20. 20 return _M

 

原文链接:http://www.cnblogs.com/zhengyionline/p/10365213.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号