摘要:實(shí)現(xiàn)無(wú)需動(dòng)態(tài)負(fù)載均衡實(shí)現(xiàn)無(wú)需動(dòng)態(tài)負(fù)載均衡環(huán)境搭建出現(xiàn)下面提示說(shuō)明成功啟動(dòng)是新浪微博開(kāi)源的基于實(shí)現(xiàn)動(dòng)態(tài)配置的三方模塊。配置動(dòng)態(tài)負(fù)載均衡創(chuàng)建指定從拉取的上游服務(wù)器后持久化到的位置,這樣即使服務(wù)器出問(wèn)題了,本地還有一個(gè)備份。
方案
傳統(tǒng)的負(fù)載均衡,如果Upstream參數(shù)發(fā)生變化,每次都需要重新加載nginx.conf文件,因此擴(kuò)展性不是很高,所以我們可以采用動(dòng)態(tài)負(fù)載均衡,實(shí)現(xiàn)Upstream可配置化、動(dòng)態(tài)化,無(wú)需人工重新加載nginx.conf,類(lèi)似分布式的配置中心
Consul+Consul-template 每次發(fā)現(xiàn)配置更改需要raload nginx,重啟Nginx。
Consul+OpenResty 實(shí)現(xiàn)無(wú)需raload動(dòng)態(tài)負(fù)載均衡
Consul+upsync+Nginx 實(shí)現(xiàn)無(wú)需raload動(dòng)態(tài)負(fù)載均衡
Consul+upsync+Nginx Consul環(huán)境搭建https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
yum -y install unzip
unzip consul_0.7.5_linux_amd64.zip
./consul 出現(xiàn)下面提示說(shuō)明成功
[root@localhost local]# ./consul usage: consul [--version] [--help][ ] Available commands are: agent Runs a Consul agent configtest Validate config file event Fire a new event exec Executes a command on Consul nodes force-leave Forces a member of the cluster to enter the "left" state info Provides debugging information for operators join Tell Consul agent to join cluster keygen Generates a new encryption key keyring Manages gossip layer encryption keys kv Interact with the key-value store leave Gracefully leaves the Consul cluster and shuts down lock Execute a command holding a lock maint Controls node or service maintenance mode members Lists the members of a Consul cluster monitor Stream logs from a Consul agent operator Provides cluster-level tools for Consul operators reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes snapshot Saves, restores and inspects snapshots of Consul server state version Prints the Consul version watch Watch for changes in Consul
5.啟動(dòng)consul
./consul agent -dev -ui -node=consul-dev -client=192.168.102.129
Upsync是新浪微博開(kāi)源的基于Nginx實(shí)現(xiàn)動(dòng)態(tài)配置的三方模塊。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并動(dòng)態(tài)更新Nginx的路由信息。此模塊不依賴(lài)于任何第三方模塊。Consul作為Nginx的DB,利用Consul的KV服務(wù),每個(gè)Nginx Work進(jìn)程獨(dú)立的去拉取各個(gè)upstream的配置,并更新各自的路由。
nginx-upsync-module:
cd /usr/local
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
unzip master.zip
unzip consul_0.7.1_linux_amd64.zip
配置Nginxtar -zxvf nginx-1.9.9.tar.gz
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
cd nginx-1.9.9/
./configure --prefix=/usr/local/nginx --user=nginx --group=root --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master
make && make install
動(dòng)態(tài)負(fù)載均衡創(chuàng)建upsync_dump_pathmkdir /usr/local/nginx/conf/servers/
upsync_dump_path指定從consul拉取的上游服務(wù)器后持久化到的位置,這樣即使consul服務(wù)器出問(wèn)題了,本地還有一個(gè)備份。
添加nginx Upstream服務(wù)
使用linux命令方式發(fā)送put請(qǐng)求
curl -X PUT http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
curl -X PUT http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
使用postmen 發(fā)送put請(qǐng)求
http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
http://192.168.102.129:8500/v1/kv/upstreams/tmp/192.168.102.128:8081
負(fù)載均衡信息參數(shù)
{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}
Upstream 動(dòng)態(tài)配置
##動(dòng)態(tài)去consul 獲取注冊(cè)的真實(shí)反向代理地址
upstream tmpup{
server 127.0.0.1:11111; upsync 192.168.212.134:8500/v1/kv/upstreams/tmp upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf; } server { listen 80; server_name localhost; location / { proxy_pass http://tmpup; index index.html index.htm; } }
upsync指令指定從consul哪個(gè)路徑拉取上游服務(wù)器配置;upsync_timeout配置從consul拉取上游服務(wù)器配置的超時(shí)時(shí)間;upsync_interval配置從consul拉取上游服務(wù)器配置的間隔時(shí)間;upsync_type指定使用consul配置服務(wù)器;strong_dependency配置nginx在啟動(dòng)時(shí)是否強(qiáng)制依賴(lài)配置服務(wù)器,如果配置為on,則拉取配置失敗時(shí)nginx啟動(dòng)同樣失敗。upsync_dump_path指定從consul拉取的上游服務(wù)器后持久化到的位置,這樣即使consul服務(wù)器出問(wèn)題了,本地還有一個(gè)備份。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/40482.html
摘要:接入層作用一的聚合。接入層作用二服務(wù)發(fā)現(xiàn)與動(dòng)態(tài)負(fù)載均衡既然統(tǒng)一的入口變?yōu)榱私尤雽樱瑒t接入層就有責(zé)任自動(dòng)的發(fā)現(xiàn)后端拆分,聚合,擴(kuò)容,縮容的服務(wù)集群,當(dāng)后端服務(wù)有所變化的時(shí)候,能夠?qū)崿F(xiàn)健康檢查和動(dòng)態(tài)的負(fù)載均衡。 此文已由作者劉超授權(quán)網(wǎng)易云社區(qū)發(fā)布。 歡迎訪問(wèn)網(wǎng)易云社區(qū),了解更多網(wǎng)易技術(shù)產(chǎn)品運(yùn)營(yíng)經(jīng)驗(yàn)。 這個(gè)系列是微服務(wù)高并發(fā)設(shè)計(jì),所以我們先從最外層的接入層入手,看都有什么樣的策略保證高并發(fā)。...
上篇文章和大家聊了 Spring Session 實(shí)現(xiàn) Session 共享的問(wèn)題,有的小伙伴看了后表示對(duì) Nginx 還是很懵,因此有了這篇文章,算是一個(gè) Nginx 掃盲入門(mén)吧! 基本介紹 Nginx 是一個(gè)高性能的 HTTP 和反向代理 web 服務(wù)器,同時(shí)也提供了 IMAP/POP3/SMTP 服務(wù)。 Nginx 是由伊戈?duì)枴べ愃饕驗(yàn)槎砹_斯訪問(wèn)量第二的 Rambler.ru 站點(diǎn)開(kāi)發(fā)...
摘要:反向代理要說(shuō)反向代理,我們就先要理解正向代理下面我們就談?wù)務(wù)虼砗头聪虼戆???蛻?hù)端才能使用正向代理。反向代理總結(jié)就一句話代理端代理的是服務(wù)端。因此,動(dòng)態(tài)資源轉(zhuǎn)發(fā)到服務(wù)器我們就使用到了前面講到的反向代理了。 反向代理 要說(shuō)反向代理,我們就先要理解正向代理 ,下面我們就談?wù)務(wù)虼砗头聪虼戆伞?正向代理 一個(gè)位于客戶(hù)端和原始服務(wù)器(origin server)之間的服務(wù)器,為了從原始...
閱讀 3416·2021-11-25 09:43
閱讀 3263·2021-10-11 10:58
閱讀 2839·2021-09-27 13:59
閱讀 3143·2021-09-24 09:55
閱讀 2233·2019-08-30 15:52
閱讀 1896·2019-08-30 14:03
閱讀 2307·2019-08-30 11:11
閱讀 2082·2019-08-28 18:12