RedisCluster读写分离改造

return new JedisClusterCommand<String>(connectionHandler, maxRedirections) {
@Override
public String execute(Jedis connection) {
return connection.get(key);
}
}.run(key);

private Map<String, ClusterNodeObject> getClusterNodes(Jedis jedis) {
Map<String, ClusterNodeObject> hpToNodeObjectMap = new HashMap<>();
String clusterNodesCommand = jedis.clusterNodes();
String[] allNodes = clusterNodesCommand.split("\n");
for (String allNode : allNodes) {
String[] splits = allNode.split(" ");
String hostAndPort = splits[1];
ClusterNodeObject clusterNodeObject =
new ClusterNodeObject(splits[0], splits[1], splits[2].contains("master"), splits[3],
Long.parseLong(splits[4]), Long.parseLong(splits[5]), splits[6],
splits[7].equalsIgnoreCase("connected"), splits.length == 9 ? splits[8] : null);
hpToNodeObjectMap.put(hostAndPort, clusterNodeObject);
}
return hpToNodeObjectMap;
}
Map<String, ZhenJedisPool> masterNodes = new HashMap<>();
for (ClusterNodeObject clusterNodeObject : clusterNodeObjects) {
String ipPort = clusterNodeObject.getIpPort();
String[] ipPortSplits = ipPort.split(":");
HostAndPort hostAndPort = new HostAndPort(ipPortSplits[0], Integer.parseInt(ipPortSplits[1]));
setNodeIfNotExist(hostAndPort);
if (clusterNodeObject.isMaster()) {
ZhenJedisPool zhenJedisPool = new ZhenJedisPool();
zhenJedisPool.setWritePool(nodes.get(ipPort));
masterNodes.put(clusterNodeObject.getNodeId(), zhenJedisPool); String[] slotSplits = clusterNodeObject.getSlot().split("-");
for (int i = Integer.parseInt(slotSplits[0]); i <= Integer.parseInt(slotSplits[1]); i++) {
this.slots.put(i, zhenJedisPool);
}
}
} for (ClusterNodeObject clusterNodeObject : clusterNodeObjects) {
if (!clusterNodeObject.isMaster()) {
String masterNodeId = clusterNodeObject.getMasterNodeId(); ZhenJedisPool zhenJedisPool = masterNodes.get(masterNodeId);
zhenJedisPool.getReadPools().add(nodes.get(clusterNodeObject.getIpPort()));
}
}
public JedisPool getSlotPool(int slot, ZhenQueryContext queryContext) {
r.lock();
try {
ZhenJedisPool zhenJedisPool = slots.get(slot);
if (queryContext.getOperationType() == OperationType.WRITE) {
return zhenJedisPool.getWritePool();
} else {
List<JedisPool> readPools = zhenJedisPool.getReadPools();
return readPools.get(new Random().nextInt(readPools.size()));
}
} finally {
r.unlock();
}
}
@Override
public String get(final String key) {
ZhenQueryContextHolder.getInstance().setQueryContext(new ZhenQueryContext(OperationType.READ));
return new ZhenJedisClusterCommand<String>(connectionHandler, maxRedirections) {
@Override
public String execute(Jedis connection) {
return connection.get(key);
}
}.run(key);
}

5974ed7dd81c112d9a2354a0a985995913b4702c 192.168.1.137:6389 master - 0 1470273087539 26 connected 0-5640
d08dc883ee4fcb90c4bb47992ee03e6474398324 192.168.1.137:6390 master - 0 1470273086034 25 connected 5641-11040
ffb4db4e1ced0f91ea66cd2335f7e4eadc29fd56 192.168.1.138:6390 slave 5974ed7dd81c112d9a2354a0a985995913b4702c 0 1470273087539 26 connected
c69b521a30336caf8bce078047cf9bb5f37363ee 192.168.1.137:6388 master - 0 1470273086536 28 connected 11041-16383
532e58842d001f8097fadc325bdb5541b788a360 192.168.1.138:6389 slave c69b521a30336caf8bce078047cf9bb5f37363ee 0 1470273086034 28 connected
aa52c7810e499d042e94e0aa4bc28c57a1da74e3 192.168.1.138:6388 myself,slave d08dc883ee4fcb90c4bb47992ee03e6474398324 0 0 19 connected
192.168.1.137:6390> get key1
-> Redirected to slot [9189] located at 192.168.1.138:6388
"value1"
//如果是只读连接 {
connection.readonly();
}
return execute(connection);

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.7.0</version>
</dependency>
client = CuratorFrameworkFactory.newClient("xxx",
new RetryNTimes(5, 5000));
client.start();
public void compareAndSet() throws Exception {
List<ZhenJedisPoolObject> jedisPoolFromCluster = getJedisPoolFromCluster();
String currentString = JSON.toJSONString(jedisPoolFromCluster);
if (client.checkExists().forPath(TOPO_PATH) == null) {
SysOutLogger.info("Start to create zk node: " + TOPO_PATH);
client.create().creatingParentsIfNeeded().forPath(TOPO_PATH, currentString.getBytes());
} else {
String statData = new String(client.getData().forPath(TOPO_PATH));
if (!currentString.equalsIgnoreCase(statData)) {
SysOutLogger.info("Node not synchronized with online, to reset...");
client.setData().forPath(TOPO_PATH, currentString.getBytes());
}
}
}
String content = new String(client.getData().forPath(TOPO_PATH), "UTF-8");
List<ZhenJedisPoolObject> zhenJedisPoolObjects =
JSON.parseObject(content, new TypeReference<List<ZhenJedisPoolObject>>() {
});
discoverClusterNodesAndSlots(zhenJedisPoolObjects); final NodeCache nodeCache = new NodeCache(client, TOPO_PATH, false);
nodeCache.start();
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
String content = new String(nodeCache.getCurrentData().getData(), "UTF-8");
List<ZhenJedisPoolObject> zhenJedisPoolObjects =
JSON.parseObject(content, new TypeReference<List<ZhenJedisPoolObject>>() {
});
discoverClusterNodesAndSlots(zhenJedisPoolObjects);
}
});
RedisCluster读写分离改造的更多相关文章
- spring-data-redis读写分离
在对Redis进行性能优化时,一直想对Redis进行读写分离.但由于项目底层采用spring-data-redis对redis进行操作,参考spring官网却发现spring-data-redis目前 ...
- springboot实现读写分离(基于Mybatis,mysql)
近日工作任务较轻,有空学习学习技术,遂来研究如果实现读写分离.这里用博客记录下过程,一方面可备日后查看,同时也能分享给大家(网上的资料真的大都是抄来抄去,,还不带格式的,看的真心难受). 完整代码:h ...
- Oceanbase读写分离方案探索与优化
[作者] 许金柱,携程资深DBA,专注于分布式数据库研究及运维. 台枫,携程高级DBA,主要负责MySQL和OceanBase的运维. [前言] 读写分离,是一种将数据库的查询操作和写入操作分离 ...
- ecshop改造读写分离配置与改造
前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. <?php $db_name = "ecshop"; $p ...
- ecshop改造读写分离
前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. config.php <?php $db_name = "ecsho ...
- 读写分离提高 SQL Server 并发性能
以下内容均非原创,仅作学习.分享!! 在 一些大型的网站或者应用中,单台的SQL Server 服务器可能难以支撑非常大的访问压力.很多人在这时候,第一个想到的就是一个解决性能问题的利器——负载均衡. ...
- 读写分离提高 SQL Server 并发性
转自:http://www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=476 在一些大型的网站或者应用中,单台的SQL Serv ...
- mysql高可用架构方案之二(keepalived+lvs+读写分离+负载均衡)
mysql主从复制与lvs+keepalived实现负载高可用 文件夹 1.前言 4 2.原理 4 2.1.概要介绍 4 2.2.工作原理 4 2.3.实际作用 4 3方 ...
- EF通用数据层封装类(支持读写分离,一主多从)
浅谈orm 记得四年前在学校第一次接触到 Ling to Sql,那时候瞬间发现不用手写sql语句是多么的方便,后面慢慢的接触了许多orm框架,像 EF,Dapper,Hibernate,Servic ...
随机推荐
- L226
The dean can’t see you at the moment. He is addressing the first-year students in the lecture hall.系 ...
- UIView.animateWithDuration 没有动画过渡效果直接跳到最后
当我在viewDidLoad()方法内添加以下代码的时候发现程序运行时不会有动画过度效果,而是直接跳到了最后. let x = (self.view.bounds.size.width - 268) ...
- Object-C 基础笔记4---ARC内存管理
内存管理的原则 1,对你自己拥有的对象负责.你只能释放自己拥有的对象.(谁污染谁治理). 2,凡是通过retain,alloc,copy等于段获得了所有权对象,都必须在你不再使用的时候释放.调用rel ...
- 如何安装Magento插件
Magento有着非常多的插件,其实就是模块,那么怎么安装需要的插件呢? 具体方法如下,以安装DeveloperToolbar这个开发插件为例讲解: 1.首先到Magento的官方网站查找到相应的插件 ...
- matlab repmat()
一.B = repmat(A,m,n) 将矩阵 A 复制 m×n 块,即把 A 作为 B 的元素,B 由 m×n 个 A 平铺而成.B 的维数是 [size(A,1)*m, size(A,2)*n] ...
- Typescript(ES6) ...用法
简单例子: //数组深拷贝 var arr2 = arr; var arr3 = [...arr]; console.log(arr===arr2); //true, 说明arr和arr2指向同一个数 ...
- 压力测试命令行工具SuperBenchmarker
压力测试命令行工具SuperBenchmarker SuperBenchmarker 是ㄧ个开源的类似于Apache ab的压力测试命令行工具.可以在 .NET 4.52+ 或者 .NET Core ...
- WebService的web客户端同步、异步、多线程向服务端传入参数的数据交互方式
接着上一章的内容,在上一章中我们知道了如何.net平台下去建立一个WebService,以及同步调用.异步调用.开线程异步调用三种客户端从服务端获取数据的方式,在本章了所要讲的,是如何将客户端的数据, ...
- 【MVC】VS常用技巧
1,在VS2010中,选中指定的代码段,可以拖拽到工具箱中,形成标签,以后还想书写类似的代码,双击鼠标即可. 2,在VS2012中,可以在注释上标注//TODO:我是注释 这样,注释就会出现在任务列表 ...
- EditPLus添加到右键图文教程
最近在研究asp听他们说EditPlus非常适合,于是下了一个,感觉还真不错,EditPlus就是一个文本编辑器,说得通俗点他和WINDOWS自带的记事本差不多,但是功能更强,一般应用于程序员编程,因 ...