Coder Social home page Coder Social logo

jedis-ms-sentinel's Introduction

jedis-ms-sentinel

This is a Redis Master-Slave system architecture based jedis client. It can provide master-slave redundancy、failover by redis-sentinel、sharding and so on.

1、Master-Slaves no sharding

image

Set<String> sentinels = new LinkedHashSet<String>();
sentinels.add("192.168.137.101:63791");
sentinels.add("192.168.137.101:63792");
Pool<MasterSlaveJedis> masterSlaveJedisPool = new MasterSlaveJedisSentinelPool("master-1", sentinels,jedisPoolConfig);

MasterSlaveJedis masterSlaveJedis = masterSlaveJedisPool.getResource();
//>>> masterSlaveJedis = MasterSlaveJedis {master=192.168.137.101:6379, slaves=[192.168.137.101:6380, 192.168.137.101:6381]}
System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis);

masterSlaveJedis.set("nowTime", "2015-03-16 15:34:55"); // The underlying actually call the master.set("nowTime", "2015-03-16 15:34:55");

LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));

String slaveHolder1 = "myslave1";
Jedis slave1 = masterSlaveJedis.opsForSlave(slaveHolder); // if no any slave found, opsForSlave() will return master as a slave to be use
System.out.println(">>> nowTime = " + slave1.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55

String slaveHolder2 = "myslave1";
Jedis slave2 = masterSlaveJedis.opsForSlave(slaveHolder);
System.out.println(">>> nowTime = " + slave2.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55

System.out.println(slave1.equals(slave2)); // must be true if slaveHolder1 equals slaveHolder2

masterSlaveJedisPool.returnResource(masterSlaveJedis);

2、Master-Slaves with sharding

image

master-1 : master=192.168.137.101:6379 slaves=[192.168.137.101:6380, 192.168.137.101:6381]
master-2 : master=192.168.137.101:6382 slaves=[192.168.137.101:6383, 192.168.137.101:6384]

Set<String> masterNames = new LinkedHashSet<String>();
masterNames.add("master-1");
masterNames.add("master-2");
Set<String> sentinels = new LinkedHashSet<String>();
sentinels.add("192.168.137.101:63791");
sentinels.add("192.168.137.101:63792");
Pool<ShardedMasterSlaveJedis> shardedMasterSlaveJedisPool = new ShardedMasterSlaveJedisSentinelPool(masterNames, sentinels,jedisPoolConfig);

ShardedMasterSlaveJedis shardedMasterSlaveJedis = shardedMasterSlaveJedisPool.getResource();
for(int i = 0; i < 10; i++){
	String key = "shard-" + i;
	shardedMasterSlaveJedis.set(key, String.valueOf(i));
	// sharded in master-1[192.168.137.101:6379] for keys : shard-0, shard-2, shard-6, shard-8, shard-9, and sharded in master-2[192.168.137.101:6382] for keys : shard-1, shard-3, shard-4, shard-5, shard-7
	System.out.println(key + " = " + shardedMasterSlaveJedis.get(key));
	
	LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));
	System.out.println(key + " = " + shardedMasterSlaveJedis.getShard(key).opsForSlave().get(key)); // Get from one master group's one slave
}

shardedMasterSlaveJedisPool.returnResource(shardedMasterSlaveJedis);

jedis-ms-sentinel's People

Contributors

penggle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.