Docker environment built Redis cluster seconds, even SpringBoot also on the whole!

SpringBoot actual electricity supplier item mall (30k + star) Address: github.com/macrozheng/...

Summary

In order to increase storage capacity and speed of response Redis, sometimes we need to build Redis cluster. This paper describes the steps to build Redis cluster environment and how to integrate the use of cluster SpringBoot in Redis.

Redis Cluster Setup

Here we use the most convenient way to set up, use Docker Compose to build on Docker Compose friends do not understand can refer to the "Using Docker Compose deploy SpringBoot application" . We will build a 6-node cluster Redis, including three master node and three slave nodes.

  • Before building Redis cluster, we need to modify the configuration file under Redis redis.conf, Download the file: github.com/antirez/red...

  • Need to modify the following properties, mainly modified some cluster configuration and operation of the port, the port number needs to be modified to demand 6391 - 6396:

# 开启集群功能
cluster-enabled yes
# 设置运行端口
port 6391
# 设置节点超时时间,单位毫秒
cluster-node-timeout 15000
# 集群内部配置文件
cluster-config-file "nodes-6391.conf"
复制代码
  • Then we need to write a file layout docker-compose.yml Redis six containers, the role of specific attributes can refer to the following notation;
version: "3"
services:
  redis-master1:
    image: redis:5.0 # 基础镜像
    container_name: redis-master1 # 容器名称
    working_dir: /config # 切换工作目录
    environment: # 环境变量
      - PORT=6391 # 会使用config/nodes-${PORT}.conf这个配置文件
    ports: # 映射端口,对外提供服务
      - 6391:6391 # redis的服务端口
      - 16391:16391 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    volumes:
      - /mydata/redis-cluster/config:/config #配置文件目录映射到宿主机
    entrypoint: # 设置服务默认的启动程序
      - /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
复制代码
  • We can see from the docker-compose.yml files, we run on each container Redis 6391 ~ 6396 six ports in the container /configis mapped to the host configuration directory of /mydata/redis-cluster/configthe directory, and also to the redis.shscript as the container the startup script;

  • redis.shAction script based environment environment variable PORTattributes to specify the profile to start Redis container;

redis-server  /config/nodes-${PORT}.conf
复制代码
  • Next we need to Redis configuration file and redis.shuploaded to the Linux server /mydata/redis-cluster/configdirectory;

  • Then our docker-compose.yml upload files to the Linux server and use docker-compose command to start all containers;
docker-compose up -d
复制代码
  • During startup outputs the following information;

  • Wherein a time to enter into the container Redis, Redis initialized clusters;
# 进入Redis容器
docker exec -it redis-master1 /bin/bash
# 初始化Redis集群命令
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
复制代码
  • Creating the cluster configuration process will let you confirm, enter the yesconfirm;

  • Redis cluster outputs the following information is successfully created;

  • Once created, we can use the redis-clicommand to connect to one of the Redis service;
# 单机模式启动
redis-cli -h 127.0.0.1 -p 6391
# 集群模式启动
redis-cli -c -h 127.0.0.1 -p 6391
复制代码
  • After passing through cluster nodescan view node information command, found in line with expectations from the original 3 main 3.

SpringBoot use Redis cluster

We are best practices "Spring Data Redis! " Described how to use Redis in SpringBoot, the use of a single Redis service node, the next time we talk about how to use Redis Cluster service.

  • We transform on the basis of the original code, modify application.yml configuration file, add Redis cluster configuration;
spring:
  redis:
#    host: 192.168.6.139 # Redis服务器地址
#    database: 0 # Redis数据库索引(默认为0)
#    port: 6379 # Redis服务器连接端口
    password: # Redis服务器连接密码(默认为空)
    timeout: 3000ms # 连接超时时间
    lettuce:
      pool:
        max-active: 8 # 连接池最大连接数
        max-idle: 8 # 连接池最大空闲连接数
        min-idle: 0 # 连接池最小空闲连接数
        max-wait: -1ms # 连接池最大阻塞等待时间,负值表示没有限制
    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 to get details of the interface brand again, it will put the brand message to Redis cache in the cluster to go;

  • Since Redis container redis-master1and redis-slave2mutual master-slave, so there are caches of the same brand information details.

Profile address

github.com/macrozheng/…

Project Source Address

github.com/macrozheng/…

No public

mall project a full tutorial serialized in public concern number the first time to obtain.

No public picture

Guess you like

Origin juejin.im/post/5e81fd1ff265da47e84e5e2c