Openresty Nginx 负载均衡
目錄
OpenResty
Openresty 服務(wù)配置文件
啟動(dòng)Openresty服務(wù)
測(cè)試調(diào)用接口
Nginx 負(fù)載均衡服務(wù)
nginx 配置文件
啟動(dòng)服務(wù)
實(shí)現(xiàn)功能
測(cè)試結(jié)果
?
這里實(shí)現(xiàn)個(gè)簡(jiǎn)單的負(fù)載均衡,只做功能展示(實(shí)際業(yè)務(wù)比這復(fù)雜高,單臺(tái)服務(wù)器無(wú)法滿(mǎn)足需求的情況下,才會(huì)進(jìn)行負(fù)載均衡)
OpenResty
用Openresty實(shí)現(xiàn)日志功能
Openresty 服務(wù)配置文件
lua 文件
-- 引入lua json庫(kù) local cjson = require "cjson" -- 獲取請(qǐng)求參數(shù) local request_args_tab = ngx.req.get_uri_args() -- 獲取系統(tǒng)時(shí)間 local time = os.date("%Y%m%d",unixtime) -- 創(chuàng)建file對(duì)象 local file = io.open("ho-data/ho-".. time ..".log","a") -- 創(chuàng)建json對(duì)象 local log_json = {} -- 將請(qǐng)求參數(shù)轉(zhuǎn)為json for k,v in pairs(request_args_tab) do log_json[k] = v end -- json數(shù)據(jù)寫(xiě)入文件,并\n換行 file:write(cjson.encode(log_json),"\n") file:flush()nginx.conf 文件
worker_processes 1; error_log logs/error.log; events {worker_connections 1024; } http {server {client_header_buffer_size 512m;large_client_header_buffers 4 512m; lua_need_request_body on;lua_code_cache off;listen 12199;location /log.gif {#internal;default_type image/gif;access_log off;log_by_lua_file 'conf/log_conf.lua';empty_gif;}location /jast {log_by_lua_file conf/jast_conf.lua;content_by_lua '#用三臺(tái)服務(wù)器做測(cè)試,調(diào)用接口后使用jast_conf.lua處理數(shù)據(jù),處理完成并返回對(duì)應(yīng)服務(wù)器標(biāo)識(shí)ngx.say("hello,89") #ngx.say("hello,44") #ngx.say("hello,121") '; }} }啟動(dòng)Openresty服務(wù)
/usr/local/openresty/nginx/sbin/nginx -p /root/openresty/openresty-1.15.8.2/jast_log -c conf/nginx.conf測(cè)試調(diào)用接口
[root@ecs-001 jast]# curl 172.16.0.89:12199/jast?userid=123&action=read&date=160000000 hello,89 [root@ecs-002 jast]# curl 172.16.0.121:12199/jast?userid=123&action=read&date=160000000 hello,121 [root@ecs-002 jast]# curl 172.16.0.44:12199/jast?userid=123&action=read&date=160000000 hello,44Nginx 負(fù)載均衡服務(wù)
nginx 配置文件
worker_processes 4; error_log logs/error.log; events {worker_connections 1024; } http {upstream jast.loadBalance{server 172.16.0.44:12199 weight=1;server 172.16.0.121:12199 weight=1;server 172.16.0.89:12199 weight=1;}server {client_header_buffer_size 512k;large_client_header_buffers 4 512k;lua_need_request_body on;lua_code_cache off;listen 2199;location / {proxy_pass http://jast.loadBalance;}} }啟動(dòng)服務(wù)
/usr/local/openresty/nginx/sbin/nginx -p /root/openresty/openresty-1.15.8.2/jast -c conf/nginx.conf實(shí)現(xiàn)功能
調(diào)用2199端口,自動(dòng)負(fù)載均衡到44:12199,121:12199,89:12199三臺(tái)服務(wù)器的服務(wù)中
測(cè)試結(jié)果
測(cè)試調(diào)用2199端口,我們只有2199開(kāi)通了外網(wǎng)端口
返回結(jié)果如下,說(shuō)明我們自動(dòng)將請(qǐng)求分發(fā)了
第1次請(qǐng)求 Hello,121第2次請(qǐng)求 Hello,44第3次請(qǐng)求 hello,89第4次請(qǐng)求 Hello,121第5次請(qǐng)求 Hello,44第6次請(qǐng)求 hello,89第7次請(qǐng)求 Hello,121第8次請(qǐng)求 Hello,44第9次請(qǐng)求 hello,89第10次請(qǐng)求 Hello,121第11次請(qǐng)求 Hello,44第12次請(qǐng)求 hello,89第13次請(qǐng)求 Hello,121第14次請(qǐng)求 Hello,44第15次請(qǐng)求 hello,89第16次請(qǐng)求 Hello,121第17次請(qǐng)求 Hello,44第18次請(qǐng)求 hello,89第19次請(qǐng)求 Hello,121第20次請(qǐng)求 Hello,44?
這個(gè)網(wǎng)站有些基本介紹:https://moonbingbing.gitbooks.io/openresty-best-practices
?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Openresty Nginx 负载均衡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ElasticSearch sql 插件
- 下一篇: Docker 服务器安装(一)