java 集成Redis 一主多从
1、测试代码如下:
public static void main(String[] args) {
Set<String> sentinels = new HashSet<String>();
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 = new JedisPoolConfig();
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 = new JedisSentinelPool(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=/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
java 集成Redis 一主多从的更多相关文章
- springboot 集成Redis一主二从三哨兵
1.Centos7 Redis一主二从三哨兵配置 Redis一主二从三哨兵环境搭建 2.接入过程 与集成redis单机不同的是jedis相关的配置做了修改,JedisPool换成了JedisSenti ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- Spring Boot 如何快速集成 Redis 哨兵?
上一篇:Spring Boot 如何快速集成 Redis? 前面的分享栈长介绍了如何使用 Spring Boot 快速集成 Redis,上一篇是单机版,也有粉丝留言说有没有 Redis Sentine ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- Java实现Redis持久化到数据库的关键方法
import java.util.Date; import java.util.Iterator; import java.util.Set; import redis.clients.jedis ...
- spring集成redis
redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列.redis官方没有提供windows版本的软件.windows版本 ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
- linux系统下安装redis以及java调用redis
关系型数据库:MySQL Oracle 非关系型数据库:Redis 去掉主外键等关系数据库的关系性特性 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2. ...
- Spring Boot 项目实战(四)集成 Redis
一.前言 上篇介绍了接口文档工具 Swagger 及项目监控工具 JavaMelody 的集成过程,使项目更加健壮.在 JAVA Web 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...
随机推荐
- 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...
- codeforces 402 D. Upgrading Array(数论+贪心)
题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...
- CF995B Suit and Tie 贪心 第十三
Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 关于C语言\b \t \n及转义序列的理解
转义序列 说明 \b 后退一格(Backspace) \t 水平制表(Tab=4个空格) \v 垂直制表 \r 回车(Enter) \f 换页 \a 发出鸣响 \n 换行 \" 输出/输入双 ...
- CentOS7.6中mysql实践
cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 使用:yum -y install mysql mysql-server 安装 ...
- MySQL中boolean类型设置
在用MySQL设置boolean的时候发现跟本就没有这种类型,后来查资料才知道: boolean类型用tinyint表示, MYSQL保存BOOLEAN值时用1代表TRUE,0代表FALSE,bool ...
- 056 模块7-os库的基本使用
目录 一.os库基本介绍 二.os库之路径操作 2.1 路径操作 三.os库之进程管理 3.1 进程管理 四.os库之环境参数 4.1 环境参数 一.os库基本介绍 os库提供通用的.基本的操作系统交 ...
- 每天学会一点点(HashMap实现原理及源码分析)
HashMap实现原理及源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希 ...
- spring接口文档注解:@ApiOperation(转)
spring接口文档注解:@ApiOperation @ApiOperation不是spring自带的注解是swagger里的 com.wordnik.swagger.annotations.ApiO ...
- 阿里雷卷:Reactive 基金会的成立将对开发方式带来哪些影响?
作者 | 赵钰莹 近日,Linux 基金会宣布成立 Reactive 基金会.对于 Reactive,各位开发者应该并不陌生,尤其是 Node.js 开发者,但真正了解并意识到这件事情对开发方式带来的 ...