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 ...
随机推荐
- anu - component
import { extend, isFn, options, clearArray, noop } from "./util"; import { CurrentOwner } ...
- mysql_query — 发送一条 MySQL 查询
仅对 SELECT,SHOW,EXPLAIN 或 DESCRIBE 语句返回 一个资源标识符,如果查询执行不正确则返回 FALSE.对于 其它类型的 SQL 语句,在执行成功时返回 TRUE,出错时返 ...
- mms:源码浅析
程序启动 程序的入口:ConversationList.java,对应主页中短信的快捷方式.由此进入短信列表模块. 短信列表模块 该模块的展示是由ConversationList.java类实现的,该 ...
- PyQt5编程:鼠标事件
参考链接:https://www.cnblogs.com/zhuluqing/p/9028816.html 一.每个事件都被封装成相应的类: pyqt中,每个事件类型都被封装成相应的事件类,如鼠标事件 ...
- html5、canval 对 图片的压缩
let src = this.cropper.getCroppedCanvas().toDataURL('image/jpeg');let can = document.createElement(' ...
- Windows 下python的tab自动补全
方法一:安装一个ipython就OK啦,而且关键字还能高亮显示呢 一.打开cmd,输入pip3 install ipython联网安装 二.安装成功后,cmd里运行ipython,成功啦. 方法二:写 ...
- Ubuntu16.04 和 hadoop2.7.3环境下 hive2.1.1安装部署
参考文献: http://blog.csdn.NET/reesun/article/details/8556078 http://blog.csdn.Net/zhongguozhichuang/art ...
- Ubuntu install TensorFlow
/******************************************************************************** * Ubuntu install T ...
- 【opencv基础】opencv和dlib库中rectangle类型之间的转换
前言 最近使用dlib库的同时也会用到opencv,特别是由于对dlib库的画图函数不熟悉,都想着转换到opencv进行show.本文介绍一下两种开源库中rectangle类型之间的转换. 类型说明 ...
- php-fpm配置及操作
启动与停止 启动 /usr/local/php/sbin/php-fpm 停止 pkill php-fpm