介绍

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.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  2. 神经网络中的XOR问题

    XOR问题 解决办法: 网络如图 其中激活函数 ReLU,令 即可解决XOR问题.

  3. 要想重启后也生效LINUX防火墙配置

    新配置的一台服务器,安装的是CentOS6.3系统,在安装完LNMP之后,发现nginx进程存在,且php解析正常,但是用分配的独立IP去访问的时候发现无法访问. 查了下网上的资料,发现可能是Linu ...

  4. php 之 json格式

    /*JSON语法数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组 JSON 数据的书写格式是:名称/值对名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值;如"myw ...

  5. Curl 采集乱码 gzip 原因及解决方案 utf-8

    用curl获取一个经过gzip压缩后的网页时返回乱码 原因大体就是服务器返回的Content-Encoding的值和网页的编码不同,造成curl解码出问题,直接将gzip或deflate编码的文件下载 ...

  6. GCC编译器的安装

    1.GCC简介 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC是Linux平台下最常用的编译器,GCC原名为GN ...

  7. jq实现瀑布流效果

    <!doctype html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. 第一个deeplearning4jproject跑起

    deeplearning4j是基于java的深度学习库,当然,它有许多特点,但暂时还没学那么深入,所以就不做介绍了 需要学习dl4j,无从下手,就想着先看看官网的examples,于是,下载了exam ...

  9. Contest20140705 testC DP

    testC 输入文件: testC.in 输出文件testC.out 时限1000ms 问题描述: ,⋯,an. ,a2,a3,⋯,an) ,⋯,alm. ,al2,al3,⋯,alm) 现要求G=g ...

  10. [BZOJ 1559] [JSOI2009] 密码 【AC自动机DP】

    题目链接:BZOJ - 1559 题目分析 将给定的串建成AC自动机,然后在AC自动机上状压DP. 转移边就是Father -> Son 或 Now -> Fail. f[i][j][k] ...