Redis整合springboot实现集群模式

整体结构

Redis.config 

package com.cc.springredis.config;import com.cc.springredis.RedisUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {/*** 实例化 RedisTemplate 对象** @return*/@Beanpublic RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();initDomainRedisTemplate(redisTemplate, redisConnectionFactory);return redisTemplate;}/*** 设置数据存入 redis 的序列化方式,并开启事务** @param redisTemplate* @param factory*/private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {//如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!  redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());// 开启事务redisTemplate.setEnableTransactionSupport(true);redisTemplate.setConnectionFactory(factory);}/*** @return RedisUtil* @throws* @Title: redisUtil*/@Bean(name = "redisUtil")public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate) {RedisUtil redisUtil = new RedisUtil();redisUtil.setRedisTemplate(redisTemplate);return redisUtil;}}

RedisUtil

package com.cc.springredis;import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;public class RedisUtil {private RedisTemplate<String, Object> redisTemplate;public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}// =============================common============================/*** 指定缓存失效时间* * @param key 键* @param time  时间(秒)* @return*/public boolean expire(String key, long time) {try {if (time > 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据key 获取过期时间* * @param key 键 不能为null* @return 时间(秒) 返回0代表为永久有效*/public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 判断key是否存在* * @param key 键* @return true 存在 false不存在*/public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除缓存* @param key 可以传一个值 或多个*/@SuppressWarnings("unchecked")public void del(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete(CollectionUtils.arrayToList(key));}}}// ============================String=============================/*** 普通缓存获取* * @param key  键* @return 值*/public Object get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}/*** 普通缓存放入* * @param key   键* @param value 值* @return true成功 false失败*/public boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 普通缓存放入并设置时间* * @param key 键* @param value  值* @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期* @return true成功 false 失败*/public boolean set(String key, Object value, long time) {try {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 递增* * @param key  键* @param by 要增加几(大于0)* @return*/public long incr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递增因子必须大于0");}return redisTemplate.opsForValue().increment(key, delta);}/*** 递减* * @param key   键* @param by 要减少几(小于0)* @return*/public long decr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递减因子必须大于0");}return redisTemplate.opsForValue().increment(key, -delta);}// ================================Map=================================/*** HashGet* * @param key 键 不能为null* @param item 项 不能为null* @return 值*/public Object hget(String key, String item) {return redisTemplate.opsForHash().get(key, item);}/*** 获取hashKey对应的所有键值* * @param key 键* @return 对应的多个键值*/public Map<Object, Object> hmget(String key) {return redisTemplate.opsForHash().entries(key);}/*** HashSet* * @param key  键* @param map  对应多个键值* @return true 成功 false 失败*/public boolean hmset(String key, Map<String, Object> map) {try {redisTemplate.opsForHash().putAll(key, map);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** HashSet 并设置时间* * @param key  键* @param map 对应多个键值* @param time  时间(秒)* @return true成功 false失败*/public boolean hmset(String key, Map<String, Object> map, long time) {try {redisTemplate.opsForHash().putAll(key, map);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建* * @param key  键* @param item 项* @param value  值* @return true 成功 false失败*/public boolean hset(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建* * @param key 键* @param item 项* @param value  值* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间* @return true 成功 false失败*/public boolean hset(String key, String item, Object value, long time) {try {redisTemplate.opsForHash().put(key, item, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除hash表中的值* * @param key 键 不能为null* @param item  项 可以使多个 不能为null*/public void hdel(String key, Object... item) {redisTemplate.opsForHash().delete(key, item);}/*** 判断hash表中是否有该项的值* * @param key  键 不能为null* @param item 项 不能为null* @return true 存在 false不存在*/public boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}/*** hash递增 如果不存在,就会创建一个 并把新增后的值返回* * @param key  键* @param item 项* @param by  要增加几(大于0)* @return*/public double hincr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, by);}/*** hash递减* * @param key 键* @param item 项* @param by 要减少记(小于0)* @return*/public double hdecr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, -by);}// ============================set=============================/*** 根据key获取Set中的所有值* * @param key 键* @return*/public Set<Object> sGet(String key) {try {return redisTemplate.opsForSet().members(key);} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据value从一个set中查询,是否存在* * @param key  键* @param value 值* @return true 存在 false不存在*/public boolean sHasKey(String key, Object value) {try {return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) {e.printStackTrace();return false;}}/*** 将数据放入set缓存* * @param key 键* @param values 值 可以是多个* @return 成功个数*/public long sSet(String key, Object... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 将set数据放入缓存* * @param key  键* @param time 时间(秒)* @param values  值 可以是多个* @return 成功个数*/public long sSetAndTime(String key, long time, Object... values) {try {Long count = redisTemplate.opsForSet().add(key, values);if (time > 0)expire(key, time);return count;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 获取set缓存的长度* * @param key 键* @return*/public long sGetSetSize(String key) {try {return redisTemplate.opsForSet().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 移除值为value的* * @param key 键* @param values 值 可以是多个* @return 移除的个数*/public long setRemove(String key, Object... values) {try {Long count = redisTemplate.opsForSet().remove(key, values);return count;} catch (Exception e) {e.printStackTrace();return 0;}}// ===============================list=================================/*** 获取list缓存的内容* * @param key  键* @param start   开始* @param end 结束 0 到 -1代表所有值* @return*/public List<Object> lGet(String key, long start, long end) {try {return redisTemplate.opsForList().range(key, start, end);} catch (Exception e) {e.printStackTrace();return null;}}/*** 获取list缓存的长度* * @param key  键* @return*/public long lGetListSize(String key) {try {return redisTemplate.opsForList().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 通过索引 获取list中的值* * @param key 键* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推* @return*/public Object lGetIndex(String key, long index) {try {return redisTemplate.opsForList().index(key, index);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将list放入缓存* * @param key 键* @param value  值* @param time 时间(秒)* @return*/public boolean lSet(String key, Object value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* * @param key  键* @param value  值* @param time   时间(秒)* @return*/public boolean lSet(String key, Object value, long time) {try {redisTemplate.opsForList().rightPush(key, value);if (time > 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* * @param key 键* @param value  值* @param time   时间(秒)* @return*/public boolean lSet(String key, List<Object> value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* * @param key  键* @param value 值* @param time  时间(秒)* @return*/public boolean lSet(String key, List<Object> value, long time) {try {redisTemplate.opsForList().rightPushAll(key, value);if (time > 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据索引修改list中的某条数据* * @param key  键* @param index 索引* @param value 值* @return*/public boolean lUpdateIndex(String key, long index, Object value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 移除N个值为value* * @param key 键* @param count  移除多少个* @param value  值* @return 移除的个数*/public long lRemove(String key, long count, Object value) {try {Long remove = redisTemplate.opsForList().remove(key, count, value);return remove;} catch (Exception e) {e.printStackTrace();return 0;}}
}

application.properties

########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
# database name
spring.redis.database=0
# server host1 单机使用,对应服务器ip
#spring.redis.host=127.0.0.1
# server password 密码,如果没有设置可不配
#spring.redis.password=
#connection port  单机使用,对应端口号
#spring.redis.port=6379
# pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1#redis集群配置
spring.redis.cluster.nodes=192.168.133.130:6380,192.168.133.130:6381,192.168.133.130:6382,192.168.133.130:6383,192.168.133.130:6384,192.168.133.130:6385,192.168.133.130:6386,192.168.133.130:6387
spring.redis.cluster.max-redirects=3

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cc</groupId><artifactId>spring-redis-cluster</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-redis-cluster</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/447267.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一个穷人是从什么时候开始有钱的?

2010年&#xff0c;文野31岁那年&#xff0c;买房后第二年&#xff0c;完成了「人生中最重要的一次转变」。 这一年&#xff0c;他在心里对自己的定位&#xff0c;从穷人变成了有钱人。 「一些人哪怕有钱了&#xff0c;心里也永远甩不脱穷的影子。」这是我曾经在《 阶段性胜…

Redis整合springboot实现消息队列

publisher消息的发出 代码整体的结构 publisherConfig package com.cc.springbootredispublisher.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.conne…

人最重要的能力是什么?

我见过很多人有能力、能成功&#xff0c;但并不幸福。人最重要的能力是什么? 人生最重要的能力是什么?我的答案是&#xff0c;随时保持内心平静的能力。 我开始想到的是“坚持”&#xff0c;后来我认为不对&#xff0c;“坚持”仅仅是成功所必备的能力&#xff0c;并不是人生…

Redis数据缓存

代码的整体结构 配置文件 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apac…

hevc/265 开源项目及相关

1.X265 个是有两个版本&#xff0c;一个是国内人搞的&#xff0c;是国外公司搞的 1.国外公司版本 只是一个编码器&#xff0c;目前没有支持解码 开发语言 c web url: www.x265.org source url: https://bitbucket.org/multicoreware/x265 x265 is an open-source projec…

IPFS星际文件系统的简介

IPFS简介 IPFS&#xff08;InterPlanetary File System&#xff09;叫星际文件传输系统&#xff0c;本质是一个基于点对点的分布式超媒体分发协议&#xff0c;它整合了分布式系统&#xff0c;为所有人提供全球统一的可寻址空间&#xff0c;因为他具有良好的安全性、较高的传输…

ARM和NEON指令 very nice

在移动平台上进行一些复杂算法的开发&#xff0c;一般需要用到指令集来进行加速。目前在移动上使用最多的是ARM芯片。 ARM是微处理器行业的一家知名企业&#xff0c;其芯片结构有&#xff1a;armv5、armv6、armv7和armv8系列。芯片类型有&#xff1a;arm7、arm9、arm11、corte…

IPFS下载安装和配置

参考链接 因为这个网站访问速度很慢&#xff0c;我提供了IPFS的MAC版本。有需要的查看我的资源下载。 大致流程 安装 $ ls go-ipfs_v0.4.10_darwin-amd64.tar.gz $ tar xvfz go-ipfs_v0.4.10_darwin-amd64.tar.gz x go-ipfs/build-log x go-ipfs/install.sh x go-ipfs/ipfs…

IPFS的相关操作命令

新增文件 在桌面新建名字为1121的文件夹&#xff0c;在文件夹里面新建file.txt文件&#xff0c;在文件里面输入数据&#xff0c;保存退出 $ pwd /Users/CHY/Desktop $ mkdir 1121 $ cd 1121/ $ vi file.txt $ cat file.txt 哈哈&#xff0c;为什么只有我不快乐 给文件输入内容…

Neon Intrinsics各函数介绍

#ifndef __ARM_NEON__ #error You must enable NEON instructions (e.g. -mfloat-abisoftfp -mfpuneon) to use arm_neon.h #endif/*(1)、正常指令&#xff1a;生成大小相同且类型通常与操作数向量相同的结果向量&#xff1b; (2)、长指令&#xff1a;对双字向量操作数执行运算…

npm安装包总是失败了的,请参考

镜像使用方法 &#xff08;三种办法任意一种都能解决问题&#xff0c;建议使用第三种&#xff0c;将配置写死&#xff0c;下次用的时候配置还在&#xff09;: 1.通过config命令 npm config set registry https://registry.npm.taobao.org npm info underscore //&#xff08…

arm 开发工具比较(ADS vs RealviewMDK vs RVDS)

ADS REALVIEW MDK RVDS 公司 ARM Keil&#xff08;后被ARM收购&#xff09; ARM 版本 最新1.2 ,被RVDS取代 最新4.0 是否免费 破解情况 有 有 工程管理 CodeWarrior IDE nVision IDE Eclipse/ CodeWarrior IDE 编译器 ARM C compiler for AD…

解决macOS Catalina(10.15)解决阻止程序运行“macOS无法验证此App不包含恶意软件”

在终端里面输入如下命令 sudo spctl --master-disable 下面图片对比执行命令前后&#xff0c;安全性与隐私 界面上显示的差异&#xff1a;使用命令之后&#xff0c;界面变了

MSYS2 + MinGW-w64 + Git + gVim 环境配置

原文 http://dantvt.is-programmer.com/posts/63161.html 以前用 MSYS 的多&#xff0c;最近重装系统顺带把环境重新配一下&#xff0c;发现 MSYS2 挺顺手的。 一、安装 MSYS2 先装 MSYS2 的好处是之后可以将 $HOME 设为 /home/name/&#xff0c;再装其他 *nix 系工具时配置…

MAC版 的最新Docker 2.2版本配置国内代理的解决办法

点击Docker图标&#xff0c;选择Preference选项&#xff0c;进行国内代理的问题 输入内容如下 {"experimental": false,"debug": true,"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.…

常用的Homebrew的命令的使用

&#xff08;1&#xff09;安装软件&#xff1a;brew install 软件名&#xff0c;例如&#xff1a;brew install wget &#xff08;2&#xff09;搜索软件&#xff1a;brew search 软件名 &#xff08;3&#xff09;卸载软件&#xff1a;brew uninstall 软件名 &#xff08;…

微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)

转自 http://www.iruanmi.com/visual-studio-2013/ 微软已经向MSDN订阅用户提供了Visual Studio 2013正式版镜像下载&#xff0c;不过非MSDN用户可以在微软的Visual Studio 2013官方网站上下载到正式版镜像&#xff08;通过下载专业版本&#xff0c;已验证与MSDN版本一致&…

《算法的乐趣》作者王晓华访谈:多看、多做、多想是秘诀

摘要&#xff1a;王晓华是一位热衷于算法研究的程序员&#xff0c;他是CSDN算法专栏的超人气博主&#xff0c;也是《算法的乐趣》一书的作者。近日&#xff0c;笔者采访了王晓华&#xff0c;请他分享算法的经验之道。 王晓华是一位热衷于算法研究的程序员&#xff0c;他是CSDN…

基于Mac环境搭建以太坊私有区块链进行挖矿模拟

第一步&#xff1a;相关软件的安装 go-ethereum客户端安装Go-ethereum客户端通常被称为Geth&#xff0c;它是个命令行界面&#xff0c;执行在Go上实现的完整以太坊节点。Geth得益于Go语言的多平台特性&#xff0c;支持在多个平台上使用(比如Windows、Linux、Mac)。Geth是以太坊…

Springboot 添加server.servlet.context-path

Springboot 2.0变革后的配置区别 1、springboot 2.0之前&#xff0c;配置为 server.context-path 2、springboot 2.0之后&#xff0c;配置为 server.servlet.context-path