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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Cloud Alibaba - 10 Ribbon 自定义负载均衡策略(权重算法)

發布時間:2025/3/21 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud Alibaba - 10 Ribbon 自定义负载均衡策略(权重算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Pre
  • 工程
    • 首先屏蔽細粒度配置
    • 然后通過代碼設置一個全局配置 指定 GlobalRibbonConfig
    • GlobalRibbonConfig 設置負載均衡策略
    • 開發自定義策略 (權重訪問)
    • 驗證
  • 源碼


Pre

我們看下Nacos Server上的服務詳情中有個權重

Spring Cloud Alibaba - 07 Ribbon 應用篇及內置的負載均衡算法

沒有根據權重訪問的策略, 自己寫個行不 ?

假設我們一個微服務部署了三臺服務器A,B,C.其中A,B,C三臺服務的性能不一,A的性能最牛逼,B次之,C最差.那么我們設置權重比例 為5 : 3:2 那就說明 10次請求到A上理論是5次,B服務上理論是3次,B服務理論是2次.


工程

artisan-cloud-customcfg-ribbon-order (修改)

artisan-cloud-customcfg-ribbon-pay (無修改)

首先屏蔽細粒度配置

#自定義Ribbon的細粒度配置 (推薦) #artisan-pay-center: # ribbon: # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule # #artisan-product-center: # ribbon: # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule# ribbon 饑餓加載 解決第一次耗時多的問題

然后通過代碼設置一個全局配置 指定 GlobalRibbonConfig

package com.artisan.config;import com.globalconfig.GlobalRibbonConfig; import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: Ribbon 全局配置,通過代碼實現* @date 2022/2/3 0:05* @mark: show me the code , change the world*/@Configuration @RibbonClients(defaultConfiguration = GlobalRibbonConfig.class) public class CustomRibbonConfig2 { }

GlobalRibbonConfig 設置負載均衡策略

package com.globalconfig;import com.artisan.customrules.ArtisanWeightedRule; import com.netflix.loadbalancer.IRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: 全局負載均衡策略* @date 2022/2/3 0:06* @mark: show me the code , change the world*/@Configuration public class GlobalRibbonConfig {@Beanpublic IRule globalConfig() {// 根據權重的規則return new ArtisanWeightedRule();} }

開發自定義策略 (權重訪問)

package com.artisan.customrules;import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.cloud.nacos.ribbon.NacosServer; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractLoadBalancerRule; import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.Server; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired;/*** @author 小工匠* @version 1.0* @description: 自定義權重策略* @date 2022/2/3 0:08* @mark: show me the code , change the world*/ @Slf4j public class ArtisanWeightedRule extends AbstractLoadBalancerRule {@Autowiredprivate NacosDiscoveryProperties discoveryProperties;@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {//讀取配置文件并且初始化,ribbon內部的, 幾乎用不上}@Overridepublic Server choose(Object key) {try {log.info("key:{}", key);BaseLoadBalancer baseLoadBalancer = (BaseLoadBalancer) this.getLoadBalancer();log.info("baseLoadBalancer--->:{}", baseLoadBalancer);//獲取微服務的名稱String serviceName = baseLoadBalancer.getName();//獲取Nacos服務發現的相關組件APINamingService namingService = discoveryProperties.namingServiceInstance();//獲取 一個基于nacos client 實現權重的負載均衡算法Instance instance = namingService.selectOneHealthyInstance(serviceName);//返回一個serverreturn new NacosServer(instance);} catch (NacosException e) {log.error("自定義負載均衡算法錯誤");}return null;} }

可以看到,這里的權重訪問主要是依賴Nacos提供的功能


驗證

權重的取值 0 ~ 1 , 修改兩個節點的訪問權重 0.9 和 0.1

訪問10次

觀察請求日志

當調整為 0.5:0.5 , 再此請求10次


源碼

https://github.com/yangshangwei/SpringCloudAlibabMaster

總結

以上是生活随笔為你收集整理的Spring Cloud Alibaba - 10 Ribbon 自定义负载均衡策略(权重算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

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