Nginx + lua +[memcached,redis]-实现网站灰度发布
精品案例
1、Nginx + lua +[memcached,redis]实现网站灰度发布
2、分库分表/基于Leaf组件实现的全球唯一ID(非UUID)
3、Redis独立数据监控,实现订单超时操作/ MQ死信操作 Select Poll Epoll Reactor模型
4、分布式任务调试Quartz应用 docker+k8s
5、Zookeeper+Kafka实战应用之流量削峰
6、基于Netty+WebSocket实现的聊天室 BIO NIO AIO Netty 基于事件驱动
7、分布式文件系统(HDFS|FastDFS)
8、OAuth2协议- 咚咚接口调用
9、基于Redis实现SSO单点登录
10、前后端分离之-Restful接口开发规范 10条规范 DRF django restframe workd
Nginx + lua +[memcached,redis]-实现网站灰度发布
lua脚本
js,python,dart,go 语法很相通,类似于Shell,强类型语言与弱类型语言
print("hello lua")
openresty安装,ump.jd.com
wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
tar zxf ngx_openresty-1.9.3.2.tar.gz
cd ngx_openresty-1.9.3.2
./configure --prefix=/soft/openresty --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
make && make install
nginx配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main \'$remote_addr - $remote_user [$time_local] "$request" \'
\'$status $body_bytes_sent "$http_referer" \'
\'"$http_user_agent" "$http_x_forwarded_for"\';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream java_prod {
server 192.168.23.164:8080;
}
upstream java_test {
server 192.168.23.164:9090;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
location /myip {
default_type \'text/plain\';
content_by_lua \'
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("Forwarded_IP:",clientIP)
if clientIP == nli then
clientIP = ngx.var.remote_addr
ngx.say("Remote_IP:",clientIP)
end
\';
}
location / {
default_type \'text/plain\';
content_by_lua_file /soft/dep.lua;
}
location @java_prod {
proxy_pass http://java_prod;
include proxy_params;
}
location @java_test {
proxy_pass http://java_test;
include proxy_params;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache\'s document root
# concurs with nginx\'s one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
memcache安装:
yum install memcached -y
systemctl start memcached
systemctl enable memcached
key-value
telnet 192.168.23.164 11211
add
set
get
delete
stats
flush_all
lua脚本
--获取x-real-ip
clientIP = ngx.req.get_headers()["X-Real-IP"]
--如果IP为空-取x_forwarded_for
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
--如果IP为空-取remote_addr
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
--定义本地,加载memcached
local memcached = require "resty.memcached"
--实例化对象
local memc, err = memcached:new()
--判断连接是否存在错误
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end
--建⽴memcache连接
local ok, err = memc:connect("127.0.0.1", 11211)
--⽆法连接往前端抛出错误信息
if not ok then
ngx.say("failed to connect: ", err)
return
end
--获取对象中的ip-存在值赋给res
local res,flags,err = memc:get(clientIP)
--
--ngx.say("value key: ",res,clientIP)
if err then
ngx.say("failed to get clientIP ", err)
return
end
--如果值为1则调⽤local-@java_test
if res == "1" then
-- ngx.say("yes")
ngx.exec("@java_test")
return
end
--否则调⽤local-@java_prod
ngx.exec("@java_prod")
return
java与tomcat安装
yum install java -y
tomcat准备