文章目录
  • 一、需求分析
  • 二、准备
  • 1、软件安装
  • 2、redis-lua封装优化
  • 三、实现
  • 1、nginx.conf
  • 2、dynamic.lua
  • 注意
  • 3、准备两个应用
  • 4、访问nginx


一、需求分析

根据用户访问url的参数,将请求转发到对应指定IP的服务器上。

二、准备

1、软件安装

安装openresty+redis,略

2、redis-lua封装优化

在关于web+lua+openresty开发中,项目中会大量操作redis,

重复创建连接–>数据操作–>关闭连接(或放到连接池)这个完整的链路调用完毕,
甚至还要考虑不同的 return 情况做不同处理,就很快发现代码中有大量的重复

推荐一个二次封装的类库
OpenResty使用Lua大全(四)OpenResty中使用Redis

三、实现

1、nginx.conf

worker_processes 4;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type text/html;
	sendfile        on;
    keepalive_timeout  65;
	
	# 指定lua文件地址
	lua_package_path '/usr/local/openresty/nginx/conf/?.lua;;';

    server {
        listen       80;
        server_name  192.168.56.10;
        charset utf-8;

        location / {
        	# 目标代理地址(动态生成)
        	set $target '';
        	# 执行lua
          	access_by_lua_block {
				local proxy = require("dynamic")
				ngx.var.target = proxy.dynamicProxy()
		   }
	        
	        proxy_pass http://$target;

			resolver 114.114.114.114;
        }

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

2、dynamic.lua

local redis = require "resty.redis_iresty"
local _M = {_VERSION = '0.01'}

local opts = {
        ip = "192.168.56.10",
        port = "6379",
        -- password = "root",
        db_index = 0
}

local red = redis:new(opts)

-- 封装函数,防止每次redis都要new
function _M.dynamicProxy()
	-- 获取url参数,通过client来获取redis的ip+端口
	local clientKey = ngx.req.get_uri_args()["client"]
	if not clientKey then
			ngx.exit(403)
		ngx.say("no param as client: ", err)
		return
	end

	-- 根据client,获取到ip+端口
	local clientIP, err = red:get(clientKey)
	if not clientIP then
			ngx.exit(404)
		ngx.say("failed to connect redis: ", err)
		return
	elseif clientIP == ngx.null then
		ngx.exit(404)
			ngx.say("failed to connect redis: ", err)
		return
	end
	
	-- 将获取到的value赋值给target
	-- ngx.var.target = clientIP
	return clientIP
	
end


return _M
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
注意

这里防止new多次,能否提高性能表示存疑。

3、准备两个应用

192.168.56.1:8081、192.168.56.1:8082,分别在redis中设置:

Openerstry + lua + redis根据请求参数实现动态路由转发_nginx

4、访问nginx

Openerstry + lua + redis根据请求参数实现动态路由转发_lua_02

Openerstry + lua + redis根据请求参数实现动态路由转发_lua_03


至此,实现了根据请求参数进行动态路由转发。