SpringBoot actual e-business project mall (30K + STAR) address: github.com/macrozheng/…

Abstract

In order to increase the storage capacity and response speed of Redis, we sometimes need to build Redis clusters. This article mainly describes the steps of setting up Redis cluster environment and how to integrate Redis cluster in SpringBoot.

Redis cluster setup

Here we use the most convenient way to build, using Docker Compose to build, do not know Docker Compose friends can refer to the “Using Docker Compose Deployment SpringBoot application”. We will set up a 6-node Redis cluster, including 3 master nodes and 3 slave nodes.

  • Before setting up the Redis cluster, we need to modify the Redis configuration file Redis. Conf, which can be downloaded from github.com/antirez/red…

  • You need to modify the following attributes, including modifying some cluster configurations and running ports. Change the port numbers to 6391 to 6396 as required:

Enable the cluster function
cluster-enabled yes
Set the run port
port 6391
Set the node timeout in milliseconds
cluster-node-timeout 15000
# Internal cluster configuration file
cluster-config-file "nodes-6391.conf"
Copy the code
  • Then we need to write docker-comemess. yml file for orchestrating 6 Redis containers.
version: "3"
services:
  redis-master1:
    image: Redis: 5.0 # Base image
    container_name: redis-master1 # container name
    working_dir: /config Change the working directory
    environment: # Environment variables
      - PORT=6391 The config/nodes-${PORT}. Conf file will be used
    ports: # Map ports to provide services externally
      - 6391: 6391 # Redis service port
      - 16391: 16391 # redis cluster monitor port
    stdin_open: true Open standard input
    tty: true # Background run does not exit
    network_mode: host Use host mode
    privileged: true Have the permission to execute commands in the container
    volumes:
      - /mydata/redis-cluster/config:/config Config file directory mapping to host
    entrypoint: Set the default launcher 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
Copy the code
  • From docker-comemage. yml file, we can see that our Redis container runs on ports 6391~6396 respectively, and maps the /config configuration directory in the container to the /mydata/redis-cluster/config directory of the host machine. The redis. Sh script is used as the startup script of the container.

  • The redis. Sh script starts the Redis container according to the PORT attribute of the environment variable.

redis-server  /config/nodes-${PORT}.conf
Copy the code
  • Next we need to combine the Redis configuration file with theredis.shUploaded to a Linux server/mydata/redis-cluster/configDirectory;

  • Next, upload our docker-composing. Yml file to the Linux server and use the docker-compose command to start all containers.
docker-compose up -d
Copy the code
  • The following information is displayed during startup.

  • Enter one of the Redis containers and initialize the Redis cluster.
Enter the Redis container
docker exec -it redis-master1 /bin/bash
Initializing the Redis cluster commandRedis -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 1Copy the code
  • The cluster creation process will ask you to confirm the configuration, enteryesConfirm;

  • After the Redis cluster is created successfully, the following information is displayed:

  • We can use it after it is created successfullyredis-cliThe command connects to one of the Redis services;
# Single machine mode startupRedis -cli -h 127.0.0.1 -p 6391Start in cluster modeRedis -cli -c -h 127.0.0.1 -p 6391Copy the code
  • After throughcluster nodesYou can run the command to view node information. It is found that the node information meets the original expectation of three primary nodes and three secondary nodes.

Redis clusters are used in SpringBoot

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

  • We modified the application. Yml configuration file and added the Redis cluster configuration.
spring:
  redis:
Host: 192.168.6.139
Redis database index (default: 0)
# port: 6379 # Redis server connection port
    password: # Redis server connection password (default null)
    timeout: 3000ms Connection timeout
    lettuce:
      pool:
        max-active: 8 # Maximum number of connections in the pool
        max-idle: 8 # Maximum number of free connections in the connection pool
        min-idle: 0 Minimum number of free connections in the connection pool
        max-wait: - 1ms A negative value indicates that there is no limit
    cluster:
      nodes:
        - 192.1686.139.: 6391
        - 192.1686.139.: 6392
        - 192.1686.139.: 6393
        - 192.1686.139.: 6394
        - 192.1686.139.: 6395
        - 192.1686.139.: 6396
Copy the code
  • At this point, we call the interface to get brand details again, and the brand information will be cached in the Redis cluster.

  • Because Redis containers redis-master1 and Redis-Slave2 are master and slave of each other, the same brand details are cached in both containers.

Configuration file address

Github.com/macrozheng/…

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.