首頁 > 軟體

Nginx代理Redis哨兵主從設定的實現

2022-07-11 18:00:49

一、環境

Nginx版本:1.21.6
Center7.5及以上或Mas OS
搭建Redis哨兵主從模式
Springboot整合Redis哨兵主從模式

提示:Nginx必須安裝upstream模組

二、設定

Nginx設定有三個方案(詳見後續內容),其中:方案一將哨兵所有節點使用一個埠對外對映;方案二是每個哨兵埠設定一個對應的對映埠,總體和正常哨兵設定方式無差別;方案三其實就是方案一和方案二的結合體。個人認為其意義不大,有興趣的小夥伴可以自行嘗試。

2.1、方案一(推薦)

# stream模組設定和http模組在相同級別
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
}

2.2、方案二

# stream模組設定和http模組在相同級別
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
    }
    upstream redis1 {
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
    }
    upstream redis2 {
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
    server {
        listen 5433;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis1;
    }
    server {
        listen 5434;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis2;
    }
}

2.3、方案三

# stream模組設定和http模組在相同級別
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    upstream redis1 {
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    upstream redis2 {
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
    server {
        listen 5433;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis1;
    }
    server {
        listen 5434;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis2;
    }
}

到此這篇關於Nginx代理Redis哨兵主從設定的實現的文章就介紹到這了,更多相關Nginx代理Redis哨兵主從內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com