Nginx代理Redis哨兵主從配置的實現

一、環境

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哨兵主從內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: