日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义负载均衡策略:

發布時間:2024/4/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义负载均衡策略: 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們剛剛講過,只要實現了IRule就可以完成自定義負載均衡,至于具體怎么來,我們先看看他默認的實現

/*** Copyright 2013 Netflix, Inc.** Licensed 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** http://www.apache.org/licenses/LICENSE-2.0** 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 com.netflix.loadbalancer;import java.util.List; import java.util.Random;import com.netflix.client.config.IClientConfig;/*** A loadbalacing strategy that randomly distributes traffic amongst existing* servers.* * @author stonse* */ public class RandomRule extends AbstractLoadBalancerRule {Random rand;public RandomRule() {rand = new Random();}/*** Randomly choose from all living servers*/@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")public Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null;}Server server = null;while (server == null) {if (Thread.interrupted()) {return null;}List<Server> upList = lb.getReachableServers();List<Server> allList = lb.getAllServers();int serverCount = allList.size();if (serverCount == 0) {/** No servers. End regardless of pass, because subsequent passes* only get more restrictive.*/return null;}int index = rand.nextInt(serverCount);server = upList.get(index);if (server == null) {/** The only time this should happen is if the server list were* somehow trimmed. This is a transient condition. Retry after* yielding.*/Thread.yield();continue;}if (server.isAlive()) {return (server);}// Shouldn't actually happen.. but must be transient or a bug.server = null;Thread.yield();}return server;}@Overridepublic Server choose(Object key) {return choose(getLoadBalancer(), key);}@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {// TODO Auto-generated method stub} } 我們來看看這個類AbstractLoadBalancerRule public abstract class AbstractLoadBalancerRule implements IRule, IClientConfigAware {private ILoadBalancer lb;@Overridepublic void setLoadBalancer(ILoadBalancer lb){this.lb = lb;}@Overridepublic ILoadBalancer getLoadBalancer(){return lb;} }

這里我們能發現,還是我們上面所說過的 實現了IRule就能夠自定義負載均衡即使是他默認的策略也實現了IRule

我們可以直接把代碼copy過來改動一點:

package com.luban.rule;import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractLoadBalancerRule; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server;import java.util.List; import java.util.Random;/*** 想要咨詢vip課程相關的同學加一下木蘭老師QQ:2746251334* 想要往期視頻的同學加一下安其拉老師QQ:3164703201* author:魯班學院-商鞅老師*/ public class LubanRule extends AbstractLoadBalancerRule {//原來是純隨機策略 我們現在改為。 如果一個下標已經被隨機到了2次了,第三次還是同樣的下標的話,那就再隨機一次Random rand;private int lastIndex = -1;private int nowIndex = -1;private int skipIndex = -1;public LubanRule() {rand = new Random();}/*** Randomly choose from all living servers*/public Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null;}Server server = null;while (server == null) {if (Thread.interrupted()) {return null;}List<Server> upList = lb.getReachableServers();List<Server> allList = lb.getAllServers();int serverCount = allList.size();if (serverCount == 0) {/** No servers. End regardless of pass, because subsequent passes* only get more restrictive.*/return null;}int index = rand.nextInt(serverCount);System.out.println("當前下標為:"+index);if (skipIndex>=0&&index == skipIndex) {System.out.println("跳過");index = rand.nextInt(serverCount);System.out.println("跳過后的下標:"+index);}skipIndex=-1;nowIndex = index;if (nowIndex == lastIndex) {System.out.println("下一次需要跳過的下標"+nowIndex);skipIndex = nowIndex;}lastIndex = nowIndex;server = upList.get(index);if (server == null) {/** The only time this should happen is if the server list were* somehow trimmed. This is a transient condition. Retry after* yielding.*/Thread.yield();continue;}if (server.isAlive()) {return (server);}// Shouldn't actually happen.. but must be transient or a bug.server = null;Thread.yield();}return server;}@Overridepublic Server choose(Object key) {return choose(getLoadBalancer(), key);}@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {// TODO Auto-generated method stub}}

這里我們就把自己寫的Rule給new出來交給spring 就好了

@Bean public IRule iRule(){return new LubanRule(); }

具體測試的話就不測試了, 那個效果放在筆記上不太明顯,可以自己把代碼copy過去測試一下

?

總結

以上是生活随笔為你收集整理的自定义负载均衡策略:的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。