精品案例
11、Nginx + lua +[memcached,redis]实现网站灰度发布 22、分库分表/基于Leaf组件实现的全球唯一ID(非UUID) 33、Redis独立数据监控,实现订单超时操作/ MQ死信操作 Select Poll Epoll Reactor模型 44、分布式任务调试Quartz应用 docker+k8s 55、Zookeeper+Kafka实战应用之流量削峰 66、基于Netty+WebSocket实现的聊天室 BIO NIO AIO Netty 基于事件驱动 77、分布式文件系统(HDFS|FastDFS) 88、OAuth2协议- 咚咚接口调用 99、基于Redis实现SSO单点登录 1010、前后端分离之-Restful接口开发规范 10条规范 DRF django restframe workd
Nginx + lua +[memcached,redis]-实现网站灰度发布
lua脚本
js,python,dart,go 语法很相通,类似于Shell,强类型语言与弱类型语言
print("hello lua")
openresty安装,ump.jd.com
1wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz 2tar zxf ngx_openresty-1.9.3.2.tar.gz 3cd ngx_openresty-1.9.3.2 4./configure --prefix=/soft/openresty --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit 5make && make install 6
nginx配置
1#user nobody; 2worker_processes 1; 3 4#error_log logs/error.log; 5#error_log logs/error.log notice; 6#error_log logs/error.log info; 7 8#pid logs/nginx.pid; 9events { 10 worker_connections 1024; 11} 12 13 14http { 15 include mime.types; 16 default_type application/octet-stream; 17 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 #access_log logs/access.log main; 23 sendfile on; 24 #tcp_nopush on; 25 26 #keepalive_timeout 0; 27 keepalive_timeout 65; 28 29 #gzip on; 30 31 upstream java_prod { 32 server 192.168.23.164:8080; 33 } 34 35 upstream java_test { 36 server 192.168.23.164:9090; 37 } 38 server { 39 listen 80; 40 server_name localhost; 41 42 #charset koi8-r; 43 44 #access_log logs/host.access.log main; 45 location /hello { 46 default_type text/html; 47 content_by_lua_block { 48 ngx.say("HelloWorld") 49 } 50 } 51 52 53 location /myip { 54 default_type 'text/plain'; 55 content_by_lua ' 56 clientIP = ngx.req.get_headers()["x_forwarded_for"] 57 ngx.say("Forwarded_IP:",clientIP) 58 if clientIP == nli then 59 clientIP = ngx.var.remote_addr 60 ngx.say("Remote_IP:",clientIP) 61 end 62 '; 63 } 64 65 location / { 66 default_type 'text/plain'; 67 content_by_lua_file /soft/dep.lua; 68 } 69 70 location @java_prod { 71 proxy_pass http://java_prod; 72 include proxy_params; 73 } 74 location @java_test { 75 proxy_pass http://java_test; 76 include proxy_params; 77 } 78 } 79 80 81 #error_page 404 /404.html; 82 # redirect server error pages to the static page /50x.html 83 # 84 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 85 # 86 #location ~ \.php$ { 87 # proxy_pass http://127.0.0.1; 88 #} 89 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 90 # 91 #location ~ \.php$ { 92 # root html; 93 # fastcgi_pass 127.0.0.1:9000; 94 # fastcgi_index index.php; 95 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 96 # include fastcgi_params; 97 #} 98 # deny access to .htaccess files, if Apache's document root 99 # concurs with nginx's one 100 # 101 #location ~ /\.ht { 102 # deny all; 103 #} 104} 105 106 107# another virtual host using mix of IP-, name-, and port-based configuration 108# 109#server { 110# listen 8000; 111# listen somename:8080; 112# server_name somename alias another.alias; 113# location / { 114# root html; 115# index index.html index.htm; 116# } 117#} 118# HTTPS server 119# 120#server { 121# listen 443 ssl; 122# server_name localhost; 123# ssl_certificate cert.pem; 124# ssl_certificate_key cert.key; 125# ssl_session_cache shared:SSL:1m; 126# ssl_session_timeout 5m; 127# ssl_ciphers HIGH:!aNULL:!MD5; 128# ssl_prefer_server_ciphers on; 129# location / { 130# root html; 131# index index.html index.htm; 132# } 133#}
proxy_params
1proxy_redirect default; 2proxy_set_header Host $http_host; 3proxy_set_header X-Real-IP $remote_addr; 4proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 5proxy_connect_timeout 30; 6proxy_send_timeout 60; 7proxy_read_timeout 60; 8proxy_buffer_size 32k; 9proxy_buffering on; 10proxy_buffers 4 128k; 11proxy_busy_buffers_size 256k; 12proxy_max_temp_file_size 256k;
memcache安装:
1yum install memcached -y 2systemctl start memcached 3systemctl enable memcached 4key-value 5telnet 192.168.23.164 11211 6add 7set 8get 9delete 10stats 11flush_all
lua脚本
1--获取x-real-ip 2clientIP = ngx.req.get_headers()["X-Real-IP"] 3--如果IP为空-取x_forwarded_for 4if clientIP == nil then 5 clientIP = ngx.req.get_headers()["x_forwarded_for"] 6end 7--如果IP为空-取remote_addr 8if clientIP == nil then 9 clientIP = ngx.var.remote_addr 10end 11--定义本地,加载memcached 12local memcached = require "resty.memcached" 13--实例化对象 14local memc, err = memcached:new() 15--判断连接是否存在错误 16if not memc then 17 ngx.say("failed to instantiate memc: ", err) 18 return 19end 20--建⽴memcache连接 21local ok, err = memc:connect("127.0.0.1", 11211) 22--⽆法连接往前端抛出错误信息 23if not ok then 24 ngx.say("failed to connect: ", err) 25 return 26end 27--获取对象中的ip-存在值赋给res 28local res,flags,err = memc:get(clientIP) 29-- 30--ngx.say("value key: ",res,clientIP) 31if err then 32 ngx.say("failed to get clientIP ", err) 33 return 34end 35--如果值为1则调⽤local-@java_test 36if res == "1" then 37 -- ngx.say("yes") 38 ngx.exec("@java_test") 39 return 40end 41--否则调⽤local-@java_prod 42 ngx.exec("@java_prod") 43 return
java与tomcat安装
yum install java -y
tomcat准备