介绍

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. windows注册表的基本使用——示例

    上网找好多资料发现一个问题就是太老.例如只有RegCreateKey而没有RegCreateKeyEx用法详解,自己摸索了几个小时终于基本用到的几个函数都试用了一遍. 下面代码已经通过编译测试 // ...

  2. nullptr和NULL 区别

    注:本文内容摘自网络,准确性有待验证,现阶段仅供学习参考.尊重作品作者成果,原文链接 :http://www.2cto.com/kf/201302/190008.html 1.为什要有nullptr ...

  3. Oracle中Long类型的使用与不可使用

    ORA-01754 表只能包含一个LONG类型的列alter table 表名 add 字段名 long raw错误原因:数据表中同时建立了LONG RAW类型和LONG类型.------------ ...

  4. php中include文件夹分析

    include是包含很多php文件的一种汇总:一般放在文件夹最外层. <?php header("content-type:text/html;charset=utf-8") ...

  5. JS+CSS实现选项卡功能

    [小小一记] 首先我们写一个选项卡的结构出来,包括tab和content: 首先是tab: <ul class="ttitle-box-tabs" id="tabs ...

  6. android按行读取文件内容的几个方法

    一.简单版 import java.io.FileInputStream; void readFileOnLine(){ String strFileName = "Filename.txt ...

  7. Android APK安装过程介绍

    课题路径:从Myfile中点击应用进行安装,到安装完成,过程分析 思想方法:在研究PreloadInstaller的时候我们直接从整个apk的文件结构入手,由整体到部分的分析:但现在整个PMS非常庞大 ...

  8. css3实现无缝滚动效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. seajs的spm使用

    压缩JS文件 只需要执行这个命令即可 spm build xxx.js 这时候你将得到一个压缩过的__build/xxx.js文件 合并JS文件 如果希望将JS文件中require的其他模块都合并到这 ...

  10. python重要的函数代码块

    注意:现在python3.3.5中print打印语句有了新写法: 1. python指定生成随机数 >>> import random >>> >>&g ...