Spring Boot Redis Cluster in action

Add configuration information

spring.redis:
  database: 0 # Redis数据库索引(默认为0)
  #host: 192.168.1.8
  #port: 6379
  password: 123456
  timeout: 10000 # 连接超时时间(毫秒)  
  pool: 
    max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
    max-idle: 8 # 连接池中的最大空闲连接
    max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    min-idle: 0 # 连接池中的最小空闲连接
  cluster:
    nodes:
      - 192.168.1.8:9001
      - 192.168.1.8:9002
      - 192.168.1.8:9003

Only 3 master nodes need to be added, and 3 slave nodes do not need to be added.

All you have to do is these configurations, other spring boots are automatically configured.

Now you can use the cluster like a single machine, and redis will automatically shard into different cluster instances by key.

problems encountered

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
    at redis.clients.jedis.JedisClusterCommand.runBinary(JedisClusterCommand.java:60)
    at redis.clients.jedis.BinaryJedisCluster.set(BinaryJedisCluster.java:77)
    at org.springframework.data.redis.connection.jedis.JedisClusterConnection.set(JedisClusterConnection.java:618)
    ... 36 more
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 46 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 53 more

When I tried to write data to redis, it reported that the connection could not be obtained. After tracking the code for a long time, I found that the connection was 127.0.0.1, not the configured 192.168.1.8. This is strange. I continued to track the code and found that it was obtained from the redis server. The returned list of cluster instances is really a pit!

image

Source code:

redis.clients.jedis.Jedis#clusterSlots

@Override
public List<Object> clusterSlots() {
    checkIsInMultiOrPipeline();
    client.clusterSlots();
    return client.getObjectMultiBulkReply();
}

This is where the returned cluster list is obtained, which returns 127.0.0.1 instead of the configured 192.168.1.8.

Finally, modify the configuration file redis.conf of each cluster node and add:

bind 192.168.1.8

After restarting the cluster nodes, reading and writing are normal.

Recommended reading

Face to face: The most complete Java multi-threading interview questions and answers in history

Face to face: the most complete Ali advanced Java interview questions in history

Face to face: the most comprehensive Spring interview questions in history

Tutorial: The most complete set of Spring Boot video tutorials

Books: 15 must-read books for advanced Java architects

Tools: Recommend an online creation flowchart and mind mapping software

Share Java dry goods, high concurrency programming, popular technical tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot information.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819320&siteId=291194637