Build a Redis cluster in seconds under Docker environment, and even SpringBoot is ready!

Posted May 27, 20205 min read

SpringBoot combat e-commerce project mall(30k + star) address: https://github.com/macrozheng/mall

Summary

In order to improve the storage capacity and response speed of Redis, sometimes we need to build a Redis cluster. This article mainly describes the construction steps of Redis cluster environment and how to integrate and use Redis cluster in Spring Boot.

Redis cluster construction

Here we use the most convenient way to build, use Docker Compose to build, friends who do not know Docker Compose can refer to "Deploying SpringBoot Application Using Docker Compose" . We will build a 6-node Redis cluster, including 3 master nodes and 3 slave nodes.

  • Before building a Redis cluster, we need to modify the Redis configuration file redis.conf, the download address of the file:[ https://github.com/antirez/re...] ( https://github . com/antirez/redis/blob/5.0/redis.conf)

  • The attributes that need to be modified are as follows, mainly because some cluster configuration and operation ports are modified, and the port number needs to be modified to 6391 ~ 6396 as needed:

    Enable cluster function

    cluster-enabled yes

    Set the running port

    port 6391

    Set the node timeout time in milliseconds

    cluster-node-timeout 15000

    Cluster internal configuration file

    cluster-config-file "nodes-6391.conf"

  • Then we need to write docker-compose.yml file for orchestration of 6 Redis containers, the role of specific attributes can refer to the following note

    version:"3"
    services:

    redis-master1:
      image:redis:5.0 # basic image
      container_name:redis-master1 # container name
      working_dir:/config # switch working directory
      environment:# environment variable
        -PORT = 6391 # Will use the configuration file config/nodes-${PORT} .conf
      ports:# Map ports to provide external services
        -6391:6391 # redis service port
        -16391:16391 # redis cluster monitoring port
      stdin_open:true # standard input opens
      tty:true # Do not exit when running in the background
      network_mode:host # Use host mode
      privileged:true # Have the permission to execute commands in the container
      volumes:
        -/mydata/redis-cluster/config:/config #Map the configuration file directory to the host
      entrypoint:# Set the default startup program of the service
        -/bin/bash
        -redis.sh
    redis-master2:
      image:redis:5.0
      working_dir:/config
      container_name:redis-master2
      environment:
        -PORT = 6392
      ports:
        -6392:6392
        -16392:16392
      stdin_open:true
      network_mode:host
      tty:true
      privileged:true
      volumes:
        -/mydata/redis-cluster/config:/config
      entrypoint:
        -/bin/bash
        -redis.sh
    redis-master3:
      image:redis:5.0
      container_name:redis-master3
      working_dir:/config
      environment:
        -PORT = 6393
      ports:
        -6393:6393
        -16393:16393
      stdin_open:true
      network_mode:host
      tty:true
      privileged:true
      volumes:
        -/mydata/redis-cluster/config:/config
      entrypoint:
        -/bin/bash
        -redis.sh
    redis-slave1:
      image:redis:5.0
      container_name:redis-slave1
      working_dir:/config
      environment:
        -PORT = 6394
      ports:
        -6394:6394
        -16394:16394
      stdin_open:true
      network_mode:host
      tty:true
      privileged:true
      volumes:
        -/mydata/redis-cluster/config:/config
      entrypoint:
        -/bin/bash
        -redis.sh
    redis-slave2:
      image:redis:5.0
      working_dir:/config
      container_name:redis-slave2
      environment:
        -PORT = 6395
      ports:
        -6395:6395
        -16395:16395
      stdin_open:true
      network_mode:host
      tty:true
      privileged:true
      volumes:
        -/mydata/redis-cluster/config:/config
      entrypoint:
        -/bin/bash
        -redis.sh
    redis-slave3:
      image:redis:5.0
      container_name:redis-slave3
      working_dir:/config
      environment:
        -PORT = 6396
      ports:
        -6396:6396
        -16396:16396
      stdin_open:true
      network_mode:host
      tty:true
      privileged:true
      volumes:
        -/mydata/redis-cluster/config:/config
      entrypoint:
        -/bin/bash
        -redis.sh
  • From the docker-compose.yml file, we can see that our Redis containers are running on the six ports 6391 ~ 6396,

The /config configuration directory in the container is mapped to the host s/mydata/redis-cluster/config directory, and the redis.sh script is also used as the startup script of the container

  • The role of redis.sh script is to start the Redis container with the specified configuration file according to the PORT attribute in the environment environment variable;

    redis-server /config/nodes-${PORT}.conf

  • Next we need to upload the Redis configuration file and redis.sh to the/mydata/redis-cluster/config directory of the Linux server;

  • Next upload our docker-compose.yml file to the Linux server, and use the docker-compose command to start all containers;

    docker-compose up -d

  • The following information will be output during the startup process;

  • At this point, enter one of the Redis containers to initialize the Redis cluster;

    Enter Redis container

    docker exec -it redis-master1/bin/bash

    Initialize Redis cluster command

    redis-cli --cluster create
    192.168.6.139:6391 192.168.6.139:6392 192.168.6.139:6393
    192.168.6.139:6394 192.168.6.139:6395 192.168.6.139:6396
    --cluster-replicas 1

  • The cluster creation process will let you confirm the configuration, enter yes to confirm;

  • The following information will be output after the Redis cluster is successfully created;

  • After successful creation, we can use the redis-cli command to connect to one of the Redis services;

    Start in stand-alone mode

    redis-cli -h 127.0.0.1 -p 6391

    Start in cluster mode

    redis-cli -c -h 127.0.0.1 -p 6391

  • After that, you can view the node information through the cluster nodes command and find that it meets the expectations of the original 3 master and 3 slave.

Using Redis cluster in SpringBoot

We are in Spring Data Redis Best Practices! talked about how to use Redis in SpringBoot, using a single node Redis service, this time we talk about how to use Redis cluster service.

  • We remodeled on the basis of the original code, modified the application.yml configuration file, and added Redis cluster configuration;

    spring:

    redis:

    host:192.168.6.139 # Redis server address

    database:0 # Redis database index(default is 0)

    port:6379 # Redis server connection port

      password:# Redis server connection password(default is blank)
      timeout:3000ms # connection timeout
      lettuce:
        pool:
          max-active:8 # Maximum number of connections in the connection pool
          max-idle:8 # Maximum number of idle connections in the connection pool
          min-idle:0 # The minimum number of idle connections in the connection pool
          max-wait:-1ms # connection pool maximum blocking wait time, negative value means no limit
      cluster:
        nodes:
          -192.168.6.139:6391
          -192.168.6.139:6392
          -192.168.6.139:6393
          -192.168.6.139:6394
          -192.168.6.139:6395
          -192.168.6.139:6396
  • At this time, we call the interface for obtaining brand details again, and the brand information will be cached in the Redis cluster;

  • Since the Redis containers redis-master1 and redis-slave2 are masters and slaves of each other, the same brand details are cached in them.

Configuration file address

https://github.com/macrozheng/mall-learning/tree/master/document/redis-cluster

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-redis

No public

mall project In the serialization of the full set of learning tutorials, follow the public number to get it for the first time.

Public Account Picture