Spring Boot Redis集群Docker组合

oo7oh9g9  于 2022-12-23  发布在  Spring
关注(0)|答案(1)|浏览(161)

我正在努力创建一个Docker Compose来创建一个Redis集群。我看到Bitnami有一个Redis集群映像,我尝试了一下,但我的Sping Boot 应用程序无法连接到它,因为下面的错误:

我尝试了另一种方法,创建2个Redis示例主从,我可以连接到它。现在我尝试创建6个Redis示例,然后用下面的命令创建一个3主3从的Redis集群:

redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 \
127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1

但当我执行命令时它说

Could not connect to Redis at 127.0.0.1:6380: Connection refused

下面是我当前的Docker-compose.yaml:

version: '3.8'
services:
  redis-node-0:
    image: redis:latest
    container_name: redis-0
    ports:
      - "6380:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-0:/redis/data

  redis-node-1:
    image: redis:latest
    container_name: redis-1
    ports:
      - "6381:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]  
    volumes:
      - redis-cluster_data-1:/redis/data

  redis-node-2:
    image: redis:latest
    container_name: redis-2
    ports:
      - "6382:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-2:/redis/data

  redis-node-3:
    image: redis:latest
    container_name: redis-3
    ports:
      - "6383:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-3:/redis/data

  redis-node-4:
    image: redis:latest
    container_name: redis-4
    ports:
      - "6384:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-4:/redis/data

  redis-node-5:
    image: redis:latest
    container_name: redis-5
    ports:
      - "6385:6379"
    command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
    volumes:
      - redis-cluster_data-5:/redis/data

networks: 
  default:
    name: overlay
volumes:
  redis-cluster_data-0:
    driver: local
  redis-cluster_data-1:
    driver: local
  redis-cluster_data-2:
    driver: local
  redis-cluster_data-3:
    driver: local
  redis-cluster_data-4:
    driver: local
  redis-cluster_data-5:
    driver: local

我对Docker和Redis都是全新的,我正在学习,所以任何帮助都将不胜感激。

h79rfbju

h79rfbju1#

实现这一点的方法并不明显,因为Redis集群不容易与Docker桥接网络一起工作。建立单节点集群最简单的方法是欺骗并将其绑定到127.0.0.1:

version: '3.8'

services:
  redis-single-node-cluster:
    image: docker.io/bitnami/redis-cluster:7.0
    environment:
      - 'ALLOW_EMPTY_PASSWORD=yes'
      - 'REDIS_CLUSTER_REPLICAS=0'
      - 'REDIS_NODES=127.0.0.1 127.0.0.1 127.0.0.1'
      - 'REDIS_CLUSTER_CREATOR=yes'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=127.0.0.1'
    ports:
      - '6379:6379'

然后,您可以使用以下Sping Boot 配置连接到它:

spring:
  redis:
    cluster:
      nodes: [localhost:6379]
    ssl: false

如果你想设置多个节点,你必须创建一个自定义网络并分配静态IP,你可能还需要设置network_mode: host

相关问题