当前位置: 首页 > news >正文

家具网站建设公司wordpress 变私有云

家具网站建设公司,wordpress 变私有云,哪些企业网站比较好,网站上海网站建设一、负载均衡概述 支持轮询、随机、一致性hash和最小活跃数等。 1、轮询 ① sequences#xff1a;内部的序列计数器 ② 服务器接口方法权重一样#xff1a;#xff08;sequences1#xff09;%服务器的数量#xff08;决定调用#xff09;哪个服务器的服务。 ③ 服务器…一、负载均衡概述 支持轮询、随机、一致性hash和最小活跃数等。 1、轮询 ① sequences内部的序列计数器 ② 服务器接口方法权重一样sequences1%服务器的数量决定调用哪个服务器的服务。 ③ 服务器接口方法权重不一样找到最大权重权重数%sequences1然后找出权重比该取模后的值大服务器列表最后进行【①】所述。 2、随机 统计服务器上该接口的方法权重总和然后对这个总和随机nextInt一下看生成的随机数落到哪个段内就调用哪个服务器上的该服务。 3、一致性hash 保证了同样的请求参数将会落到同一台服务器上。 4、最小活跃数 每个接口和接口方法都对应一个RpcStatus记录它们的活跃数、失败等相关统计信息调用时活跃数1调用结束时活跃数-1所以活跃值越大表明该提供者服务器的该接口方法耗时越长而消费能力强的提供者接口往往活跃值很低。最少活跃负载均衡保证了“慢”提供者能接收到更少的服务器调用。 二、负载均衡策略配置 1、多注册中心集群负载均衡 2、Cluster Invoker 支持的选址策略如下dubbo2.7.5 版本具体使用请参见文档 2-1、指定优先级 !-- 来自 preferred“true” 注册中心的地址将被优先选择 只有该中心无可用地址时才 Fallback 到其他注册中心 -- dubbo:registry addresszookeeper://${zookeeper.address1} preferredtrue /2-2、同zone优先 !-- 选址时会和流量中的 zone key 做匹配流量会优先派发到相同 zone 的地址 -- dubbo:registry addresszookeeper://${zookeeper.address1} zonebeijing /2-3、权重轮询 !-- 来自北京和上海集群的地址将以 10:1 的比例来分配流量 -- dubbo:registry idbeijing addresszookeeper://${zookeeper.address1} weight”100“ / dubbo:registry idshanghai addresszookeeper://${zookeeper.address2} weight”10“ /三、负载均衡解读 1负载均衡AbstractClusterInvoker.invoke(final Invocation invocation)方法 Override public Result invoke(final Invocation invocation) throws RpcException {//...... 省略代码 ListInvokerT invokers list(invocation);LoadBalance loadbalance initLoadBalance(invokers, invocation);RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);return doInvoke(invocation, invokers, loadbalance); } /** 获取负载均衡的实现方法未配置时默认random */ protected LoadBalance initLoadBalance(ListInvokerT invokers, Invocation invocation) {if (CollectionUtils.isNotEmpty(invokers)) {return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl().getMethodParameter(RpcUtils.getMethodName(invocation), LOADBALANCE_KEY, DEFAULT_LOADBALANCE));} else {return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(DEFAULT_LOADBALANCE);} }2实现入口AbstractClusterInvoker.doSelect(…) ① 在dubbo中所有负载均衡均继承 AbstractLoadBalance 类该类实现了LoadBalance接口并封装了一些公共的逻辑。 /** 1. LoadBalance.java接口 */ SPI(RandomLoadBalance.NAME) public interface LoadBalance {/*** select one invoker in list.** param invokers invokers.* param url refer url* param invocation invocation.* return selected invoker.*/Adaptive(loadbalance)T InvokerT select(ListInvokerT invokers, URL url, Invocation invocation) throws RpcException; } /** 2. AbstractLoadBalance.java 抽象类 */ Override public T InvokerT select(ListInvokerT invokers, URL url, Invocation invocation) {if (CollectionUtils.isEmpty(invokers)) {return null;}// 如果invokers列表中仅一个Invoker直接返回即可无需进行负载均衡if (invokers.size() 1) {return invokers.get(0);}// 调用doSelect方法进行负载均衡该方法为抽象方法由子类实现return doSelect(invokers, url, invocation); }protected abstract T InvokerT doSelect(ListInvokerT invokers, URL url, Invocation invocation);/** 2-1. 公共方法权重计算逻辑 */ protected int getWeight(Invoker? invoker, Invocation invocation) {int weight;URL url invoker.getUrl();// Multiple registry scenario, load balance among multiple registries.if (REGISTRY_SERVICE_REFERENCE_PATH.equals(url.getServiceInterface())) {weight url.getParameter(REGISTRY_KEY . WEIGHT_KEY, DEFAULT_WEIGHT);} else {// 从url中获取权重 weight配置值weight url.getMethodParameter(invocation.getMethodName(), WEIGHT_KEY, DEFAULT_WEIGHT);if (weight 0) {// 获取服务提供者启动时间戳long timestamp invoker.getUrl().getParameter(TIMESTAMP_KEY, 0L);if (timestamp 0L) {// 计算服务提供者运行时长long uptime System.currentTimeMillis() - timestamp;if (uptime 0) {return 1; // 未启动直接返回权重为1}// 获取服务预热时间默认为10分钟int warmup invoker.getUrl().getParameter(WARMUP_KEY, DEFAULT_WARMUP);// 如果服务运行时间小于预热时间则重新计算服务权重即降级if (uptime 0 uptime warmup) {weight calculateWarmupWeight((int)uptime, warmup, weight);}}}}return Math.max(weight, 0); }// 2-2. 重新计算服务权重 static int calculateWarmupWeight(int uptime, int warmup, int weight) {// 计算权重下面代码逻辑上形似于 (uptime / warmup) * weight// 随着服务运行时间 uptime 增大权重计算值 ww 会慢慢接近配置值 weightint ww (int) ( uptime / ((float) warmup / weight));return ww 1 ? 1 : (Math.min(ww, weight)); }注 select 方法的逻辑比较简单首先会检测 invokers 集合的合法性然后再检测 invokers 集合元素数量。如果只包含一个 Invoker直接返回该 Inovker 即可。如果包含多个 Invoker此时需要通过负载均衡算法选择一个 Invoker。具体的负载均衡算法由子类实现。 权重的计算主要用于保证当服务运行时长小于服务预热时间时对服务进行降权避免让服务在启动之初就处于高负载状态。服务预热是一个优化手段与此类似的还有 JVM 预热。主要目的是让服务启动后“低功率”运行一段时间使其效率慢慢提升至最佳状态。 配置方式默认100dubbo.provider.weight300dubbo.provider.weight300 ② RandomLoadBalance 加权随机负载均衡 算法思路根据权重比随机选择哪台服务器如servers[A,B,C]weights [5, 3, 2]权重和为10调用A的次数约有50%B有30%C有20%。 Override protected T InvokerT doSelect(ListInvokerT invokers, URL url, Invocation invocation) {// Number of invokersint length invokers.size();// 判断是否需要权重负载均衡if (!needWeightLoadBalance(invokers,invocation)){return invokers.get(ThreadLocalRandom.current().nextInt(length));}// Every invoker has the same weight?boolean sameWeight true;// the maxWeight of every invokers, the minWeight 0 or the maxWeight of the last invokerint[] weights new int[length];// The sum of weightsint totalWeight 0;// ① 计算总权重totalWeight② 检测每个服务提供者的权重是否相同for (int i 0; i length; i) {int weight getWeight(invokers.get(i), invocation);// SumtotalWeight weight;// save for later use// 如果权重分别为5,3,2则weights[0]5weights[1]8weights[2]10weights[i] totalWeight;// 判断每个服务权重是否相同如果不相同则sameWeight置为falseif (sameWeight totalWeight ! weight * (i 1)) {sameWeight false;}}// 各提供者服务权重不一样时计算随机数落在哪个区间上if (totalWeight 0 !sameWeight) {// If (not every invoker has the same weight at least one invokers weight0), select randomly based on totalWeight.// 随机获取一个[0, totalWeight]区间内的随机的数字int offset ThreadLocalRandom.current().nextInt(totalWeight);// Return a invoker based on the random value.for (int i 0; i length; i) {// weights[0]5offset[0, 5)// weights[1]8offset[5, 8)// weights[2]10offset[8, 10)if (offset weights[i]) {return invokers.get(i);}}}// If all invokers have the same weight value or totalWeight0, return evenly.// 如果所有服务提供者权重值相同此时直接随机返回一个即可return invokers.get(ThreadLocalRandom.current().nextInt(length)); }注RandomLoadBalance的算法比较简单多次请求后能够按照权重进行“均匀“分配。调用次数少时可能产生的随机数比较集中此缺点并不严重可以忽略。它是一个高效的负载均衡实现因此Dubbo选择它作为缺省实现。 ③ LeastActiveLoadBalance 加权最小活跃数负载均衡 活跃调用数越小表明该服务提供者效率越高单位时间内可处理更多的请求。此时应优先将请求分配给该服务提供者。 在具体实现中每个服务提供者对应一个活跃数 active。初始情况下所有服务提供者活跃数均为0。每收到一个请求活跃数加1完成请求后则将活跃数减1。 除了最小活跃数LeastActiveLoadBalance 在实现上还引入了权重值。所以准确的来说LeastActiveLoadBalance 是基于加权最小活跃数算法实现的。 Override protected T InvokerT doSelect(ListInvokerT invokers, URL url, Invocation invocation) {// Number of invokersint length invokers.size();// The least active value of all invokers// 最小活跃数int leastActive -1;// The number of invokers having the same least active value (leastActive)// 具有相同“最小活跃数”的服务提供者int leastCount 0;// The index of invokers having the same least active value (leastActive)// leastIndexes 用于记录具有相同“最小活跃数”的 Invoker 在 invokers 列表中的下标信息int[] leastIndexes new int[length];// the weight of every invokersint[] weights new int[length];// The sum of the warmup weights of all the least active invokersint totalWeight 0;// The weight of the first least active invoker// 第一个最小活跃数的 Invoker 权重值用于与其他具有相同最小活跃数的 Invoker 的权重进行对比// 以检测是否“所有具有相同最小活跃数的 Invoker 的权重”均相等int firstWeight 0;// Every least active invoker has the same weight value?// 表示各服务的权重是否相同boolean sameWeight true;// Filter out all the least active invokersfor (int i 0; i length; i) {InvokerT invoker invokers.get(i);// Get the active number of the invoker// 获取invoker对应的活跃数int active RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive();// Get the weight of the invokers configuration. The default value is 100.// 获取权重int afterWarmup getWeight(invoker, invocation);// save for later useweights[i] afterWarmup;// If it is the first invoker or the active number of the invoker is less than the current least active number// 发现更小的活跃数重新开始if (leastActive -1 || active leastActive) {// Reset the active number of the current invoker to the least active number// 使用当前活跃数 active 更新最小活跃数 leastActiveleastActive active;// Reset the number of least active invokersleastCount 1;// Put the first least active invoker first in leastIndexes// 记录当前下标值到 leastIndexes 中leastIndexes[0] i;// Reset totalWeighttotalWeight afterWarmup;// Record the weight the first least active invokerfirstWeight afterWarmup;// Each invoke has the same weight (only one invoker here)sameWeight true;// If current invokers active value equals with leaseActive, then accumulating.// 当前 Invoker 的活跃数 active 与最小活跃数 leastActive 相同} else if (active leastActive) {// Record the index of the least active invoker in leastIndexes orderleastIndexes[leastCount] i;// Accumulate the total weight of the least active invoker// 累加权重totalWeight afterWarmup;// If every invoker has the same weight?// 检测当前 Invoker 的权重与 firstWeight 是否相等不相等则将 sameWeight 置为 falseif (sameWeight afterWarmup ! firstWeight) {sameWeight false;}}}// Choose an invoker from all the least active invokers// 1. 当只有一个 Invoker 具有最小活跃数此时直接返回该 Invoker 即可if (leastCount 1) {// If we got exactly one invoker having the least active value, return this invoker directly.return invokers.get(leastIndexes[0]);}// 2. 有多个 Invoker 具有相同的最小活跃数但它们之间的权重不同if (!sameWeight totalWeight 0) {// If (not every invoker has the same weight at least one invokers weight0), select randomly based on // totalWeight.// 随机生成一个 [0, totalWeight) 之间的数字int offsetWeight ThreadLocalRandom.current().nextInt(totalWeight);// Return a invoker based on the random value.for (int i 0; i leastCount; i) {// 获取权重数组的下标int leastIndex leastIndexes[i];// 随机权重 - 具有最小活跃数的 Invoker 的权重值offsetWeight - weights[leastIndex];if (offsetWeight 0) { // 与RandomLoadBalance一致权重越大调用的次数越多return invokers.get(leastIndex);}}}// If all invokers have the same weight value or totalWeight0, return evenly.// 如果权重相同或权重为0时随机返回一个 Invokerreturn invokers.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]); }④ ConsistentHashLoadBalance cache-1、cache-2、cache-3、cache-4分别为不同的节点 根据IP或者其他信息为缓存节点生成一个hash并将这个hash投射到[0,2^32 - 1] 的圆环上。当有查询或写入请求时则为缓存项的key生成一个hash值。然后查找第一个大于或等于该hash值的缓存节点并到这个节点中查询或写入缓存项。 如果当前节点挂了则在下一次查询或写入缓存时为缓存项查找另一个大于或其hash值的缓存节点即可。 如下图每个节点在圆环上占据一个位置如果缓存项的key的hash值小于缓存节点hash值则到该缓存节点中存储或读取缓存项。 比如下面绿色点对应的缓存项将会被存储到 cache-2 节点中。由于 cache-3 挂了原本应该存到该节点中的缓存项最终会存储到 cache-4 节点中。 ⑤ RoundRobinLoadBalance 轮询是指将请求轮流分配给每台服务器。 例如有三台服务器A、B、C我们将第一个请求分配给A服务器第二个请求分配给B服务器第三个请求分配给C服务器第四个请求再次分配给A服务器如此循环这个过程叫做轮询轮询是一种无状态负载均衡算法。适用于每台服务器性能相近的场景下。 轮询加权对每台性能不一样的服务器进行加权处理如服务器A、B、C的权重分别为5:2:1时在8次请求中服务器A将收到5次请求、B收到2次请求、C收到一次请求请求次数越多每台服务器得到的请求数接近服务器的权重比。 Override protected T InvokerT doSelect(ListInvokerT invokers, URL url, Invocation invocation) {// key [group/]path[:version].methodName注path com.jyt.*.service.接口名String key invokers.get(0).getUrl().getServiceKey() . invocation.getMethodName();// 获取key对应值如果key的值不存在则将第二个参数的返回值存入并返回ConcurrentMapString, WeightedRoundRobin map methodWeightMap.computeIfAbsent(key, k - new ConcurrentHashMap());int totalWeight 0;long maxCurrent Long.MIN_VALUE;long now System.currentTimeMillis();InvokerT selectedInvoker null;WeightedRoundRobin selectedWRR null;// 遍历服务提供者for (InvokerT invoker : invokers) {// dubbo://11.1.1.109:21001/com.jyt.*.service.类名String identifyString invoker.getUrl().toIdentityString();// 获取当前服务提供者的权重int weight getWeight(invoker, invocation);// 根据identifyString获取当前提供者对应的权重如果不存在则使用第二个参数返回值并返回WeightedRoundRobin weightedRoundRobin map.computeIfAbsent(identifyString, k - {WeightedRoundRobin wrr new WeightedRoundRobin();wrr.setWeight(weight);return wrr;});// 如果提供者的权重被修改了则更新weightedRoundRobin的权重值if (weight ! weightedRoundRobin.getWeight()) {// weight changedweightedRoundRobin.setWeight(weight);}// current加上weight并获取结果初始的current为0long cur weightedRoundRobin.increaseCurrent();/*** 如果A服务权重weight500B权重weight100时totalWeight 600初始cur0服务调用场景* 第一次A服务器curweight curA 500cur maxCurrent所以maxCurrent curA 500* B服务器curweight curB 100 maxCurrent500为true故走A服务器即curA curA - 600 -100** 第二次A服务器curweight curA 400cur maxCurrent所以maxCurrent curA 400* B服务器curweight curB 200 maxCurrent400为true故走A服务器即curA curA - 600 -200** 第三次A服务器curweight curA 300cur maxCurrent所以maxCurrent curA 300* B服务器curweight curB 300 maxCurrent300为true故走A服务器即curA curA - 600 -300** 第四次A服务器curweight curA 200cur maxCurrent所以maxCurrent curA 200* B服务器curweight curB 400 maxCurrent200为false故走B服务器即curB curB - 600 -200** 第五次A服务器curweight curA 700cur maxCurrent所以maxCurrent curA 700* B服务器curweight curB -100 maxCurrent700为true故走A服务器即curA curA - 600 100** 第六次A服务器curweight curA 600cur maxCurrent所以maxCurrent curA 600* B服务器curweight curB 0 maxCurrent600为true故走A服务器即curA curA - 600 0* * ... ... 如此循环A、A、A、B、A、A*/weightedRoundRobin.setLastUpdate(now);// 判断是否比最大的值大if (cur maxCurrent) {// 如果大则将当前服务提供者置为本次服务提供者maxCurrent cur;selectedInvoker invoker;selectedWRR weightedRoundRobin;}// 权重累计totalWeight weight;}// 当两者大小不一致时map中可能会存在一些已经下线的服务本次剔除一些很久节点信息if (invokers.size() ! map.size()) {map.entrySet().removeIf(item - now - item.getValue().getLastUpdate() RECYCLE_PERIOD);}// 如果存在选择好的提供者则改变他的current值 - totalWeight;if (selectedInvoker ! null) {selectedWRR.sel(totalWeight);return selectedInvoker;}// should not happen herereturn invokers.get(0); }
http://www.pierceye.com/news/505800/

相关文章:

  • 邱县网站建设河北seo网络优化师
  • iis5.1怎么新建网站中国生意网
  • 教你做吃的网站厦门市集美区建设局网站
  • 电子商务网站建设需要青岛网站建设邓巴迪
  • 网站建设考虑的因素建设网站地图
  • 天津塘沽网站建设新网官方网站
  • 做本地团购网站国外产品网站
  • 湖北省两学一做网站国外浏览器app下载
  • 遵义网站建设公司巴彦淖尔市网站制作
  • 后台管理系统网站模板合作网站登录制作
  • 腾讯云网站备案流程seo优化实训总结
  • 那个网站有免费的模板wordpress首页模板文件
  • 阿里云主机可以放几个网站手机上自己设计广告的软件
  • 南通公司企业网站建设淘宝网店制作
  • 长沙网站建设推广太仓网站建设企业网站
  • 加强网站微信信息编辑队伍建设网站设计 北京店
  • 广州网站建设有限公司程序员入门先学什么
  • 资源下载类网站如何做外链北京有几家宽带网络公司
  • 个人网站要有什么网页设计与制作初学者教程
  • 宁波品牌网站建设芗城区建设局网站
  • 仿卢松松博客网站源码购买网站空间多少钱
  • 无锡网站推微信公众号文章里好看的图片在哪个网站做
  • 做网站建设费用预算龙岩市建筑设计院
  • 网站做适配多少钱浙江建设厅特种考试查询
  • 简单的网站更新 关键词优化 关键词互联如何做网
  • 重庆网站seo搜索引擎优化网站qq登录 开发
  • 备案号放网站下面居中物流网站制作目的
  • 房产网站排名做情侣网站
  • 营销型网站建设推荐国内团购网站做的最好的是
  • 前端网站建设苏州高新区建设局网站管网