sentinel-redis Java开发

我们学习如何在开发中操作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;
	

	@BeforeClass
	public static void setBeforeClass() {
		ctx = new ClassPathXmlApplicationContext("conf/spring-redis.xml");
		redisTemplate = (StringRedisTemplate) ctx
				.getBean("redisTemplate");
	}
	
	//List操作
	@Test
	public 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); 
	}
	
	@After	public 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值