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 ...
随机推荐
- Android中Parcel的分析以及使用
简单点来说:Parcel就是一个存放读取数据的容器, Android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel ...
- SWIFT 之CoreData初试
SWIFT中使用CoreData来保存本地数据,在建立项目的时候把 "Use Core Data"选项选上 项目建立完成后点击后缀为 .xcdatamodeld的那个文件,点击右下 ...
- ZOJ 2965 Accurately Say "CocaCola"!
Time Limit: 2 Seconds Memory Limit: 65536 KB In a party held by CocaCola company, several stude ...
- Python中的import
模块(module):用来从逻辑(实现一个功能)上组织Python代码(变量.函数.类),本质就是*.py文件.文件是物理上组织方式"module_name.py",模块是逻辑上组 ...
- 【leetcode】2-AddTwoNums
problem: Add Two Numbers 需要学习的是单向链表的基础使用:
- (2)hashlib模块(加密算法模块)
hash算法模块内有很多种,如md5.sha1等,只是加密的程度不一样 hash是一种算法 该算法接收传入的文本内容,经过hash运算得到一串hash值 hash值具备三个特点: 1. 如果传入的内容 ...
- CTF-练习平台-Misc之 MISC图穷匕见
十七.MISC图穷匕见 用txt打开,发现文件尾有东西,截取出来 用notepad++的插件 HEX转ASCII 得到35019个坐标 根据图片的详细信息的提示 应该是要把这些坐标转换为图形 这里使用 ...
- Hash表的平均查找长度ASL计算方法
Hash表的“查找成功的ASL”和“查找不成功的ASL” ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数: H(Key) = (key x 3) MOD 7 ...
- EasyPHP-Devserver-17的坑位
mysql登陆错误:error: 'Plugin '*2A8AF30E682613A2F1CE1E28BA11D8560B294DCE' is not loaded' http://stackover ...
- 一个5.0/3.3V双向通讯的电路
来自群友 西江月-梧州 的分享 硬件程工-深圳福永(79993868) 17:06:33 当3.3V高时二极管阳极为3.3V,阴极接了10K上拉为5V,二极管的压降为反向,此时二极管不导通. 硬件程工 ...