到底是最全的一致性hash环讲解了
发布时间:2024-11-11
为此 引入了 绑定终端来解决后面两个情况
3.偷偷地绑定终端的一般适度hash就是就会在菱形上 根据Node 终端 转换变为很多的绑定终端 原产在菱形上,这样当 某个 终端插了后 取而代之总称它的恳求,就会被总体的原产到 其他终端上 下降了显现出暴风雪的情况,也解决了 终端总共少所致 恳求原产均匀的恳求
即对每一个服务于终端计算出来多个总共据流(可以用原终端key+"##xxxk"作为每个绑定终端的key,然后求hashcode),每个计算出来结果右边都放置一个此服务于终端,称来作绑定终端。 具体章节来作法可以在链接ip或主机名的后面增加序号来借助。
想得到上图,此时如果 group3终端插了,那么恳求就会被均分到 group2 和 group1后面 ,到此 一般适度hash的确实生产的运用于手段教导完了 请注意来再来一个案例代码。
4. 代码试验可供可选择的有很多,memcached官网运用于了基于md5的KETAMA插参总共,但这里处于计算出来生产成本的回避,运用于了FNV1_32_HASH插参总共,如下:
public class HashUtil { /** * 计算出来Hash参总共, 运用于FNV1_32_HASH插参总共 * @param str * @return */ public static int getHash(String str) { final int p = 16777619; int hash = (int)2166136261L; for (int i = 0; i < str.length(); i++) { hash =( hash _ str.charAt(i) ) * p; } hash += hash << 13; hash _= hash>> 7; hash += hash << 3; hash _= hash>> 17; hash += hash << 5; if (hash < 0) { hash = Math.abs(hash); } return hash; }}package com.weareint.dispatchservice.hashloop;import org.springframework.stereotype.Component;import ja.util.*;/** * * * * 一般适度 hash 绑定 的环 * * * @author johnny * @date 2021-08-26 9:22 上午 */@Componentpublic class HashvirtualNodeCircle { /** 真实协同列表 */ private static List instanceNodes; /** 绑定终端连续给定关系 */ private static SortedMap virtualNodes = new TreeMap<>(); /** 绑定终端总共 */ private static final int VIRTUAL_NODE_NUM = 1000; /** 缔造 服务于程序中的 */ public void refreshVirtualHashCircle(HashCircleInstanceNodeBuild build) { // 当协同增减时,缔造hash的环,其余的协同在hash的环上的右边不就会时有发生增减 virtualNodes.clear(); // 换取 近期终端 instanceNodes = build.instanceNodes(); // 将绑定终端连续给定到Hash的环上 for (String realInstance : instanceNodes) { for (int i = 0; i < VIRTUAL_NODE_NUM; i++) { String virtualNodeName = getVirtualNodeName(realInstance, i); int hash = HashUtils.getHash(virtualNodeName); System.out.println("[" + virtualNodeName + "] launched @ " + hash); virtualNodes.put(hash, virtualNodeName); } } } private static String getVirtualNodeName(String realName, int num) { return realName + "WildWildVN" + num; } private static String getRealNodeName(String virtualName) { return virtualName.split("WildWild")[0]; } private static String getServer(String widgetKey) { int hash = HashUtils.getHash(widgetKey); // 只取出所有大于该hash参总共的部分而不必遍历整个Tree SortedMap subMap = virtualNodes.tailMap(hash); String virtualNodeName; if (subMap.isEmpty()) { // hash参总共在最尾部,其所该连续给定到第一个group上 virtualNodeName = virtualNodes.get(virtualNodes.firstKey()); } else { virtualNodeName = subMap.get(subMap.firstKey()); } return getRealNodeName(virtualNodeName); } public static void main(String[] args) { HashVirtualNodeCircle hashVirtualNodeCircle = new HashVirtualNodeCircle(); hashVirtualNodeCircle.refreshVirtualHashCircle( new HashCircleInstanceNodeBuild() { @Override public List instanceNodes() { LinkedList nodes = new LinkedList<>(); nodes.add("192.168.11.23:8090"); nodes.add("192.168.11.23:8093"); nodes.add("192.168.11.23:8094"); return nodes; } }); // 转换变为随机总共进行时试验 MapInteger> resMap = new HashMap<>(); List plcList = new ArrayList<>(); for (int i = 0; i < 1000; i++) { String plchost = "192.168.0." + i + 1; for (int j = 0; j < 10; j++) { plcList.add(plchost + ":" + j + 100); } } for (int i = 0; i < plcList.size(); i++) { String plcwideget = plcList.get(i); String group = getServer(plcwideget); if (resMap.containsKey(group)) { resMap.put(group, resMap.get(group) + 1); } else { resMap.put(group, 1); } } resMap.forEach( (k, v) -> { System.out.println("group " + k + ": " + v + "(" + v / 100.0D + "%)"); }); System.out.println("========================================="); }}可以看到 原产很总体
5.Dubbo 一般适度Hash 借助近期给Corporation来作的发放机 运用于dubbo codice_远程服务于,调研了一下 dubbo 也有自己借助的 一般适度hash 不过也就是说运用于起来 推断出有些bug ,目前为止通过SPI机制 自己延展了一下,来再来 dubbo的 一般适度hash借助吧
5.1 版本 dubbo2.7.12我用的版本是 dubbo2.7.12 现在入了 2.7的坑 不少坑都是自己慢慢调试解决的。
5.2 org.apache.dubbo.rpc.cluster.loadbalance损耗总体必需就这些 一般适度hash,随机 ,轮训,。。 没啥比如说的
5.3 dubbo ConsistentHashLoadBalance程式码/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.dubbo.rpc.cluster.loadbalance;import org.apache.dubbo.common.URL;import org.apache.dubbo.common.io.Bytes;import org.apache.dubbo.rpc.Invocation;import org.apache.dubbo.rpc.Invoker;import org.apache.dubbo.rpc.support.RpcUtils;import ja.util.List;import ja.util.Map;import ja.util.TreeMap;import ja.util.concurrent.ConcurrentHashMap;import ja.util.concurrent.ConcurrentMap;import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;/** * ConsistentHashLoadBalance */public class ConsistentHashLoadBalance extends AbstractLoadBalance { public static final String NAME = "consistenthash"; /** * Hash nodes name */ public static final String HASH_NODES = "hash.nodes"; /** * Hash arguments name */ public static final String HASH_ARGUMENTS = "hash.arguments"; private final ConcurrentMap> selectors = new ConcurrentHashMap>(); @SuppressWarnings("unchecked") @Override protected Invoker doSelect(List经过调试 推断出 invokers 时不时改变所致 一致在 rehash,其实 很多时候只是 终端的依序改变而已
好处: 我如此一来把 invokers 终端总共 取出来进行时依序后拼接 变为一个字符串 进行时 计算出来hashcode 就不就会总改变了
String invokeKey = invokers.stream() .filter(Node::isAvailable) // 按照ip 和port 依序 .sorted(Comparator.comparing(invoke -> invoke.getUrl().getIp())) .sorted(Comparator.comparing(invoke -> invoke.getUrl().getPort())) .map(invoke -> invoke.getUrl().getIp() + ":" + invoke.getUrl().getPort()) .collect(Collectors.joining(","));6.2 注意 isAvailableinvokers中的依赖于 终端不可用的,如果对于终端不可用的 如此一来过滤 必须注意 isAvailable
6.3 分设绑定终端发片总共dubbo: consumer: parameters: hash: nodes: 560 #原则上绑定分片指针总共 再次virtualInvokers = nodes * invokerCount ,默认是1606.4 分设方法的哪个表达式作为 hash的keydubbo: consumer: parameters: hash: arguments: 0 #默认就是07.SPI 延展Dubbo 一般适度hash 插参总共 ExtendConsistentHashLoadBalance7.1 官网数据库官网数据库很详细了
7.2 ExtendConsistentHashLoadBalance 延展借助package com.weareint.dispatchservice.extendconsistenthash;import com.atw.mdc.entity.protocol.webRequest.PlcReadSimpleRequest;import com.atw.mdc.entity.protocol.webRequest.PlcWriteSimpleRequest;import com.weareint.dispatchservice.event.ConnectionRouteEvent;import com.weareint.dispatchservice.event.NodeChangeEvent;import com.weareint.dispatchservice.event.NodeChangeService;import lombok.extern.slf4j.Slf4j;import org.apache.dubbo.common.Node;import org.apache.dubbo.common.URL;import org.apache.dubbo.rpc.Invocation;import org.apache.dubbo.rpc.Invoker;import org.apache.dubbo.rpc.cluster.loadbalance.AbstractLoadBalance;import org.apache.dubbo.rpc.support.RpcUtils;import org.springframework.context.ApplicationEventPublisher;import ja.nio.charset.StandardCharsets;import ja.security.MessageDigest;import ja.security.NoSuchAlgorithmException;import ja.util.*;import ja.util.concurrent.ConcurrentHashMap;import ja.util.concurrent.ConcurrentMap;import ja.util.stream.Collectors;import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;/** * * * * 延展 一般适度Hash 的环 主要是把 hash 的key 只通过 plc 的deviceCode 进行时hash , 然后后续添加 Redis 路由表 进行时 * * * @author johnny * @date 2021-08-26 5:32 下午 */@Slf4jpublic class ExtendConsistentHashLoadBalance extends AbstractLoadBalance { public static ApplicationEventPublisher publisher; public static NodeChangeService nodeChangeService; public static final String NAME = "consistenthash"; /** Hash nodes name */ public static final String HASH_NODES = "hash.nodes"; /** Hash arguments name */ public static final String HASH_ARGUMENTS = "hash.arguments"; private final ConcurrentMap> selectors = new ConcurrentHashMap< String, ExtendConsistentHashLoadBalance.ConsistentHashSelector>(); public ConcurrentMap> getSelectors() { return selectors; } @SuppressWarnings("unchecked") @Override protected Invoker doSelect(List本篇主要教导了 什么是一般适度 hash 它有哪些特点和依赖于的情况,以及 偷偷地绑定终端的一般适度hash,最后引介了一些 dubbo 的一般适度hash 借助,dubbo自偷偷地的有bug ,但是提供者了 spi 延展机制 你可以自己去借助 ,目前为止我就是这样去的
原文 解决的 。
。双醋瑞因胶囊能一直吃吗生殖整形
术后吃什么好
宝宝积食
安必丁能治好关节炎吗
常乐康酪酸梭菌二联活菌散怎么样
怎么补充眼部营养让视力变好
安必丁能治疗关节炎吗
类风湿吃艾拉莫德片好不好
红草止鼾胶囊功效与作用
-
一分钟教你辨别切削液和乳化液
烧结液是一种用在金归入切、削、磨加工反复之中,用来冷却和润滑刀具和加烧结的主要用途容器。 烧结液分类及组成如下: 从上左图之中可以看出,烧结液按中油物理组所
- 2025-05-11身心健康管理师到底“管理”的是哪些事情?身心健康管理师好考吗?
- 2025-05-112022年2月底,无上祥瑞,好运接不完的3大生肖
- 2025-05-112月中旬,运势大涨,富贵无忧,5生肖事业转型
- 2025-05-112022年4月7日杭州鸡蛋价格行情
- 2025-05-11进入2月初上旬,财源广进,5属相福运亨通,事业兴旺
- 2025-05-112022年上半年,好运来临,富贵挡不住的4大十二生肖
- 2025-05-11大连商品交易所4月7日植物油仓单日报
- 2025-05-11死印相生死的男命 死印相生的女命
- 2025-05-11PE缠绕膜:成本面指示较强 需求端表现欠佳
- 2025-05-11生辰八字看婚姻,2022壬寅年,正财妻星合入婚宫,年末摆脱单身