Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a distributed cache redis-cluster environment and call Jedis by wishstack

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to achieve distributed cache redis-cluster environment building and wishstack call Jedis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can get something.

There are three ways of redis cluster

1. Redis's official cluster solution-Redis-cluster (just released for almost a year)

2. Twemproxy (contributed by twitter) has been used for a long time and mature, but some features are not supported.

3. Pea Tip's improved and optimized solution for Twemproxy codis

For more distributed cache scheme level selection, please refer to the blog: http://my.oschina.net/tantexian/blog/685620

Redis application scenario: solve the problem that the amount of local cache data is too small wherever you need to use the cache. Distributed cache can effectively prevent the avalanche of local cache failure database. Solve the problem that caches can not be shared in distributed applications (distributed session preservation). Caching distributed cache redis services are provided for customers in cloud computing (Qingyun, UnitedStack and other manufacturers already have)

Next, we will mainly explain how to build a Redis-cluster cluster environment and how wishstack uses Jedis to call a Redis cluster:

Mkdir cluster-test

Cd cluster-test

Mkdir 7000 7001 7002 7003 7004 7005

Yum install gcc wget-y

Wget http://download.redis.io/releases/redis-3.0.5.tar.gz

Tar xzvf redis-3.0.5.tar.gz

Cd redis-3.0.5

Make (make MALLOC=libc is used for direct make error reporting)

Yum install tcl-y

Make test

Make install

After installation, the redis-* general operation command script is copy under linux bin and can be used directly (of course, you can also use the compiled source code package. / redis-3.0.5/src/)

Copy the configuration file of the redis source package to 7000 /

Cp redis-3.0.5/redis.conf 7000/

Vi 7000/redis.conf

Modify the following options in the configuration file

Port 7000

Daemonize yes

Cluster-enabled yes

Cluster-config-file nodes-7000.conf

Cluster-node-timeout 5000

Appendonly yes

Copy the modified 7000/redis.conf file to another folder (change the redis.conf port port to port for)

Cp 7000/redis.conf 7001/

Cp 7000/redis.conf 7002/

Cp 7000/redis.conf 7003/

Cp 7000/redis.conf 7004/

Cp 7000/redis.conf 7005/

Note: the redis.conf under the 7001 folder modifies the port to 7001, and so on.

Start 6 redis instances respectively:

Redis-server. / 7000/redis.conf

Redis-server. / 7001/redis.conf

Redis-server. / 7002/redis.conf

Redis-server. / 7003/redis.conf

Redis-server. / 7004/redis.conf

Redis-server. / 7005/redis.conf

Redis-server. / 7000/redis.conf & & redis-server. / 7001/redis.conf & & redis-server. / 7002/redis.conf & & redis-server. / 7003/redis.conf & & redis-server. / 7004

Use the ruby script redis-trib.rb to execute the create cluster command of redis to create a cluster

# create subcommand of redis-trib.rb in redis3.0.5 source code package to build a cluster

Where the parameter-replicas

Specifies that each Master node in the Redis Cluster is equipped with several Slave nodes

The roles of nodes are determined by the order, first master, then slave.

To use redis-trib.rb scripts, you must have a ruby environment and a ruby interface for redis.

Yum-y install ruby ruby-devel rubygems rpm-build

Gem install redis (if the network speed is not good, use Taobao's Geme source: gem sources-- add https://ruby.taobao.org/-- remove https://rubygems.org/)

Create a redis cluster:

. / redis-3.0.5/src/redis-trib.rb create-- replicas 1 127.0.0.1 127.0.0.1:7005./redis-3.0.5/src/redis-trib.rb create 7000 127.0.1 127.0.0.1:7005./redis-3.0.5/src/redis-trib.rb create 7001 127.0.0.1 127.0.0.1:7005./redis-3.0.5/src/redis-trib.rb create 7002 127.0.1 127.0.0.1:7005./redis-3.0.5/src/redis-trib.rb create-- replicas 1 172.31.2.32 7000172.31.2.32 7001 7001. 2.32:7002 172.31.2.32:7003 172.31.2.32:7004 172.31.2.32:7005

. / redis-3.0.5/src/redis-trib.rb create-- replicas 1 127.0.0.1VR 7000 127.0.0.1V 7001 127.0.0.1V 7002 127.0.0.1R 7003 127.0.1V 7003 127.0.1V 7004 127.0.1V 7005

This command is used here to create a new cluster, and the option-replicas 1 indicates that we want to create a slave node for each master node in the cluster.

The other parameters that follow are the address list of the cluster instance, 3 master3 and slave

Redis-trib will print out a copy of the expected configuration to show you. If you think there is no problem, you can enter yes, and redis-trib will apply this configuration to the cluster, so that each node will start to communicate with each other. Finally, you can get the following information:

[OK] All 16384 slots covered

This means that all 16384 slots in the cluster are processed by at least one master node, and the cluster is operating normally.

Test redis-cluster:

Slaveof configuration item.

. / redis-trib.rb create-- replicas adds a master-slave node

Note: if the above error is reported, use redis-cli-c-p 7000 port connection, use dbsize command, if not zero, use flushall command to clear zero. If the newspaper is slave, it can't clear zero.

Then find the master corresponding to the slave and perform the above operation. Know that dbsize is 0.

Redis-cli-c-p 7000 shutdown & & redis-cli-c-p 7001 shutdown & & redis-cli-c-p 7002 shutdown & & redis-cli-c-p 7003 shutdown & & redis-cli-c-p 7004 shutdown & & redis-cli-c-p 7005 shutdown

Redis-server. / 7000/redis.conf & & redis-server. / 7001/redis.conf & & redis-server. / 7002/redis.conf & & redis-server. / 7003/redis.conf & & redis-server. / 7004

. / redis-3.0.5/src/redis-trib.rb create-- replicas 1 172.31.2.32 Virgo 7000 172.31.2.32 Viru 7002 172.31.2.32 Ru 7002 172.31.2.32 Ru 7003 172.31.2.32 Ru 7004 172.31.2.32 v 7005

Note: before del-node, you need to transfer this node. / redis-trib.rb reshard 127.0.0.1 to other nodes. Only empty instances can be deleted.

If the result of the query using keys * is empty, the data saved by the current instance is empty and can be removed using the del-node command.

Turn off the 127.0.0.1 redis-cli vig 7000: redis-cli-c-p 7000 shutdown

For more information about redis-cli operations, please refer to: http://blog.csdn.net/tantexian/article/details/49924873

Then restart redis-server above 172.31.2.33 to add the node to the redis cluster of 172.31.2.32 created above.

The first step is to install the redis base environment as in the previous steps.

Add the new running examples *: 8000 and *: 8001 above 172.31.2.33 to the cluster.

Cluster cluster related commands. For more redis related commands, please see the document: http://redis.readthedocs.org/en/latest/

Cluster

CLUSTER INFO prints information about the cluster

CLUSTER NODES lists all the nodes (node) currently known to the cluster, as well as relevant information about those nodes.

Node

CLUSTER MEET adds the nodes specified by ip and port to the cluster, making it part of the cluster.

CLUSTER FORGET removes the nodes specified by node_id from the cluster.

CLUSTER REPLICATE sets the current node as a slave to the node specified by node_id.

CLUSTER SAVECONFIG saves the configuration file of the node to the hard disk.

Slot (slot)

CLUSTER ADDSLOTS [slot...] Assign one or more slots (slot) to the current node (assign).

CLUSTER DELSLOTS [slot...] Removes the assignment of one or more slots to the current node.

CLUSTER FLUSHSLOTS removes all slots assigned to the current node, making the current node a node with no slots assigned.

CLUSTER SETSLOT NODE assigns the slot slot to the node specified by node_id, and if the slot is already assigned to another node, let the other node delete the slot > before assigning it.

CLUSTER SETSLOT MIGRATING migrates the slot slot of this node to the node specified by node_id.

CLUSTER SETSLOT IMPORTING imports slot slot into this node from the node specified by node_id.

CLUSTER SETSLOT STABLE cancels the import (import) or migration (migrate) of slot slot.

Key

CLUSTER KEYSLOT calculates which slot the key key should be placed on.

CLUSTER COUNTKEYSINSLOT returns the number of key-value pairs currently contained in slot slot.

CLUSTER GETKEYSINSLOT returns the keys in count slot slots.

This is the deployment of the redis cluster environment in the scenario:

At least three machines are recommended: (in fact, PING-PONG does not have a central architecture for redis.server services, and multiple master and slave can be started on the same machine)

Node1:172.31.2.31

Node2:172.31.2.32

Node3:172.31.2.33

Three groups of one master and two slave designs:

172.31.2.31GRV 7000 (Master) 172.31.2.32GRU 7000 (Slave) 172.31.2.33V7000 (Slave)

172.31.2.32 (master) 172.31.2.31 (from) 172.31.2.33 (from)

172.31.2.33 (master) 172.31.2.31 (master) 172.31.2.31 (from) 172.31.2.32

In this way, a relatively high degree of stability is achieved.

Jedis uses:

Https://github.com/xetorthio/jedis

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report