1、测试代码如下:

public static voidmain(String[] args) {

Set sentinels = new HashSet();

sentinels.add("118.25.7.111:26379");

sentinels.add("118.25.7.111:26380");

sentinels.add("118.25.7.111:26381");

String clusterName= "mymaster";//此处添加密码

String password = "foo";//建立连接池配置参数

JedisPoolConfig config = newJedisPoolConfig();

config.setMaxIdle(50);//设置最小空闲数

config.setMinIdle(8);

config.setMaxWaitMillis(10000);

config.setTestOnBorrow(true);

config.setTestOnReturn(true);//Idle时进行连接扫描

config.setTestWhileIdle(true);//表示idle object evitor两次扫描之间要sleep的毫秒数

config.setTimeBetweenEvictionRunsMillis(30000);//表示idle object evitor每次扫描的最多的对象数

config.setNumTestsPerEvictionRun(10);//表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义

config.setMinEvictableIdleTimeMillis(60000);

JedisSentinelPool redisSentinelJedisPool= newJedisSentinelPool(clusterName,sentinels,config,password);

Jedis jedis= null;try{

jedis=redisSentinelJedisPool.getResource();

jedis.set("key", "aaa");

System.out.println(jedis.get("key"));

System.out.println(jedis.get("bbb"));

}catch(Exception e) {

e.printStackTrace();

}finally{//redisSentinelJedisPool.returnResource(jedis);

jedis.close();

}

redisSentinelJedisPool.close();

}

问题:

1、-DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no‘ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no‘, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘--protected-mode no‘ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

解决:此处是因为sentinel的保护模式开启(默认)导致的,在sentinel对应的配置文件中将其关闭即可,即

#关闭保护模式

protected-mode no

2、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.JedisSentinelPool.getResource(JedisSentinelPool.java:209)

at com.ww.wwta.config.client.RedisClusterClient.main(RedisClusterClient.java:92)

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:889)

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)

at redis.clients.util.Pool.getResource(Pool.java:49)

... 2 more

解决:未将对应的主服务的端口加入防火墙,将设置端口加入防火墙即可,即

firewall-cmd --permanent --zone=public --add-port=26380/tcp

firewall-cmd --reload

查看是否已加入:

firewall-cmd --permanent --zone=public --list-ports

3、连接127.0.0.1:6780连接拒绝

需将sentinel配置sentinel monitor mymaster 127.0.0.1 6380 2 -> sentinel monitor mymaster 118.25.7.111 6380 2

原文:https://www.cnblogs.com/kingsonfu/p/10140271.html

java redis多主多备_java 集成Redis 一主多从相关推荐

  1. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下: http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk673 ...

  2. Redis第三话 – Springboot集成Redis以及常用API和客户端介绍

    本文主要记录在Springboot中集成Redis的使用. 1. springboot集成redis 1.1 maven配置 基于springboot 版本2.5.6,parent包就不贴了. < ...

  3. redis 支持 json_Spring Boot 中集成 Redis

    阅读本文约需要5分钟 大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈).上次老师跟大家分享了 Spring Boot 中的拦截器,今天跟大家分享下 ...

  4. java集成redis集群_spring集成redis cluster详解

    客户端采用最新的jedis 2.7 1.maven依赖: redis.clients jedis 2.7.3 2.增加spring 配置 classpath:connect-redis.propert ...

  5. 【JAVA秘籍功法篇-SpringBoot】SpringBoot如何集成Redis?

    SpringBoot如何集成redis 大家好,我是王老狮,一个有思想有内涵的架构狮.Redis作为分布式缓存,在我们的项目中经常会用到,今天我们看下SpringBoot如何集成redis. POM中 ...

  6. Java集成Redis key过期通知

    为什么要使用过期通知呢? 捕获Redis中过期的Key,解锁新姿势.比如有个用户会员的模块,那么可以在redis添加一个用户会员的有效时Key,然后在Java项目中捕获,处理相关的逻辑. 一.开启Re ...

  7. java实现redis缓存_java实现redis缓存功能

    一.安装redis 1.mac安装,如果有安装brew 可以直接快捷安装:brew install redis 2.linux下载安装wget http://download.redis.io/rel ...

  8. java redis 数据自过期_Java架构-Redis的内存回收策略和Key过期策略,看这篇就够了...

    Redis 作为当下最热门的 Key-Value 存储系统,在大大小小的系统中都扮演着重要的角色,不管是 session 存储还是热点数据的缓存,亦或是其他场景,我们都会使用到 Redis.在生产环境 ...

  9. java redis缓存使用_java使用redis缓存数据库

    开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,maven配置如下 redis.clients jedis 2.9.0 Redis ...

最新文章

  1. java自定义监听器例子_Spring 实现自定义监听器案例
  2. Hibernate配置(2)
  3. Java使用非对称数据加密RSA
  4. python写出的程序如何给别人使用-python如何写出表白程序
  5. 冒泡排序html代码,冒泡排序.html
  6. vb fso方式访问网络上的共享文件夹_经常遇见的网络故障详细分析及解决方法!...
  7. python矩阵sin计算器_Python-Numpy科学计算器的基本软件包
  8. 1900页Python系列PPT分享一:基础知识(106页)
  9. 秋凉了,大家别加班了,早回吧:)
  10. GD2拖动验证码Thinkphp版
  11. 如何绘制景区热力图_百度地图“景区热力图” 大数据让你拥有“千里眼”
  12. 浅谈Innodb存储结构(Buffer Pool、Double Write Buffer、Change Buffer、Redo log、Undo Log、自适应索引.......)
  13. 深访杨超越杯编程大赛发起人 还原硬核粉丝追星全过程
  14. Beta阶段敏捷冲刺前准备
  15. 问卷研究的五类分析思路模板
  16. 一分钟带你了解超五类、六类网线的特点及区别
  17. 通知栏消息多种提醒方式
  18. 双硬盘安装win10+ubuntu18.04双系统,U盘安装,UEFI引导
  19. i.MX8QM环境搭建
  20. 大数据营销--中关村大数据产业联盟秘书长赵国栋访谈

热门文章

  1. Windows Phone 7 Tip (13) -- 如何搜集应用使用数据
  2. (zz)ubuntu 9.04 下无线破解
  3. 安装了低版本Jdk后eclipse无法打开的终极解决方法
  4. 解决vscode中golang插件依赖安装失败问题
  5. http-server swagger-editor启动成功后,访问404的解决方法
  6. 函数声明末尾的“ const”是什么意思? [重复]
  7. “throw”和“throw ex”之间有区别吗?
  8. 实现Map并保持插入顺序的Java类?
  9. 什么是(功能)反应式编程?
  10. spring 获取配置文件的值