redis集群链接-java

java链接redis集群报错:

redis.clients.jedis.exceptions.JedisDataException: MOVED 13102 127.0.0.1


MOVED indicates that you're using Redis Cluster. ShardedJedis is not for Redis Cluster, so you should use JedisCluster instead. Please note that JedisCluster doesn't have pipeline mode, so you may want to send your operation one by one.

大概意思就是ShardedJedis 不能用于Redis集群,要用JedisCluster 代替ShardedJedis 才可以,而且JedisCluster 还没有pipeline 模式,所以一次只能执行一个操作


JedisCluster jedisCluster = RedisUtils.getJedisCluster(
new HostAndPort("192.168.0.140", 7000),
new HostAndPort("192.168.0.140", 7001),
new HostAndPort("192.168.0.140", 7002),
new HostAndPort("192.168.0.140", 7003),
new HostAndPort("192.168.0.140", 7003),
new HostAndPort("192.168.0.140", 7004)
);

jedisCluster.hset("hashs", "hello2", "world2");
print("判断是否存在key001:"+jedisCluster.hexists("hashs", "hello2"));
print("查找key001的值:"+jedisCluster.hget("hashs", "hello2"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您提出的问题,我可以给您提供一些参考。在Spring Boot中使用Redis集群,您需要以下步骤: 1.添加Redis依赖 您需要在Maven或Gradle中添加Redis集群的依赖,例如: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2.配置Redis 首先,您需要创建Redis集群的配置文件。例如,在application.yml中添加以下配置: spring: redis: cluster: nodes: node1:6379,node2:6379,node3:6379 其中,node1、node2和node3分别是您的Redis集群节点的IP地址和端口号。 接下来,您需要在Java配置类中创建RedisTemplate实例。例如: @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); return template; } @Bean public RedisConnectionFactory redisConnectionFactory() { RedisClusterConfiguration config = new RedisClusterConfiguration(Arrays.asList("node1:6379", "node2:6379", "node3:6379")); return new JedisConnectionFactory(config); } 您还可以设置其他的Redis配置,例如密码、连接池大小等。 3.使用Redis 最后,您可以在您的Java代码中使用RedisTemplate实例来访问Redis集群。例如,您可以使用以下代码向Redis集群中存储一个键值对: redisTemplate.opsForValue().set("myKey", "myValue"); 希望这些信息能对您有所帮助!如有其他问题,欢迎继续咨询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值