Environment to prepare
- Six Redis servers are required. Build a pseudo cluster.
- Six instances of Redis are required.
- Need to run on different ports 6379-6384
Ruby language runtime environment
We need to use Ruby scripts for cluster building
-
Ruby packaging system RubyGems
- RubyGems is a Ruby packaging system for packaging Ruby components
-
Gem is a Ruby driver for Redis
-
Redis-trib.rb tool for creating Redis clusters
Redis installation
Install 6 Redis
Set up six pseudo clusters in Windows
In the root directory Redis/of each Redis installation folder, create the startup script start.bat file. The content is as follows: Notice The port number matches the Redis configured port number
title redis-6379
redis-server.exe redis.windows.conf
Copy the code
Redis configuration
- Modify redis.windows.conf with port numbers corresponding to:
6379, 6380, 6381, 6382, 6383, 6384.
- Open cluster – enabled:
cluster-enabled yes
- Specify cluster profile:
cluster-config-file nodes-6379.conf
, cluster-config-file nodes-6379.conf is the configuration information of the node. In this command, nodes-port. conf is used. The file is generated in the directory after the service starts. - Specify timeout:
cluster-node-timeout 15000
- Open persistent:
appendonly yes
Install Ruby
Rubyinstaller-2.6.3-1-x64.exe can be installed in a simple way
Installing the Ruby Driver
- Unpack the Ruby driver (Rubygems-2.7.7), go to the root directory, and execute
ruby setup.rb
Copy the code
- Go to the Redis directory and run gem install Redis
Execute the cluster build script
- Start 6 Redis
- Copy redis-trib.rb to redis directory (6379 redis directory)
- Execute the build script at 6379 root:
Redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384Copy the code
- Can I set the above configuration? (type ‘yes’ to accept): Please confirm and enter yes. The results are as follows:
Test Cluster command
- Start client: redis-cli -c -h 127.0.0.1 -p 6379, can talk to any node port
- View the entire cluster: cluster info
- Check the current Redis: Info Replication
- View the slot number: Cluster Nodes
Cluster code testing
Note: Import the jar packages required by Jedis before testing the code
@Test
public void testCluster(a) throws IOException, InterruptedException {
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("127.0.0.1".6379));
nodes.add(new HostAndPort("127.0.0.1".6380));
nodes.add(new HostAndPort("127.0.0.1".6381));
nodes.add(new HostAndPort("127.0.0.1".6382));
nodes.add(new HostAndPort("127.0.0.1".6383));
nodes.add(new HostAndPort("127.0.0.1".6384));
JedisCluster cluster = new JedisCluster(nodes);
try {
String res = cluster.get("name");
System.out.println(res);
//cluster.quit();
} catch (Exception e) {
e.printStackTrace();
//cluster.quit();}}Copy the code