In the docker environment, the redis cluster is built every second, and even springboot is integrated!

Time:2020-10-31

Springboot practical e-commerce project Mall (30K + star) address: https://github.com/macrozheng/mall

abstract

In order to improve the storage capacity and response speed of redis, sometimes we need to build a redis cluster. This paper describes the main steps of using redis to build a cluster in the spring boot environment.

Redis cluster building

For the most inconvenient way to use docker, please refer to composer. 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 fileredis.conf, the file can be downloaded from: https://github.com/antirez/re…
  • The attributes that need to be modified are as follows: some cluster configuration and running ports are modified, and the port number needs to be changed to 6391 ~ 6396 as required
#Enable cluster function
cluster-enabled yes
#Set run port
port 6391
#Set the node timeout in milliseconds
cluster-node-timeout 15000
#Cluster internal configuration file
cluster-config-file "nodes-6391.conf"
  • Then we need to write docker- compose.yml The file is used to arrange 6 redis containers. Please refer to the following notes for the role of specific attributes;
version: "3"
services:
  redis-master1:
    image:  redis:5.0 #  base image 
    container_ Name: redis-master1 ා container name
    working_ Dir: / config ා switch working directory
    Environment: environment variable
      -Port = 6391 ා will use the configuration file of config / nodes - ${port}. Conf
    Ports: Map ports and provide services to the outside world
      -6391: 6391 ා redis service port
      -16391: 16391 ා redis cluster monitoring port
    stdin_ Open: true ා standard input open
    TTY: true ා background running does not exit
    network_ Mode: host ා use host mode
    Privileged: true ා has permission to execute commands in the container
    volumes:
      -/ mydata / redis cluster / config: ා the configuration file directory is mapped to the host
    Entrypoint: set the default startup program for 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 docker- compose.yml In the file, we can see that our redis container runs on the six ports 6391-6396 respectively,

Place the/configThe configuration directory is mapped to the host’s/mydata/redis-cluster/configTable of contents withredis.shThe script is used as the startup script of the container;

  • redis.shThe role of the script is based on thePORTProperty to specify the configuration file to start the redis container;
redis-server  /config/nodes-${PORT}.conf
  • Next, we need to combine the redis configuration file with theredis.shUpload to Linux server/mydata/redis-cluster/configDirectory;

  • 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 startup;

  • At this point, enter one of the redis containers and 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
  • During the cluster creation process, you will be asked to confirm the configuration and enteryesConfirm it;

  • After the redis cluster is successfully created, the following information will be output;

  • We can use theredis-cliCommand to connect to one of the redis services;
#Stand alone mode startup
redis-cli -h 127.0.0.1 -p 6391
#Cluster mode start
redis-cli -c -h 127.0.0.1 -p 6391
  • After that, it was approvedcluster nodesThe command can view the node information and find that it meets the expectation of the original 3 masters and 3 slaves.

Using redis cluster in springboot

We are in spring data redis best practices! 》How to use redis in springboot is described in. This time, we will talk about how to use redis cluster service.

  • We modify and modify the original code application.yml Configuration file, add 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 connection pool
        Max idle: 8 ා maximum number of free connections in connection pool
        Min idle: 0 ා minimum number of free connections in connection pool
        Max wait: - 1ms ා maximum blocking waiting time of connection pool; negative value indicates that there is 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 point, we call the interface to obtain the brand details again, and the brand information will be cached in the redis cluster;
  • Due to redis containerredis-master1andredis-slave2They are master-slave, so they all cache the same brand details.

Profile address

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

Project source code address

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

official account

In the series of the complete learning course of mall project,Official accountGet it the first time.