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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud Ribbon实战以及Ribbon随机策略RandomRule的源码浅析(六)

發布時間:2025/3/15 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud Ribbon实战以及Ribbon随机策略RandomRule的源码浅析(六) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

隨機策略RandomRule的源碼:

/*** 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(); //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; //剛開始不知道服務哪個為nullwhile (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); //假如有三臺微服務serverCount=3 隨機獲取一個數字(nextInt(3)左閉右開)server = upList.get(index); //根據隨機數獲取集合的一個微服務if (server == null) { //如果微服務微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()) { //假如這個微server是活的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} }

上面的源碼我大概做了一個簡單的注釋,我就不多講了

現在我們需求改變,每個服務要求被調用5次

思路如下:

下面我們自定義一個這樣的滿足上面需求的策略算法:

package com.atguigu.myrule;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;public class RandomRule_KZ extends AbstractLoadBalancerRule {// total = 0 // 當total==5以后,我們指針才能往下走,// index = 0 // 當前對外提供服務的服務器地址,// total需要重新置為零,但是已經達到過一個5次,我們的index = 1// 分析:我們5次,但是微服務只有8001 8002 8003 三臺,OK?private int total = 0; // 總共被調用的次數,目前要求每臺被調用5次private int currentIndex = 0; // 當前提供服務的機器號public Server choose(ILoadBalancer lb, Object key) {if (lb == null) {return null;}Server server = null; //剛開始不知道服務哪個為nullwhile (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); //假如有三臺微服務serverCount=3 隨機獲取一個數字(nextInt(3)左閉右開) // server = upList.get(index); //根據隨機數獲取集合的一個微服務// private int total = 0; // 總共被調用的次數,目前要求每臺被調用5次 // private int currentIndex = 0; // 當前提供服務的機器號if(total<5){server = upList.get(currentIndex);total++;}else{total = 0;currentIndex++;if(currentIndex >= upList.size()) {currentIndex = 0;}}if (server == null) { //如果微服務微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()) { //假如這個微server是活的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} }

關鍵的兩個變量

關鍵的算法實現:

這個自定義配置類已經完成,我們需要使用我們自己定義的負載均衡策略算法RandomRule_KZ

package com.atguigu.myrule;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import com.netflix.loadbalancer.RetryRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class MySelfRule {@Beanpublic IRule myRule() {//return new RoundRobinRule();// return new RandomRule();//達到的目的,用我們重新選擇的隨機算法替代默認的輪詢。//自定義算法(每臺機器訪問五次)return new RandomRule_KZ();} }

代碼我們就已經修改完成,我們就測試下能不能實現每個微服務訪問5次的需求,

依然啟動所有的微服務,測試;http://localhost/consumer/dept/get/1

不停刷新會發現每個微服務都被調用五次

不停刷新會發現每個微服務都被調用五次

不停刷新會發現每個微服務都被調用五次

?

綜上所講,通過該源碼的方式我們實現了自己的需求

?

總結

以上是生活随笔為你收集整理的SpringCloud Ribbon实战以及Ribbon随机策略RandomRule的源码浅析(六)的全部內容,希望文章能夠幫你解決所遇到的問題。

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