jredis cluster客戶端使用
一.序言
前面搭建了個3個msater-slave 的本地集群測試,這里用java 的客戶端 進行一些簡單測試,看看集群是否生效。
redis client 推薦:http://redis.io/clients
我使用的:https://github.com/xetorthio/jedis
二.測試
maven:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> <type>jar</type> <scope>compile</scope> </dependency>
@org.junit.Test public void cluster(){ String key = "2"; // 這東西 可以直接看到key 的分片數,就能知道放哪個 節點 System.out.println(JedisClusterCRC16.getSlot(key)); Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); // 3個master 節點 JedisCluster jc = new JedisCluster(jedisClusterNodes); System.out.println(jc.get(key)); jc.setnx(key, "bar"); String value = jc.get(key); System.out.println(value); }
別人的例子,我測試:
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001)); jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002)); JedisCluster jc = new JedisCluster(jedisClusterNodes); for (int i = 1; i <= 10000; i++) { long start = System.currentTimeMillis(); jc.set("k:" + i, "v" + i); System.out.print("set " + i +"th value in " + (System.currentTimeMillis() - start) + " ms"); start = System.currentTimeMillis(); jc.get("k:" + i); System.out.println(", get " + i +"th value in " + (System.currentTimeMillis() - start) + " ms"); }
由于是本機 固態硬盤,速度 杠桿的。
三.測試反饋:
1.集群正常,基本操作都OK
2.如果某個master 掛了,比如7001, 集群依然可用,會存放到對應的salve 7004 上去。
3.如果master -slave 都掛了,會導致 整個集群不可用,異常,因此最好配有M-S 的結構
4.我默認配置,有rdb 和 aof 持久化,因此master 掛了,重啟,數據可以從salve 上恢復
5.存放的key 會根據返回的位置,放在不同的slot 上,實現均衡
小結:
1.這里僅僅是最基本的配置,和簡單測試
2.如果配置文件以及更多命令得參考文檔
2.1 客戶端命令:http://redis.io/topics/clients
2.2 中文jredis API:http://www.360doc.com/content/15/0328/12/5054188_458684113.shtml 建議看源碼用例
2.3 redis.conf 中文(舊):http://www.cppblog.com/HappySky2046/archive/2014/06/18/207323.html
3.如果前期負載不很大,可以開啟持久化,畢竟集群 還不熟悉,如果負載高了,而且集群比較熟悉了,業務也不依賴的情況下,可以關閉,或者 適當調整,調整多看看 原理 和 配置文件說明 就行了。
來自:http://greemranqq.iteye.com/blog/2230137