sentinel-redis Java开发

来源:互联网 发布:蓝鸟中文编程破解版 编辑:程序博客网 时间:2024/06/08 08:05

我们学习如何在开发中操作Redis的Sentinel,进行对缓存的操作。假设已经安装完成了Redis服务,并成功运行。

环境:

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

Master 端口:7000

Slave1 端口:7001

Slave2 端口:7002

三个sentinel

分别监控 Master

sentinel   27000

sentinel1 27001

sentinel2 27002

 

开发

1.加入依赖jar

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.1</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.6.2.RELEASE</version></dependency>

2.spring 配置sentinel-redis,spring-redis.xml:

<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">        <property name="master">            <bean class="org.springframework.data.redis.connection.RedisNode">                <property name="name" value="mymaster"></property>            </bean>        </property>        <property name="sentinels">            <set>                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="127.0.0.1"></constructor-arg>                 <constructor-arg name="port" value="27000"></constructor-arg>                                   </bean>                                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="127.0.0.1"/>                    <constructor-arg name="port" value="27001"/>                                </bean>                                <bean class="org.springframework.data.redis.connection.RedisNode">                                       <constructor-arg name="host" value="127.0.0.1"/>                    <constructor-arg name="port" value="27002"/>                                </bean>            </set>        </property>   </bean>   <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">      <constructor-arg ref="redisSentinelConfiguration"/>   </bean>   <bean id="redisTemplate"  class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jeidsConnectionFactory"/><!-- Key-Value序列化使用的是StringRedisSerializer 结果是读友好的  -->   <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="jeidsConnectionFactory" /></bean>


3.程序调用 TEST

package com.test.jedis;import org.apache.log4j.Logger;import org.junit.After;import org.junit.Assert;import org.junit.BeforeClass;import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runners.MethodSorters;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.data.redis.core.ListOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/** * RedisTemplate实例,操作字符串 * @author Devon * */@FixMethodOrder(MethodSorters.NAME_ASCENDING)public class TestJedisRedisTemplate {private Logger logger = Logger.getLogger(TestJedisRedisTemplate.class);public static ApplicationContext ctx;public static RedisTemplate redisTemplate;@BeforeClasspublic static void setBeforeClass() {ctx = new ClassPathXmlApplicationContext("conf/spring-redis.xml");redisTemplate = (StringRedisTemplate) ctx.getBean("redisTemplate");}//List操作@Testpublic void test4() {String key = "spring";ListOperations<String, String> lop = redisTemplate.opsForList();RedisSerializer<String> serializer = new StringRedisSerializer();redisTemplate.setKeySerializer(serializer);redisTemplate.setValueSerializer(serializer);lop.leftPush(key, "aaa");lop.leftPush(key, "bbb");long size = lop.size(key); // rt.boundListOps(key).size();Assert.assertEquals(2, size);}<pre class="html" name="code"> 
/**  
* 操作字符串  
*/ 
@Test public void test5() {  
ValueOperations<String, String> vop = stringRedisTemplate.opsForValue();    
String key = "string_redis_template";  
String v = "use StringRedisTemplate set k v";  vop.set(key, v);    
String value = vop.get(key);  logger.info("KEY :"+key+" VALUE:"+value);  
Assert.assertEquals(v, value); 
}@Afterpublic void ats(){ListOperations<String, String> lop = redisTemplate.opsForList();String key = "spring";
System.out.println("========"+lop.leftPop(key));
}
}

 

下面是对 redisTemplate 具体的操作视图接口类介绍:

 

Key类型操作

ValueOperations

Redis String/Value 操作

ListOperations

Redis List 操作

SetOperations

Redis Set 操作

ZSetOperations

Redis Sort Set 操作

HashOperations

Redis Hash 操作

Value约束操作

BoundValueOperations

Redis String/Value key 约束

BoundListOperations

Redis List key 约束

BoundSetOperations

Redis Set key 约束

BoundZSetOperations

Redis Sort Set key 约束

BoundHashOperations

Redis Hash key 约束

 

0 0
原创粉丝点击