需求的產生 (分地區更新)
咱們公司呢是作電商平臺的,其主打商品裏有一款智能飲水機產品,而每臺智能飲水機產品都是裝在全國客戶的家裏。爲了售後維護須要服務端如何保存數據並傳回這塊實現較簡單,而在遠程展現水機的餘額、歸屬地、出水的質量等功能這塊是由水機自身所鑲嵌的一塊智能PAD屏所完成,其PAD內部安裝的是安卓系統;
在由開發完成新功能的開發後需迭代智能PAD屏內部安卓系統APK版本時,在更新版本這塊咱們一直作的方法是全量更新不作任何更新上的限制。但隨着業務量的增長全國大概有30萬臺水機版本須要更新,顯然以前的更新方式再也不適用於現有這種高業務量的需求了。因此咱們考慮了一個新的更新方案 "按地區更新";html
參考的方案
1、最直接的方案是購買阿里雲的CDN,利用CDN的緩存來實現。緩存原理以下前端
2、採用灰度發佈node
Nginx+GeoIP Modules+GeoIP Datebase
yum install -y geoip-devel
;[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/yimi-1.2.9.apk { imit_conn addr 10; limit_rate 100k; if ($no_allowed_region = 1) { return 550; } } #變量判斷# location /files/pad/yimipad-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; } } }
location /files/pad/yimi-1.2.9.apk { root /data; limit_conn addr 10; limit_rate 100k; if ($no_allowed_region = 1) { return 550; } }
location /files/pad/yimipad-version.json { root /data; limit_conn addr 10; limit_rate 100k; if ($no_allowed_region = 1) { return 550; } }
從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; }
nginx -s reload
測試命令 mmdblookup