標籤:ret util 遇到的問題 sso 串連池 代碼 factor set
添加配置資訊
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
只需要添加3個master節點,3個slave節點不需要添加。
你要做的也只有這些配置了,其他的spring boot都自動設定好了。
現在就可以像使用單機一樣使用叢集,redis會自動按key分區到不同的叢集執行個體。
遇到的問題
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 moreCaused 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 moreCaused 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
嘗試往redis寫資料的時候,報不能擷取串連異常,跟蹤了半天代碼,發現串連的是127.0.0.1,而不是配置的192.168.1.8,這就奇怪了,繼續跟蹤代碼發現是往redis伺服器擷取的返回的叢集執行個體列表,真是坑!
源碼:
redis.clients.jedis.Jedis#clusterSlots
@Overridepublic List<Object> clusterSlots() { checkIsInMultiOrPipeline(); client.clusterSlots(); return client.getObjectMultiBulkReply();}
就是這裡擷取返回的叢集列表,返回的就是127.0.0.1,而不是配置的192.168.1.8。
最後修改各個叢集節點的設定檔redis.conf,添加:
bind 192.168.1.8
重啟叢集節點之後,讀寫正常。
關注下面的公眾號,回複 “答案” 擷取全套Redis面試題及答案。
Spring Boot Redis Cluster 實戰乾貨