我的android和ios只有一套代码,服务器在中国大陆和越南各自独立部署了一套,除了域名不一样,其他基本上一样。想法是:
1,app首次启动的时候,去连接一个网关切换服务器,网关会记录访问者的ip地址,
2,Ip2region 解析出ip所在国家代码,通过json格式返还给APP,
3,APP根据国家代码,配合用户所在的时区检测,如果返回的国家代码和用户所在时区一致,直接切换到对应国家服务器,如果不一致弹出对话框让用户选择地域
4,用户一旦确认使用的服务器,就把API地址写入app缓存中,下次检测到有缓存数据,直接使用缓存中的api接口地址,不再请求远端服务器。
ip2region库有各种主流语言的SDK,我本来想使用java写一个解析服务,但一想到还要专门维护一套java服务器,就直接放弃了,后面看到Ip2region 有lua的SDK
Nginx又是lua天然的组合,这样只要安装一套支持Lua的nginx就行了。最后我直接使用了Openresty ,Openresty 直接把各种常用的lua插件打包集成到一块了。安装完成开箱即可使用。
官方站点:https://openresty.org/en/
github地址:https://github.com/openresty/openresty/blob/master/
我这里直接使用的是最新版本,当前最新版本为:OpenResty 1.29.2.1
官方提供了各个主流操作系统发行的打包文件,我是用的腾讯定制版的操作系统,官方虽然有提供,但是下载一直是404,我只能源码编译。
我编译是用了openssl-3.5.5 pcre2-10.46 nginx_upstream_check_module, 都是源码下载下来。
对于openssl-3.5.5 还要专门打上官方的补丁文件,是用openssl 一定要看下官方支持的最新patch是哪个版本,太高的openssl可能没有补丁。
对于为什么打补丁,见如下说明:
核心作用
openssl-3.5.5-sess_set_get_cb_yield.patch (对应OpenSSL 1.1.1+版本)的核心作用是:
让OpenSSL的SSL session get callback支持"yield"(挂起/让出),使OpenResty能够在SSL握手阶段执行非阻塞的Lua代码(如查询Redis/MySQL获取session)
这是OpenResty实现"异步SSL session获取"的核心基础设施patch,没有它,SSL阶段的Lua代码就无法真正非阻塞
打补丁方式为,解压openssl-3.5.5 cd openssl-3.5.5 然后 patch -p1 < openssl-3.5.5-sess_set_get_cb_yield.patch
./configure --prefix=/usr/local/openresty --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_slice_module --with-http_sub_module --with-http_addition_module --with-http_auth_request_module --with-stream --with-stream_ssl_module --with-file-aio --with-pcre=../pcre2-10.46 --with-pcre-jit --with-openssl=../openssl-3.5.5 --add-module=../nginx_upstream_check_module --with-debug
二,编译ip2region 的lua_c模块
下载 https://github.com/lionsoul2014/ip2region
cd ip2region/binding/lua_c
执行:gcc -std=c99 -Wall -O2 -I../c/ -I/usr/local/openresty/luajit/include/luajit-2.1 ../c/xdb_util.c ../c/xdb_searcher.c xdb_searcher.c -fPIC -shared -o xdb_searcher.so -L/usr/local/openresty/luajit/lib -lluajit-5.1 默认生成一个xdb_searcher.so C模块,把它copy到 lua_package_cpath 加载的路径下,然后重启openresty
我的nginx配置文件:我就把xdb_searcher.so copy到了/usr/local/openresty/lualib/
lua_package_path "/webdata/conf/ip-router/lua/?.lua;/webdata/conf/ip-router/lualib/?.lua;;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!