实现一致性Spring集成Redis实例入门

首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责

    

ShardedJedis实现析分

    ShardedJedis是基于一致性哈希法算实现的分布式Redis集群客户端;ShardedJedis的计划分为以下几块:

    

  1. 对象池计划:Pool,ShardedJedisPool,ShardedJedisFactory
  2. 面向用户的作操封装:BinaryShardedJedis,BinaryShardedJedis
  3. 一致性哈希实现:Sharded

    关于ShardedJedis计划,疏忽了Jedis的计划细节,计划类图如下:

    

    代码实现:

    model对象:

package com.duobei.memcached.model;

import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private String name;
	private String pass;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

}

    试测类:

    每日一道理
青春是用意志的血滴和拼搏的汗水酿成的琼浆——历久弥香;青春是用不凋的希望和不灭的向往编织的彩虹——绚丽辉煌;青春是用永恒的执著和顽强的韧劲筑起的一道铜墙铁壁——固若金汤。
package com.duobei.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.duobei.memcached.model.User;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisTest {

	private ApplicationContext context;
	private ShardedJedisPool shardedJedisPool;
	private ShardedJedis jedis;

	public RedisTest() {

	}

	@Before
	public void init() throws Exception {

		String config[] = { "applicationContext.xml",
				"applicationContext-redis.xml" };
		context = new ClassPathXmlApplicationContext(config);

		shardedJedisPool = (ShardedJedisPool) context
				.getBean("shardedJedisPool");
		jedis = (ShardedJedis) shardedJedisPool.getResource();
	}

	@Test
	@Ignore
	public void testSet() {
		System.out.println(jedis.set("name", "changxiaoxiao"));
	}

	@Test
	@Ignore
	public void testGet() {
		System.out.println(jedis.get("name"));
	}

	@Test
	@Ignore
	public void addOne() {

		/*
		 * 结构一个User对象
		 */
		User user = new User();
		user.setName("changxiaoxiao");
		user.setPass("changxiaoxiao");

		jedis.set("userOne".getBytes(), ObjectToByte(user));
		System.out.println("添加功成!");
	}

	
	@Test
	//@Ignore
	public void show() {
		byte[] bytes = jedis.get("userOne".getBytes());
		User user = (User) ByteToObject(bytes);
		System.out.println("Name:" + user.getName());
		System.out.println("Password:" + user.getPass());
	}

	/**
	 * 对象序列化
	 * @param obj
	 * @return
	 */
	public byte[] ObjectToByte(Object obj) {
		byte[] bytes = null;
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
			bo.close();
			oo.close();  
		}
		catch(Exception e) { 
			e.printStackTrace();
		}
		return bytes;
    }
	
	/**
	 * 反序列化
	 * @param bytes
	 * @return
	 */
	public Object ByteToObject(byte[] bytes) {
		Object object = null;
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			object = ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
}

文章结束给大家分享下程序员的一些笑话语录: 刹车失灵
有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”

posted @ 2013-04-24 20:59  xinyuyuanm  阅读(518)  评论(0编辑  收藏  举报