美文网首页Java程序员之言Java 杂谈
Spring Boot Redis Cluster 实战干货

Spring Boot Redis Cluster 实战干货

作者: java面试笔试 | 来源:发表于2018-09-14 10:28 被阅读3次

    来源:Java技术栈

    链接:https://www.jianshu.com/p/883e7d94f6eb

    添加配置信息

    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分片到不同的集群实例。

    遇到的问题

    Causedby:redis.clients.jedis.exceptions.JedisConnectionException:Couldnotgetaresourcefromthepool

    atredis.clients.util.Pool.getResource(Pool.java:53)

    atredis.clients.jedis.JedisPool.getResource(JedisPool.java:226)

    atredis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66)

    atredis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116)

    atredis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)

    atredis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)

    atredis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)

    atredis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)

    atredis.clients.jedis.JedisClusterCommand.runBinary(JedisClusterCommand.java:60)

    atredis.clients.jedis.BinaryJedisCluster.set(BinaryJedisCluster.java:77)

    atorg.springframework.data.redis.connection.jedis.JedisClusterConnection.set(JedisClusterConnection.java:618)

    ... 36more

    Causedby:redis.clients.jedis.exceptions.JedisConnectionException:java.net.ConnectException:Connectionrefused:connect

    atredis.clients.jedis.Connection.connect(Connection.java:207)

    atredis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)

    atredis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)

    atredis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)

    atorg.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)

    atorg.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)

    atorg.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)

    atredis.clients.util.Pool.getResource(Pool.java:49)

    ... 46more

    Causedby:java.net.ConnectException:Connectionrefused:connect

    atjava.net.DualStackPlainSocketImpl.waitForConnect(NativeMethod)

    atjava.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)

    atjava.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

    atjava.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

    atjava.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

    atjava.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)

    atjava.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

    atjava.net.Socket.connect(Socket.java:589)

    atredis.clients.jedis.Connection.connect(Connection.java:184)

    ... 53more

    尝试往redis写数据的时候,报不能获取连接异常,跟踪了半天代码,发现连接的是127.0.0.1,而不是配置的192.168.1.8,这就奇怪了,继续跟踪代码发现是往redis服务器获取的返回的集群实例列表,真是坑!

    源码:

    redis.clients.jedis.Jedis#clusterSlots

    @Override

    public List clusterSlots() {

    checkIsInMultiOrPipeline();

    client.clusterSlots();

    returnclient.getObjectMultiBulkReply();

    }

    就是这里获取返回的集群列表,返回的就是127.0.0.1,而不是配置的192.168.1.8。

    最后修改各个集群节点的配置文件redis.conf,添加:

    bind192.168.1.8

    重启集群节点之后,读写正常。

    (完)

    公众号:javafirst

    相关文章

      网友评论

        本文标题:Spring Boot Redis Cluster 实战干货

        本文链接:https://www.haomeiwen.com/subject/rdnngftx.html