Openresty Nginx 负载均衡
生活随笔
收集整理的這篇文章主要介紹了
Openresty Nginx 负载均衡
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
OpenResty
Openresty 服務(wù)配置文件
啟動Openresty服務(wù)
測試調(diào)用接口
Nginx 負(fù)載均衡服務(wù)
nginx 配置文件
啟動服務(wù)
實(shí)現(xiàn)功能
測試結(jié)果
?
這里實(shí)現(xiàn)個簡單的負(fù)載均衡,只做功能展示(實(shí)際業(yè)務(wù)比這復(fù)雜高,單臺服務(wù)器無法滿足需求的情況下,才會進(jìn)行負(fù)載均衡)
OpenResty
用Openresty實(shí)現(xiàn)日志功能
Openresty 服務(wù)配置文件
lua 文件
-- 引入lua json庫 local cjson = require "cjson" -- 獲取請求參數(shù) local request_args_tab = ngx.req.get_uri_args() -- 獲取系統(tǒng)時間 local time = os.date("%Y%m%d",unixtime) -- 創(chuàng)建file對象 local file = io.open("ho-data/ho-".. time ..".log","a") -- 創(chuàng)建json對象 local log_json = {} -- 將請求參數(shù)轉(zhuǎn)為json for k,v in pairs(request_args_tab) do log_json[k] = v end -- json數(shù)據(jù)寫入文件,并\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 '#用三臺服務(wù)器做測試,調(diào)用接口后使用jast_conf.lua處理數(shù)據(jù),處理完成并返回對應(yīng)服務(wù)器標(biāo)識ngx.say("hello,89") #ngx.say("hello,44") #ngx.say("hello,121") '; }} }啟動Openresty服務(wù)
/usr/local/openresty/nginx/sbin/nginx -p /root/openresty/openresty-1.15.8.2/jast_log -c conf/nginx.conf測試調(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;}} }啟動服務(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端口,自動負(fù)載均衡到44:12199,121:12199,89:12199三臺服務(wù)器的服務(wù)中
測試結(jié)果
測試調(diào)用2199端口,我們只有2199開通了外網(wǎng)端口
返回結(jié)果如下,說明我們自動將請求分發(fā)了
第1次請求 Hello,121第2次請求 Hello,44第3次請求 hello,89第4次請求 Hello,121第5次請求 Hello,44第6次請求 hello,89第7次請求 Hello,121第8次請求 Hello,44第9次請求 hello,89第10次請求 Hello,121第11次請求 Hello,44第12次請求 hello,89第13次請求 Hello,121第14次請求 Hello,44第15次請求 hello,89第16次請求 Hello,121第17次請求 Hello,44第18次請求 hello,89第19次請求 Hello,121第20次請求 Hello,44?
這個網(wǎng)站有些基本介紹:https://moonbingbing.gitbooks.io/openresty-best-practices
?
?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Openresty Nginx 负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ElasticSearch sql 插件
- 下一篇: 如何使用confd+ACM管理Nginx