我在工作中遇到的redis集群使用
写次随笔,给自己工作学习中记录一个笔记
废话不多说,直接上代码:
功能:将相关信息存到redis中,并设置过期时间,如果redis中有,从redis获取,如果没有,从mysql中获取。redis配置了三台集群环境
1:首先,是配置文件中相关配置信息,在java代码中,相关配置直接从配置文件中读取:
#redis配置
MaxActive=10
#最大空闲连接数
MaxIdle=5
#最小空闲连接数
MinIdle=3
#最大连接数
MaxTotal=8
#jedis集群地址A
JedisA.host=192.168.0.191
portA=6300
#jedis集群地址B
JedisB.host=192.168.0.192
portB=6300
#jedis集群地址C
JedisC.host=192.168.0.193
portC=6300
#是否先进先出
Lifo=true
#在获取连接的时候检查有效性, 默认false
TestOnBorrow=false
#用returnObject方法时,是否进行有效检查
TestOnReturn=false
#获取连接时的最大等待毫秒数
MaxWaitMillis=-1
#在空闲时检查有效性
TestWhileIdle=false
#逐出扫描的时间间隔
TimeBetweenEvictionRunsMillis=-1
#每次逐出检查时 逐出的最大数目
NumTestsPerEvictionRun=3
#逐出连接的最小空闲时间,默认10分钟
TimeMillis=600000
#用户信息过期时间为1天
userPastTime=86400
#分割时间设置过期为5分钟
LoanPastTime=300
#产品过期时间
ProductPastTime=60
2:在项目启动的时候,对redis进行初始化:
//redis初始化,JedisPoolConfig为jedis的参数对象,redis在java里封装的对象时redis,参数信息从配置文件读取
//redis初始化
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt(BusinessService.getInstance().getParameter("MaxIdle")));
config.setMaxTotal(Integer.parseInt(BusinessService.getInstance().getParameter("MaxTotal")));
config.setMinIdle(Integer.parseInt(BusinessService.getInstance().getParameter("MinIdle")));
config.setLifo(Boolean.parseBoolean(BusinessService.getInstance().getParameter("Lifo")));
config.setMinEvictableIdleTimeMillis(Long.parseLong(BusinessService.getInstance().getParameter("TimeMillis")));
config.setTestOnBorrow(Boolean.parseBoolean(BusinessService.getInstance().getParameter("TestOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(BusinessService.getInstance().getParameter("TestOnReturn")));
config.setMaxWaitMillis(Long.parseLong(BusinessService.getInstance().getParameter("MaxWaitMillis")));
config.setTestWhileIdle(Boolean.parseBoolean((BusinessService.getInstance().getParameter("TestWhileIdle"))));
config.setTimeBetweenEvictionRunsMillis(Long.parseLong(BusinessService.getInstance().getParameter("TimeBetweenEvictionRunsMillis")));
config.setNumTestsPerEvictionRun(Integer.parseInt(BusinessService.getInstance().getParameter("NumTestsPerEvictionRun")));
//RedisUtils(config)为工具类,在初始化的时候调用一次,对jedis对象进行初始化
new RedisUtils(config);
3:RedisUtils类
public RedisUtils(JedisPoolConfig config)
{ synchronized(this)
{
if(jedisCluster==null)
{
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort(BusinessService.getInstance().getParameter("JedisA.host"), Integer.parseInt(BusinessService.getInstance().getParameter("portA"))));
jedisClusterNodes.add(new HostAndPort(BusinessService.getInstance().getParameter("JedisB.host"), Integer.parseInt(BusinessService.getInstance().getParameter("portB"))));
jedisClusterNodes.add(new HostAndPort(BusinessService.getInstance().getParameter("JedisC.host"), Integer.parseInt(BusinessService.getInstance().getParameter("portC"))));
jedisCluster = new JedisCluster(jedisClusterNodes,2000,2000,config);
}
} }
4:jedisCluster存值
/**
*
* @author weishaolong
* @date 2016-3-29 下午5:13:10 输入参数
* @param key key
* @param seconds 有效时间 单位秒
* @param strValue 值
*/
public static void setJedis(String key,int seconds,String strValue)
{
try
{
jedisCluster.setex(key,seconds,strValue); }
catch(Exception e)
{
log.error("Redis存值异常!By:RedisUtils.setJedis"+e.toString());
} }
5:jedisCluster取值
public static String getJedisValue(String key)
{
String strJedisVal = null;
try
{
strJedisVal=jedisCluster.get(key);
return strJedisVal;
}
catch(Exception e)
{
log.error("Redis取值异常!By:RedisUtils.getJedisValue"+e.toString());
}
return strJedisVal; }
6:这样,使用jedisCluster就可以进行使用了,java业务代码直接RedisUtils.getJedisValue("");就可以获取值了
7:其中集群还可以使用ShardJedisPool,但是ShardJedisPool我一直报
jedis.exceptions.JedisMovedDataException: MOVED 632 192.168.0.192:6301,
报这个错的原因:ShardJedisPool是分片处理的,当为集群环境的时候,是不支持路由的,所以会报数据迁移失败
初始化:
List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(3);
JedisShardInfo infoA = new JedisShardInfo(BusinessService.getInstance().getParameter("JedisA.host"),
Integer.parseInt(BusinessService.getInstance().getParameter("portA")));
JedisShardInfo infoB = new JedisShardInfo(BusinessService.getInstance().getParameter("JedisB.host"),
Integer.parseInt(BusinessService.getInstance().getParameter("portB")));
JedisShardInfo infoC = new JedisShardInfo(BusinessService.getInstance().getParameter("JedisC.host"),
Integer.parseInt(BusinessService.getInstance().getParameter("portC")));
jdsInfoList.add(infoA);
jdsInfoList.add(infoB);
jdsInfoList.add(infoC);
ShardJedisPool =new ShardedJedisPool(config, jdsInfoList);
存值:
public static void setJedis(String key,int seconds,String strValue)
{
try
{
ShardedJedis shardJedis=ShardJedisPool.getResource();
shardJedis.setex(key,seconds,strValue);
ShardJedisPool.returnResourceObject(shardJedis); }
catch(Exception e)
{
log.error("Redis存值异常!By:RedisUtils.setJedis"+e.toString());
} }
取值:
public static String getJedisValue(String key)
{
String strJedisVal = null;
try
{
ShardedJedis shardJedis=ShardJedisPool.getResource();
strJedisVal=shardJedis.get(key);
ShardJedisPool.returnResourceObject(shardJedis);
return strJedisVal;
}
catch(Exception e)
{
log.error("Redis取值异常!By:RedisUtils.getJedisValue"+e.toString());
}
return strJedisVal; }
最后,要注意一点:ShardJedisPool是一个连接池,和mysql的连接池是很类似的,每次从池子中获取值之后,要记得把这个连接返还给池子
ShardJedisPool.returnResourceObject(redis);
项目停止的时候,关闭连接池
public static void closeJedisPool()
{
if(ShardJedisPool!=null){
ShardJedisPool.destroy();
}
}
好了,主要是代码层次的讲解,原理方面没怎么说,后期进行补充....
我在工作中遇到的redis集群使用的更多相关文章
- redis 集群
http://www.linuxidc.com/Linux/2015-08/121845.htm Redis3.0版本之后支持Cluster,具体介绍redis集群我就不多说,了解请看redis中文简 ...
- Redis集群简记
Redis集群 http://doc.redisfans.com/topic/cluster-tutorial.html redis 集群是为了多个节点之间数据的共享和集群高可用的保证. redis ...
- SpringBoot系列教程之Redis集群环境配置
之前介绍的几篇redis的博文都是基于单机的redis基础上进行演示说明的,然而在实际的生产环境中,使用redis集群的可能性应该是大于单机版的redis的,那么集群的redis如何操作呢?它的配置和 ...
- redis集群搭建及一些问题
redis 1.简化版redis (本套Redis集群为简化版安装部署,只需解压至普通用户家目录下或者任意目录,解压后修改脚本,执行脚本后即可使用.) 注意,此版本需要在redis配置文件中添加 pr ...
- Redis 集群规范
什么是 Redis 集群??Redis 集群是一个分布式(distributed).容错(fault-tolerant)的 Redis 实现,集群可以使用的功能是普通单机 Redis 所能使用的功能的 ...
- redis集群安装2
概要:本文主要介绍如何在Centos7中单机搭建redis集群三主三从,按照本文绝对可以实现该需求,至于先搭建单机版主要为了方便理解redis集群,为下一步开发或生产上redis集群做铺垫.同时本 ...
- <正则吃饺子> :关于redis集群的搭建、集群测试、搭建中遇到的问题总结
项目中使用了redis ,对于其基本的使用,相对简单些,根据项目中已经提供的工具就可以实现基本的功能,但是只是这样的话,对于redis还是太肤浅,甚至刚开始时候,集群.多节点.主从是什么,他们之间是什 ...
- LINUX:关于Redis集群搭建 、和搭建项目中遇到的问题
文章来源:http://www.cnblogs.com/hello-tl/p/7804225.html 0.Redis的简单安装 1.安装redis依赖 # yum install gcc tcl g ...
- 7.redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?
作者:中华石杉 面试题 redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗? 面试官心理分析 在前几年, ...
随机推荐
- 计算机网络OSI参考模型与tcp/ip四层模型
OSI参考模型--7层 1层物理层:主要定义物理设备标准,如网线的接口类型.光线的接口类型.各种传输介质的传输速率等.它的主要作用是传输比特流(就是由1.0转化为电流强弱来进行传输,到达目的地后在转化 ...
- 通通玩blend美工(1)——荧光Button
原文:通通玩blend美工(1)--荧光Button 最近老大出差去了,光做项目也有点烦,写点教程消遣消遣(注:此乃初级教程,所以第一个消遣是本人消遣,第二个是指供各位看官消遣...) 看着各位大虾出 ...
- 同时使用SpringJUnit4ClassRunner和Parameterized进行参数化
标题实际上是个不可能完成的任务,因为我们只能用一个Runwith注解,而且只能写一个类,但是我们可以曲线救国,插入下方的5到14行就可以注入了 @ContextConfiguration(classe ...
- Github上的watch、star和fork分别是什么意思
Github上的watch.star和fork分别是什么意思呢? 1.watch可以用来设置接收邮件提醒 2.如果想持续关注该项目就star一下 3.如果想将项目拷贝一份到自己的账号下就fork fo ...
- ArcGIS for Desktop入门教程_第一章_引言 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第一章_引言 - ArcGIS知乎-新一代ArcGIS问答社区 1 引言 1.1 读者定位 我们假设用户在阅读本指南前应已具备以下知识: · 熟悉W ...
- C# Excel导入Access
/// <summary> /// 导入 /// </summary> private void btn_In_Click(object sender, EventArgs e ...
- UWP项目生成错误: 未能使用“CompileXaml”任务的输入参数初始化该任务。“CompileXaml”任务不支持“PlatformXmlDir”参数。请确认该参数存在于此任务中,并且是可设置的公共实例属性。
UWP项目生成错误: 未能使用“CompileXaml”任务的输入参数初始化该任务.“CompileXaml”任务不支持“PlatformXmlDir”参数.请确认该参数存在于此任务中,并且是可设置的 ...
- 使用内核对象Mutex可以防止同一个进程运行两次
用互斥法实现防止程序重复运行,使用内核对象Mutex可以防止同一个进程运行两次.注意:是名称相同的进程,而不是exe,因为exe程序可以改名. using System.Threading; publ ...
- LigerUI中Grid的使用时关于url请求不到数据的问题
前台代码:(这里贴的是js的代码,完整的代码可以在LigerUI的文档中找到), 这里使用的是url请求数据,问题不是处在前台,所以就不细说. $("#maingrid").lig ...
- 如何开发Office平台上的扩展应用(又称为Office 2013 App,Office 2013 Add-Ins,Apps for Office,Office 应用)
Office 扩展应用(Office Apps,Office 2013 AddIns,Apps for Office)开发 —— 中文文档 继 VBA 和 VSTO 之后, 微软为 Office 平台 ...