Redis environment construction (integrate SprinigBoot to switch between cluster and stand-alone environment with one click)

Introduction to Redis

Redis uses C language to develop an open source high-performance key-value pair (key-value) database. It adapts to storage requirements in different scenarios by providing multiple key-value data types. Common data types are as follows:

  • String type
  • Hash type
  • List type
  • Collection type
  • Ordered collection type

This article uses the environment: CentOS7, Redis3.0.0 version demonstration

Application scenarios of Redis

  • Implement application caching (content that is not frequently changed)
  • Website visit statistics
  • Combine lua to realize interface frequency access control
  • Implement distributed locks

Stand-alone version installation

To install reids, you need to compile the source code downloaded from the official website. The compilation environment depends on the gcc environment. If there is no gcc environment, you need to install gcc: yum install gcc-c++

Redis Chinese official website: http://www.redis.cn
Source package download: http://download.redis.io/releases/redis-3.0.0.tar.gz

  1. Copy reids-3.0.0.tar.gz to /usr/local
  2. Unzip the source code tar -zxvf reids-3.0.0.tar.gz
  3. Enter the decompressed directory and compile cd /usr/local/redis-3.0.0 & make
  4. Install to the specified directory: such as /usr/local/redis, cd /usr/local/redis-3.0.0 & make PREFIX=/usr/local/redis install
  5. Find the redis.conf file in the source package, which is the configuration file of redis. The default port is port=6379
  6. Copy to redis.conf /usr/local/redis/bin under the cp source package in the installation directory
  7. There are two startup methods, front-end startup and back-end startup.
    Front-end startup: enter the /usr/local/redis/bin directory and
    ./redis-server can start the back-end startup: you need to modify the redis.conf configuration file daemonize yes is not started in back-end mode. Command: cd /usr/local/redis/bin ./redis-server ./redis.conf
  8. Reids shutdown command: cd /usr/local/redis/bin ./redis-cli shutdown

Cluster version installation

The principle is not explained: please refer to the principle of Taotao redis cluster. The explanation is very clear. Part of the environment installation introduction in this article comes from Taotao.

  1. The redis cluster management tool redis-trib.rb depends on the ruby ​​environment. First, you need to install the ruby ​​environment.
    Install ruby ​​yum install ruby ​​& yum install rubygems
  2. Install ruby ​​and redis interface programs. Copy the downloaded reids-3.0.0.gem (download in advance) to /usr/local
    Execute: gem install /usr/local/reids-3.0.0.gem
  3. Cluster node planning
    What is implemented here is a pseudo cluster, with different port numbers representing different nodes. As follows:
    Master node: 192.168.15.130:7001 192.168.15.130:7002 192.168.15.130:7003
    Slave node: 192.168.15.130:7004 192.168.15.130:7005 192.168.15.130:7006
  4. Create a redis-cluster directory under /usr/local, and create directories 7001, 7002...7006 under it.
  5. Copy the files in the redis installation directory bin to each 700x directory, and colleagues copy the redis-trib.rb file in the redis source directory src to the redis-clulster directory. (If there are rdb or dump files, please delete them first, and copy them to the 700x directory, otherwise the startup will fail)
  6. Modify the reids.conf configuration file in each 700x directory:
    port xxxx
    cluster-enabled yes
  7. Configure the above content. Enter the 7001, 7002…7006 directories respectively: execute ./redis-server ./redis.conf or you can write a shell script to start it with one click. After each startup ** (use ps aux|grep redis) view the startup process**
  8. Execute redis-trib.rb. This script is a ruby ​​script and needs to rely on the ruby ​​environment. It has been installed in the previous article.
    cd /usr/local/redis-cluster directory execute:
    ./redis-trib.rb create --replicas 1 192.168.15.130:7001 192.168.15.130:7002 192.168.15.130:7003 192.168.15.130:7004 192.168.15.130:7005 192.168.15.130:7006 execute the command
    to create a cluster. Description of the above command: the reids cluster has at least 3 master nodes, and each master node has a slave node and a total of 6 nodes.
    Replicas is specified as 1, which means that each master node has a slave node.
  9. The cluster is successfully created and logged in to any redis node to query the status of the nodes in the cluster. The
    client has logged in as a cluster: ./redis-cli -c -h 192.168.15.130:7002 & cluster nodes
    where -c means to connect to redis in cluster mode, -h specifies ip address, -p specifies the port number. cluster nodes query cluster node information, cluster-info query cluster status information
  10. So far, the cluster is successfully built.
  11. Add a master node: After the
    cluster is created successfully, you can add nodes to the cluster. The following is to add a master master node. Add 7007 nodes, refer to the cluster node plan. Add a 7007 directory as a new node.
    Execute the following command: ./redis-trib.rb add-node 192.168.15.130:7007 192.168.15.130:7001
    View the cluster status cluster nodes to
    add the master node. The first ip is the ip of the newly added node, and the second ip is any ip address of the master node
  12. Hash slot redistribution After
    adding the master node, you need to hash the master node in decibels so that the master node can store data. The redis cluster has 16384 slots. Each node in the cluster allocates its own slot. You can see the slot occupancy by viewing the cluster nodes: cluster nodes
    allocate slots to the 7007 node just added:
  • Connect to the cluster: ./redis-trib-rb reshard 192.168.15.130:7001 (connect to any node in the cluster)
  • Enter the number of slots allocated
  • Enter the node of the receiving slot: check the 7007 node id through cluster nodes.
  • Enter the source node id. Enter the source node id, the slot will be taken from the source node, the allocated slot no longer exists in the source node, enter all to get the slot from all source nodes, and enter done to cancel the allocation.
  • Enter yes to start moving the slot to the target node id
  1. Add a slave node: After the cluster is successfully created, you can add a node to the cluster. The following is to add a slave node. Add a slave node of node 7008, and use 7008 as a slave node of 7007.
    ./redis-trib.rb add-node --slave --master-id master node id Add the ip and port of the node to the existing node ip and port in the cluster
    execute the following command: ./redis-trib.rb add-node --slave --master-id Master node id 192.168.15.130:7008 192.168.15.130:7001 The
    master node id is 7007 node id. Can be viewed through cluster nodes

Code: SpringBoot+Redis integration code address, which can switch between stand-alone and cluster environments with one click. (https://gitee.com/enthusiasts/redis-spring-boot-starter)
Personal gitee: https://gitee.com/enthusiasts

Guess you like

Origin blog.csdn.net/qq_37640410/article/details/108251806