介绍

Spring-Data-Redis项目(简称SDR) 是对Redis的Key-Value数据存储操作提供了更高层次的抽象,提供了一个对几种主要的redis的Java客户端(例 如:jedis,jredis,jdbc-redis等)的抽象,使开发中可以几乎完全屏蔽具体使用客户端的影响,使业务代码保持较强的稳定性。

Spring-Data-Redis提供了一个基础的泛型RedisTemplate供开发者快速的利用代码完成基础的crud工作。而StringRedisTemplate则提供了最常用的String类型的实现。在实践中可以考虑完全省去dao层的设计,直接在service层注入相应的template实例。

Redis Sentinel是Redis官 方提供的集群管理工具,使用一个或多个sentinel和Redis的master/slave可以组成一个群集,可以检测master实例是否存活,并 在master实例发生故障时,将slave提升为master,并在老的master重新加入到sentinel的群集之后,会被重新配置,作为新 master的slave。这意味着基于Redis sentinel的HA群集是能够自我管理的。

环境

本文基于redis-2.8.19和jedis2.4.2版本。

在一台机器上启动3个redis,一个做master,两个做slave。

Master 端口:6380

Slave1 端口:6381

Slave2端口:6382

Sentinel配置

Master

redis.conf

port 6380

sentinel.conf

port 26380

sentinel monitor mymaster 192.168.0.100 6380 2

Slave1

redis.conf

port 6381

slaveof 192.168.0.100 6380

sentinel.conf

port 26381

sentinel monitor mymaster 192.168.0.100 6380 2

Slave2

redis.conf

port 6382

slaveof 192.168.0.100 6380

sentinel.conf

port 26382

sentinel monitor mymaster 192.168.0.100 6380 2

Spring配置

<beanid="redisSentinelConfiguration"

class="org.springframework.data.redis.connection.RedisSentinelConfiguration">

<propertyname="master">

<beanclass="org.springframework.data.redis.connection.RedisNode">

<propertyname="name"value="mymaster"></property>

</bean>

</property>

<propertyname="sentinels">

<set>

<beanclass="org.springframework.data.redis.connection.RedisNode">

<constructor-argname="host"value="192.168.0.100"></constructor-arg>

<constructor-argname="port"value="26380"></constructor-arg>

</bean>

<beanclass="org.springframework.data.redis.connection.RedisNode">

<constructor-argname="host"value="192.168.0.100"/>

<constructor-argname="port"value="26381"/>

</bean>

<beanclass="org.springframework.data.redis.connection.RedisNode">

<constructor-argname="host"value="192.168.0.100"/>

<constructor-argname="port"value="26382"/>

</bean>

</set>

</property>

</bean>

<beanid="jeidsConnectionFactory"

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<constructor-argref="redisSentinelConfiguration"/>

</bean>

<beanid="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate"

p:connection-factory-ref="jeidsConnectionFactory"/>

程序

写数据

public void set(final byte[] key, finalbyte[] value,final longliveTime) {

redisTemplate.execute(new RedisCallback() {

public LongdoInRedis(RedisConnectionconnection)

throws DataAccessException {

connection.set(key,value);

if (liveTime > 0) {

connection.expire(key,liveTime);

}

return 1L;

}

});

}

读数据

public byte[] get(final byte[] key) {

return redisTemplate.execute(new RedisCallback() {

public byte[]doInRedis(RedisConnection connection)

throws DataAccessException {

returnconnection.get(key);

}

});

}

删数据

public long del(final String...keys) {

return redisTemplate.execute(new RedisCallback() {

public LongdoInRedis(RedisConnectionconnection)

throws DataAccessException {

longresult = 0;

for (inti = 0; i < keys.length; i++) {

result = connection.del(keys[i].getBytes());

}

returnresult;

}

});

}

注意

1)             在配置Redis的sentinel.conf文件时注意使用外部可以访问的ip地址,因为当redis-sentinel服务和redis-server在同一台机器的时候,主服务发生变化时配置文件中将主服务ip变为127.0.0.1,这样外部就无法访问了。

2)             发生master迁移后,如果遇到运维需要,想重启所有redis,必须最先重启“新的”master节点,否则sentinel会一直找不到master。

使用Spring-data-redis操作Redis的Sentinel的更多相关文章

  1. 使用Spring Data Redis操作Redis(集群版)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  2. spring data jpa 操作pipelinedb 的continuous view 与stream

    一. 由于pipelinedb是postgreSQL的扩展,因此相关依赖于配置都合集成postgreSQL是一样的. springboot + spring data jpa + postgreSQL ...

  3. Spring Boot使用Spring Data Redis操作Redis(单机/集群)

    说明:Spring Boot简化了Spring Data Redis的引入,只要引入spring-boot-starter-data-redis之后会自动下载相应的Spring Data Redis和 ...

  4. 使用Spring Data Redis操作Redis(单机版)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  5. Spring 使用RedisTemplate操作Redis

    首先添加依赖: <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> < ...

  6. Spring Data Solr操作solr的简单案例

    Spring Data Solr简介 虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便 ...

  7. 通过Spring Data Neo4J操作您的图形数据库

    在前面的一篇文章<图形数据库Neo4J简介>中,我们已经对其内部所使用的各种机制进行了简单地介绍.而在我们尝试对Neo4J进行大版本升级时,我发现网络上并没有任何成型的样例代码以及简介,而 ...

  8. Spring Boot (五)Spring Data JPA 操作 MySQL 8

    一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...

  9. [Reprinted] 使用Spring Data Redis操作Redis(一) 很全面

    Original Address: http://blog.csdn.net/albertfly/article/details/51494080

  10. 使用WeihanLi.Redis操作Redis

    WeihanLi.Redis Intro StackExchange.Redis 扩展,更简单的泛型操作,并提供一些的适用于业务场景中的扩展 基于 Redis 的五种数据类型扩展出了一些应用: Str ...

随机推荐

  1. Java小例子(学习整理)-----学生管理系统-控制台版

    1.功能介绍: 首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试! 功能: 增加学生信息 删除学生信息 修改学生信息 查询学生信息:  按照学号(精确查询)  按照姓名(模糊查询) 打 ...

  2. Bresenham画直线,任意斜率

    function DrawLineBresenham(x1,y1,x2,y2) %sort by x,sure x1<x2. if x1>x2 tmp=x1; x1=x2; x2=tmp; ...

  3. C++ 静态、动态链接库的简单实现

    一.什么是静态链接库,什么是动态链接库? 1.静态链接库就是你使用的.lib文件,库中的代码最后需要连接到你的可执行文件中去,所以静态连接的可执行文件一般比较大一些.在静态库情况下,函数和数据被编译进 ...

  4. 对装饰模式(Decorator)的解读

    看过好多对装饰模式的讲解,他们几乎都有一句相同的话:对现有类功能的扩展.不知道大家怎么理解这句话的,之前我把”对功能的扩展“理解成”加功能=加方法“,比如Person类本来有两个功能:Eat 和 Ru ...

  5. ava下static关键字用法详解

    Java下static关键字用法详解 本文章介绍了java下static关键字的用法,大部分内容摘自原作者,在此学习并分享给大家. Static关键字可以修饰什么? 从以下测试可以看出, static ...

  6. 基于Lua的清除类游戏算法

    最近在开发游戏,用Lua语言.习惯了其它的语言,然后对Lua的一些语法很不习惯. 比如table的元素个数的取值,比switch语句等等. 不过没有办法,还是要运用Lua来写游戏的.看来学C++还真的 ...

  7. Hadoop, Python, and NoSQL lead the pack for big data jobs

    Hadoop, Python, and NoSQL lead the pack for big data jobs   Rise in cloud-based analytics could incr ...

  8. ProfessionalKnowledgeArchitecture

  9. coder

    #include <iostream>#include <GL/glut.h>using std::cout;using std::endl;float windowWidth ...

  10. RegularExpressionValidator 常用

    RegularExpressionValidator 控件用于验证输入值是否匹配正则表达式指定的模式 属性: ControlToValidate="要验证的控件名称" Valida ...