首先 安裝好 Consul upsync
然后:
1、配置安裝Nginx
需要做配置,包括分組之類的,創建目錄,有些插件是需要存放在這些目錄的
groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
2、編譯Nginx
cd /home/nginx/nginx-1.9.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --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=/home/upsync/nginx-upsync-module-master
解讀:

注意,如果出現這個錯誤:

解決辦法
yum -y install openssl openssl-devel
最后: make && make install
下面開始配置Nginx了
Upstream(上游服務器) 動態配置
##動態去consul 獲取注冊的真實反向代理地址
upstream toov5{
#自己虛擬出的 作為監聽用的 端口號 固定死的
server 127.0.0.1:11111;
#連接consul server動態獲取upstream配置負載均衡信息 間隔0.5秒讀取一次 使用192.168.212.134:8500 原生的 他自己虛擬出來的 不要亂改! 讀取的是toov5 這個分組的!!!
upsync 192.168.91.5:8500/v1/kv/upstreams/toov5 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
#動態拉取consulServer相關負載均衡配置信息持久化到硬盤上
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}
server {
listen 80;
server_name localhost;
location / {
#配置反向代理
proxy_pass http://toov5;
index index.html index.htm;
}
}
解讀:
upsync指令指定從consul哪個路徑拉取上游服務器配置;upsync_timeout配置從consul拉取上游服務器配置的超時時間;upsync_interval配置從consul拉取上游服務器配置的間隔時間;upsync_type指定使用consul配置服務器;strong_dependency配置nginx在啟動時是否強制依賴配置服務器,如果配置為on,則拉取配置失敗時nginx啟動同樣失敗。upsync_dump_path指定從consul拉取的上游服務器后持久化到的位置,這樣即使consul服務器出問題了,本地還有一個備份。
注意:替換 consul 注冊中心地址
創建upsync_dump_path
mkdir /usr/local/nginx/conf/servers/
upsync_dump_path指定從consul拉取的上游服務器后持久化到的位置,這樣即使consul服務器出問題了,本地還有一個備份。
配置時候的狀態:
#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;
##動態去consul 獲取注冊的真實反向代理地址
upstream toov5{
#自己虛擬出的 作為監聽用的 端口號 固定死的
server 127.0.0.1:11111;
#連接consul server動態獲取upstream配置負載均衡信息 間隔0.5秒讀取一次
upsync 192.168.91.5:8500/v1/kv/upstreams/toov5 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
#動態拉取consulServer相關負載均衡配置信息持久化到硬盤上
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
}
server {
listen 80;
server_name localhost;
location / {
#配置反向代理
proxy_pass http://toov5;
index index.html index.htm;
}
}
# server {
# listen 80;
# server_name localhost;
# #charset koi8-r;
# #access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
# }
#
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }
#
# }
}
然后啟動consul : ./consul agent -dev -ui -node=consul-dev -client=192.168.91.5
啟動 Nginx ./nginx
本地啟動三台:8080 8081 8082

然后開始做動態負載均衡了!添加nginx Upstream服務開始:
1.使用linux命令方式發送put請求 添加這個 192.168.8.159:8081 到上游服務
curl -X PUT http://192.168.91.5:8500/v1/kv/upstreams/toov5/192.168.8.159:80812\
2、 使用postman方式

然后圖形化界面:

訪問:

看看Nginx的本地:

成功了哦
配置權重:
{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}

他可以實現輪訓,故障轉移哦。默認的~
看這里:

被同步過來了額

