SpringBoot集成redisson(单机,集群,哨兵)

1.springBoot集成redisson(单机,集群,哨兵)

redisson版本使用3.8.2

<dependency>
??????<groupId>org.redisson</groupId>
??????<artifactId>redisson</artifactId>
??????<version>3.8.2</version>
</dependency>

2.配置文件

application.properties

spring.redis.database=0
spring.redis.password=
spring.redis.timeout=3000
#sentinel/cluster/single
spring.redis.mode=single
#连接池配置
spring.redis.pool.max-idle=16
spring.redis.pool.min-idle=8
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=3000
spring.redis.pool.conn-timeout=3000
spring.redis.pool.so-timeout=3000
spring.redis.pool.size=10
#单机配置
spring.redis.single.address=192.168.60.23:6379
#集群配置
spring.redis.cluster.scan-interval=1000
spring.redis.cluster.nodes=
spring.redis.cluster.read-mode=SLAVE
spring.redis.cluster.retry-attempts=3
spring.redis.cluster.failed-attempts=3
spring.redis.cluster.slave-connection-pool-size=64
spring.redis.cluster.master-connection-pool-size=64
spring.redis.cluster.retry-interval=1500
#哨兵配置
spring.redis.sentinel.master=business-master
spring.redis.sentinel.nodes=
spring.redis.sentinel.master-onlyWrite=true
spring.redis.sentinel.fail-max=3
3.配置文件读取

RedisProperties

import?lombok.Data;
import?lombok.ToString;
import?org.springframework.boot.context.properties.ConfigurationProperties;

/*
?
[email protected]?Abbotbr/>?*[email protected]
?*[email protected]?2018/10/18?10:42
?**/

@ConfigurationProperties(prefix?=?"spring.redis",?ignoreUnknownFields?=?false)br/>@Data
@ToString
public?class?RedisProperties?{

????private?int?database;

????/*
?????
?等待节点回复命令的时间。该时间从命令发送成功时开始计时
?????*/
????private?int?timeout;

????private?String?password;

????private?String?mode;

????/*
?????
?池配置
?????*/
????private?RedisPoolProperties?pool;

????/*
?????
?单机信息配置
?????*/
????private?RedisSingleProperties?single;

????/*
?????
?集群?信息配置
?????*/
????private?RedisClusterProperties?cluster;

????/*
?????
?哨兵配置
?????*/
????private?RedisSentinelProperties?sentinel;
}
池配置RedisPoolProperties

import?lombok.Data;
import?lombok.ToString;

/*
?
[email protected]?Abbot
br/>?*[email protected]?redis?池配置
?*[email protected]?2018/10/18?10:43
br/>?**/
@Data
@ToString
public?class?RedisPoolProperties?{

????private?int?maxIdle;

????private?int?minIdle;

????private?int?maxActive;

????private?int?maxWait;

????private?int?connTimeout;

????private?int?soTimeout;

????/*
?????
?池大小
?????*/
????private??int?size;

}
RedisSingleProperties

@Databr/>@ToString
public?class?RedisSingleProperties?{
????private??String?address;
}
集群配置RedisClusterProperties

@Databr/>@ToString
public?class?RedisClusterProperties?{

????/*
?????
?集群状态扫描间隔时间,单位是毫秒
?????*/
????private?int?scanInterval;

????/*
?????
?集群节点
?????*/
????private?String?nodes;

????/*
?????
?默认值:?SLAVE(只在从服务节点里读取)设置读取操作选择节点的模式。?可用值为:?SLAVE?-?只在从服务节点里读取。
??????MASTER?-?只在主服务节点里读取。?MASTER_SLAVE?-?在主从服务节点里都可以读取
?????
/
????private?String?readMode;
????/
??????(从节点连接池大小)?默认值:64
?????
/
????private?int?slaveConnectionPoolSize;
????/

??????主节点连接池大小)默认值:64
?????
/
????private?int?masterConnectionPoolSize;

????/*
?????
?(命令失败重试次数)?默认值:3
?????*/
????private?int?retryAttempts;

????/*
?????
命令重试发送时间间隔,单位:毫秒?默认值:1500
?????*/
????private?int?retryInterval;

????/*
?????
?执行失败最大次数默认值:3
?????*/
????private?int?failedAttempts;
}
哨兵配置 RedisSentinelProperties

@Databr/>@ToString
public?class?RedisSentinelProperties?{

????/*
?????
?哨兵master?名称
?????*/
????private?String?master;

????/*
?????
?哨兵节点
?????*/
????private?String?nodes;

????/*
?????
?哨兵配置
?????*/
????private?boolean?masterOnlyWrite;

????/*
?????

?????*/
????private?int?failMax;
}
4.CacheConfiguration

@Configurationbr/>@EnableConfigurationProperties(RedisProperties.class)
public?class?CacheConfiguration?{

[email protected]
????RedisProperties?redisProperties;

[email protected]br/>[email protected]({Redisson.class})
[email protected]("‘${spring.redis.mode}‘==‘single‘?or?‘${spring.redis.mode}‘==‘cluster‘?or?‘${spring.redis.mode}‘==‘sentinel‘")
????protected?class?RedissonSingleClientConfiguration?{

????????/*
?????????
?单机模式?redisson?客户端
?????????*/

[email protected]br/>[email protected](name?=?"spring.redis.mode",?havingValue?=?"single")
????????RedissonClient?redissonSingle()?{
????????????Config?config?=?new?Config();
????????????String?node?=?redisProperties.getSingle().getAddress();
????????????node?=?node.startsWith("redis://")???node?:?"redis://"?+?node;
????????????SingleServerConfig?serverConfig?=?config.useSingleServer()
????????????????????.setAddress(node)
????????????????????.setTimeout(redisProperties.getPool().getConnTimeout())
????????????????????.setConnectionPoolSize(redisProperties.getPool().getSize())
????????????????????.setConnectionMinimumIdleSize(redisProperties.getPool().getMinIdle());
????????????if?(StringUtils.isNotBlank(redisProperties.getPassword()))?{
????????????????serverConfig.setPassword(redisProperties.getPassword());
????????????}
????????????return?Redisson.create(config);
????????}

????????/*
?????????
?集群模式的?redisson?客户端
br/>?????????*
?????????*[email protected]
?????????*/
br/>[email protected]
[email protected](name?=?"spring.redis.mode",?havingValue?=?"cluster")
????????RedissonClient?redissonCluster()?{
????????????System.out.println("cluster?redisProperties:"?+?redisProperties.getCluster());

????????????Config?config?=?new?Config();
????????????String[]?nodes?=?redisProperties.getCluster().getNodes().split(",");
????????????List<String>?newNodes?=?new?ArrayList(nodes.length);
????????????Arrays.stream(nodes).forEach((index)?->?newNodes.add(
????????????????????index.startsWith("redis://")???index?:?"redis://"?+?index));

????????????ClusterServersConfig?serverConfig?=?config.useClusterServers()
????????????????????.addNodeAddress(newNodes.toArray(new?String[0]))
????????????????????.setScanInterval(
????????????????????????????redisProperties.getCluster().getScanInterval())
????????????????????.setIdleConnectionTimeout(
????????????????????????????redisProperties.getPool().getSoTimeout())
????????????????????.setConnectTimeout(
????????????????????????????redisProperties.getPool().getConnTimeout())
????????????????????.setFailedAttempts(
????????????????????????????redisProperties.getCluster().getFailedAttempts())
????????????????????.setRetryAttempts(
????????????????????????????redisProperties.getCluster().getRetryAttempts())
????????????????????.setRetryInterval(
????????????????????????????redisProperties.getCluster().getRetryInterval())
????????????????????.setMasterConnectionPoolSize(redisProperties.getCluster()
????????????????????????????.getMasterConnectionPoolSize())
????????????????????.setSlaveConnectionPoolSize(redisProperties.getCluster()
????????????????????????????.getSlaveConnectionPoolSize())
????????????????????.setTimeout(redisProperties.getTimeout());
????????????if?(StringUtils.isNotBlank(redisProperties.getPassword()))?{
????????????????serverConfig.setPassword(redisProperties.getPassword());
????????????}
????????????return?Redisson.create(config);
????????}

???????/*??
?????????
?哨兵模式?redisson?客户端
br/>?????????*[email protected]
?????????*/

[email protected]br/>[email protected](name?=?"spring.redis.mode",?havingValue?=?"sentinel")
????????RedissonClient?redissonSentinel()?{
????????????System.out.println("sentinel?redisProperties:"?+?redisProperties.getSentinel());
????????????Config?config?=?new?Config();
????????????String[]?nodes?=?redisProperties.getSentinel().getNodes().split(",");
????????????List<String>?newNodes?=?new?ArrayList(nodes.length);
????????????Arrays.stream(nodes).forEach((index)?->?newNodes.add(
????????????????????index.startsWith("redis://")???index?:?"redis://"?+?index));

????????????SentinelServersConfig?serverConfig?=?config.useSentinelServers()
????????????????????.addSentinelAddress(newNodes.toArray(new?String[0]))
????????????????????.setMasterName(redisProperties.getSentinel().getMaster())
????????????????????.setReadMode(ReadMode.SLAVE)
????????????????????.setFailedAttempts(redisProperties.getSentinel().getFailMax())
????????????????????.setTimeout(redisProperties.getTimeout())
????????????????????.setMasterConnectionPoolSize(redisProperties.getPool().getSize())
????????????????????.setSlaveConnectionPoolSize(redisProperties.getPool().getSize());

????????????if?(StringUtils.isNotBlank(redisProperties.getPassword()))?{
????????????????serverConfig.setPassword(redisProperties.getPassword());
????????????}

????????????return?Redisson.create(config);
????????}
????}
}

5.使用时候直接注入RedissClient客户端就可以使用
如下这样写的目的是为了统一给其它服务提供接口

[email protected]
????RedissonClient?redisson;

[email protected](value?=?"lock",?method?=?RequestMethod.POST,?consumes?=?"application/json;charset=UTF-8",?produces?=?"application/json;charset=UTF-8")br/>[email protected]
????ServerResponse<RLock>?lock(@RequestBody?ServerRequest<LockReqBody>?req)?{
????????return?callR(redisson?->?{
????????????RLock?lock?=?redisson.getLock(req.getReqBody().getLockKey());
????????????lock.lock(req.getReqBody().getTimeout(),?req.getReqBody().getUnit());
????????????return?lock;
????????});
????}
????//省略部分代码
????private?<R>?ServerResponse?callR(Function<RedissonClient,?R>?function)?{
????????ServerResponse?dv?=?RespHelper.serverResponse(RespCodeService.ERROR,?"");
????????try?{
????????????long?startTime?=?System.currentTimeMillis();
????????????dv?=?RespHelper.serverResponse(RespCodeService.SUCCESS,?function.apply(redisson));
????????????logger.info("CALLR?METHOD?USE?TIME:{}",?System.currentTimeMillis()?-?startTime);
????????}?catch?(Throwable?e)?{??????????
????????????System.out.println("callR?error");
????????}
????????return?dv;
????}
欢迎工作一到五年的Java工程师朋友们加入Java架构开发: 855835163
群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

SpringBoot集成redisson(单机,集群,哨兵)

原文地址:http://blog.51cto.com/14028890/2308518

时间: 2024-10-06 23:59:49

SpringBoot集成redisson(单机,集群,哨兵)的相关文章

阿里云构建Kafka单机集群环境

简介 在一台ECS阿里云服务器上构建Kafa单个集群环境需要如下的几个步骤: 服务器环境 JDK的安装 ZooKeeper的安装 Kafka的安装 1. 服务器环境 CPU: 1核 内存: 2048 MB (I/O优化) 1Mbps 操作系统 ubuntu14.04 64位 感觉服务器性能还是很好的,当然不是给阿里打广告,汗. 随便向kafka里面发了点数据,性能图如下所示:  2. 安装JDK 想要跑Java程序,就必须安装JDK.JDK版本,本人用的是JDK1.7. 基本操作如下: 从JDK

RabbitMQ入门教程(十四):RabbitMQ单机集群搭建

原文:RabbitMQ入门教程(十四):RabbitMQ单机集群搭建 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/78723467 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 集群简介 理解集群先理解一下元数据 队列元数据:队列的名称和声明队列时设置的属性(是否持久化.是否自动删除.队列所属的节点)

springboot redis(单机/集群)

前言 前面redis弄了那么多, 就是为了在项目中使用. 那这里, 就分别来看一下, 单机版和集群版在springboot中的使用吧.  在里面, 我会同时贴出Jedis版, 作为比较. 单机版 1. pom.xml  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.s

springboot2.x版本整合redis(单机/集群)(使用lettuce)

在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce. 此处springboot2.x,所以使用的是Lettuce.关于jedis跟lettuce的区别: Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接 Lett

springboot整合redis(集群)

一.加入maven依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <!-- ******************************* -->

ZooKeeper环境搭建(单机/集群)(转)

前提: 配置文件主要是在$ZOOKEEPER_HOME/conf/zoo.cfg,刚解压时为zoo_sample.cfg,重命名zoo.cfg即可. 配置文件常用项参考:http://www.cnblogs.com/EasonJim/p/7483880.html 环境搭建: 一.ZooKeeper的搭建方式 ZooKeeper安装方式有三种,单机模式和集群模式以及伪集群模式. 单机模式:ZooKeeper只运行在一台服务器上,适合测试环境: 伪集群模式:就是在一台物理机上运行多个ZooKeepe

一张图看懂单机/集群/热备/磁盘阵列(RAID)

单机部署(Standalone) 只有一个饮水机提供服务器,服务只部署一份 集群部署(Cluster) 多个饮水机同时提供服务,服务冗余部署,每个冗余的服务都对外提供服务,一个服务挂掉时依然可用 热备部署(Hot-swap) 只有一个桶提供服务,另一个桶stand-by,在水用完时自动热替换,服务冗余部署.只有一个主服务对外提供服务,影子服务在主服务挂掉时顶上 磁盘阵列RAID(Redundant Arrays of indepent Disks) RAID 0: 存储性能高的磁盘阵列,原理:将

Zookeeper实战之单机集群模式

前一篇文章介绍了Zookeeper的单机模式的安装及应用,但是Zookeeper是为了解决分布式应用场景的,所以通常都会运行在集群模式下.今天由于手头机器不足,所以今天打算在一台机器上部署三个Zookeeper服务来组成一个Zookeeper集群.这里解压Zookeeper的安装包到/opt目录下,这里用三个目录来代表三个Zookeeper实例,分别是/opt/zookeeper1,/opt/zookeeper2和/opt/zookeeper3. 1. 首先编辑每个Zookeeper目录下的co

【转载】一张&ldquo;神图&rdquo;看懂单机/集群/热备/磁盘阵列(RAID)

单机部署(stand-alone):只有一个饮水机提供服务,服务只部署一份 集群部署(cluster):有多个饮水机同时提供服务,服务冗余部署,每个冗余的服务都对外提供服务,一个服务挂掉时依然可用 热备部署(hot-swap):只有一个桶提供服务,另一个桶stand-by,在水用完时自动热替换,服务冗余部署,只有一个主服务对外提供服务,影子服务在主服务挂掉时顶上 磁盘阵列RAID(Redundant Arrays of independent Disks) RAID0:存储性能高的磁盘阵列,又称