系统相关
首页 > 系统相关> > 客户端灰度发布,NGINX+GeoIP2+GeoIP Database

客户端灰度发布,NGINX+GeoIP2+GeoIP Database

作者:互联网

需求的产生 (分地区更新)

我们公司呢是做电商平台的,其主打商品里有一款智能饮水机产品,而每台智能饮水机产品都是装在全国客户的家里。为了售后维护需要服务端如何保存数据并传回这块实现较简单,而在远程展示水机的余额、归属地、出水的质量等功能这块是由水机自身所镶嵌的一块智能PAD屏所完成,其PAD内部安装的是安卓系统;
在由开发完成新功能的开发后需迭代智能PAD屏内部安卓系统APK版本时,在更新版本这块我们一直做的方法是全量更新不做任何更新上的限制。但随着业务量的增加全国大概有30万台水机版本需要更新,显然之前的更新方式不再适用于现有这种高业务量的需求了。所以我们考虑了一个新的更新方案 "按地区更新";

参考的方案

一、最直接的方案是购买阿里云的CDN,利用CDN的缓存来实现。缓存原理如下

二、采用灰度发布

Nginx+GeoIP Modules+GeoIP Datebase

[root@node1-master nginx-1.14.2]# cd /usr/share/GeoIP
[root@node1-master GeoIP]# gzip -d GeoLiteCity.dat.gz
[root@node1-master GeoIP]# ln -sv GeoLiteCity.dat GeoCity.dat
[root@node1-master ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz 
[root@node1-master ~]# tar xf nginx-1.14.2.tar.gz && cd nginx-1.14.2 
[root@node1-master nginx-1.14.2]# nginx -V
[root@node1-master nginx-1.14.2]# ./configure \
--user=nginx \
--group=nginx  \
--with-http_stub_status_module \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--with-threads \
--with-http_ssl_module \
--with-pcre --with-pcre=/usr/local/nginx/modules/pcre-8.30 \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-stream --with-http_slice_module \
--with-cc-opt=-DTCP_FASTOPEN=23 \
--add-module=/usr/local/nginx/modules/ngx_cache_purge-2.3 \
--add-module=/usr/local/nginx/modules/nginx_upstream_check_module \
--add-module=/usr/local/nginx/modules/file-md5-master \
--add-module=/usr/local/nginx/modyles/nginx-sticky
--with-http_geoip_module  //此模块即为编译时所加入的静态geoip模块
[root@node1-master nginx-1.14.2]# make
[root@node1-master nginx-1.14.2]# cp ./objs/nginx /usr/local/nginx/sbin/nginx
[root@node1-master nginx-1.14.2]# nginx -V
会显示--with-http_geoip_module此模块证明编译成功

Nginx 配置文件参考

user root root;
worker_processes  auto;
worker_cpu_affinity auto;
error_log /usr/local/nginx/logs/error.log  crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log  main;

##定义GeoIP数据库及存储路径## 
geoip_city /usr/share/GeoIP/GeoCity.dat;
geoip_proxy 192.168.0.0/24;
geoip_proxy 192.168.1.0/24;
geoip_proxy_recursive on;

##结合GeoIP数据库并引入map指令来自定义变量,充分发挥作用##
map $geoip_city $no_allowed_region {
default 1;
Shanghai yes;
Beijing yes;
Tianjin yes;
}

server {
listen 80;
listen 443 default_server;
server_name you.domain.com;
ssl on;
ssl_certificate  /usr/local/nginx/ssl/you.domain.com.crt;
ssl_certificate_key  /usr/local/nginx/ssl/you.domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
root   /html;
index index.html index.htm;
try_files /index_$geoip_city.html ./index.html;

location /static {
root   /data;
    }
location ~ ^/api/pad/available {
default_type application/json;
return 200 '{"success":true}';
    }
#变量判断#
location /files/pad/yim-1.2.9.apk {
imit_conn addr 10;
limit_rate 100k;
if ($no_allowed_region = 1) {
return 550;
        }
    }
#变量判断#
location /files/pad/yimpad-version.json {
limit_conn addr 10;
limit_rate 100k;
if ($no_allowed_region = 1) {
return 550;
        }
    }
location ~ ^/static/water/material {
root       /data/files;
limit_conn addr 10;
limit_rate 100k;
        }
    }
}

从GeoIP升级到GeoIP2

[root@node1-master ~]# yum groupinstall -y Development Tools
[root@node1-master ~]# yum install -y pcre-devel openssl openssl-devel zlib-devel
official help site https://github.com/maxmind/libmaxminddb  
[root@node1-master ~]# cd /usr/local/src && git clone --recursive https://github.com/maxmind/libmaxminddb
[root@node1-master ~]# cd libmaxminddb
[root@node1-master ~]# ./bootstrap
[root@node1-master ~]# ./configure
[root@node1-master ~]# make
[root@node1-master ~]# make install
[root@node1-master ~]# sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
[root@node1-master ~]# ldconfig
参考"Nginx+GeoIP Modules+GeoIP Datebase"编译nginx的方法,一定要带上原有参数
[root@node1-master ~]# nginx -V
[root@node1-master ~]# cd /usr/local/nginx/modules
[root@node1-master ~]# git clone --recursive https://github.com/leev/ngx_http_geoip2_module
[root@node1-master ~]# cd /root/nginx-1.14.2
[root@node1-master nginx-1.14.2]# ./configure --原有参数 --add-module=/usr/local/nginx/modules/ngx_http_geoip2_module
[root@node1-master nginx-1.14.2]# make 
[root@node1-master nginx-1.14.2]# mv /usr/local/nginx/sbin/nginx{,.baks}
[root@node1-master nginx-1.14.2]# cp objs/nginx /usr/local/nginx/sbin
[root@node1-master nginx-1.14.2]# make upgrade
如果在make upgrade的时候报错 "make: *** [upgrade] error 1" 先完全kill掉nginx主进程,然后使用-c选项启动nginx,再次执行make upgrade
[root@node1-master ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
这里把ngx_http_geoip2_module构建为静态模块,如果构建为动态模块需Nginx版本大于1.9.11+
official help site https://dev.maxmind.com/geoip/geoip2/geolite2
[root@node1-master ~]# wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
[root@node1-master ~]# wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
[root@node1-master ~]# tar xf GeoLite2-City.tar.gz -C /usr/share/GeoIP2
[root@node1-master ~]# tar xf GeoLite2-Country.tar.gz -C /usr/share/GeoIP2
##geoip2 with##
geoip2 /usr/share/GeoIP2/GeoLite2-City.mmdb {
auto_reload 60m;
$geoip2_metadata_city_build metadata build_epoch;
$geoip2_data_city_name city names en;
$geoip2_data_city_name default=Shanghai city names en;
}

##map geoip##
map $geoip2_data_city_name  $default_city_list {
default 1;
Shanghai yes;
}

##server or location##
if ($default_city_list = 1) {
        return 770;
}

测试命令 mmdblookup

标签:Database,GeoIP,nginx,灰度,usr,node1,master,root
来源: https://blog.51cto.com/51eat/2465005