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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Cloud Alibaba - 08 Ribbon 两种方式实现细粒度自定义配置控制微服务的负载均衡策略

發布時間:2025/3/21 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud Alibaba - 08 Ribbon 两种方式实现细粒度自定义配置控制微服务的负载均衡策略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 需求
  • 工程
  • java代碼實現細粒度配置 (不推薦)
  • 配置實現細粒度配置 (推薦)
  • 源碼


需求

假設我們有個場景:

Order-Center 需要采用隨機算法調用產品中心 , 而采用輪詢算法調用其他中心微服務


工程


java代碼實現細粒度配置 (不推薦)

注意事項: PayCenterRibbonConfig,ProductCenterRibbonConfig不能被放在我們主啟動類所在包以及子包下,不然就起不到細粒度配置

【支付中心對應的Ribbon訪問策略】

package com.globalconfig;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RoundRobinRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2022/2/2 19:42* @mark: show me the code , change the world*/ @Configuration public class PayCenterRibbonConfig {@Beanpublic IRule roundRobinRule() {return new RoundRobinRule();} }

【產品中心對應的Ribbon訪問策略】

package com.globalconfig;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2022/2/2 19:45* @mark: show me the code , change the world*/ @Configuration public class ProductCenterRibbonConfig {@Beanpublic IRule randomRule() {return new RandomRule();} }

【帶有@LoadBalanced的RestTemplate】

package com.artisan.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2022/2/2 15:47* @mark: show me the code , change the world*/ @Configuration public class WebConfig {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}}

【為每個服務分配 訪問策略 】

package com.artisan.config;import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2022/2/2 19:51* @mark: show me the code , change the world*/@Configuration @RibbonClients(value = {@RibbonClient(name = "artisan-product-center",configuration = com.globalconfig.ProductCenterRibbonConfig.class),@RibbonClient(name = "artisan-pay-center",configuration = com.globalconfig.PayCenterRibbonConfig.class) }) public class CustomRibbonConfig { }

【Controller 】

import com.artisan.common.entity.OrderInfo; import com.artisan.common.entity.PayInfo; import com.artisan.common.entity.ProductInfo; import com.artisan.common.vo.OrderAndPayVo; import com.artisan.common.vo.OrderVo; import com.artisan.mapper.OrderInfoMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2022/2/2 03:20* @mark: show me the code , change the world*/ @RestController @Slf4j public class OrderInfoController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate OrderInfoMapper orderInfoMapper;/*** 調用地址, 注冊中心 地址*/public static final String PRODUCT_URI = "http://artisan-product-center/selectProductInfoById/";public static final String PAY_URI = "http://artisan-pay-center/selectPayInfoByOrderNo/";@RequestMapping("/v2/selectOrderInfoById/{orderNo}")public Object selectOrderInfoById(@PathVariable("orderNo") String orderNo) {OrderInfo orderInfo = orderInfoMapper.selectOrderInfoById(orderNo);if (null == orderInfo) {return "根據orderNo:" + orderNo + "查詢沒有該訂單";}// 發起遠程Http調用ResponseEntity<ProductInfo> responseEntity = restTemplate.getForEntity(PRODUCT_URI + orderInfo.getProductNo(), ProductInfo.class);ProductInfo productInfo = responseEntity.getBody();if (productInfo == null) {return "沒有對應的商品";}OrderVo orderVo = new OrderVo();orderVo.setOrderNo(orderInfo.getOrderNo());orderVo.setUserName(orderInfo.getUserName());orderVo.setProductName(productInfo.getProductName());orderVo.setProductNum(orderInfo.getProductCount());return orderVo;}@RequestMapping("/getOrderAndPayInfoByOrderNo/{orderNo}")public Object getOrderAndPayInfoByOrderNo(@PathVariable("orderNo") String orderNo) {OrderInfo orderInfo = orderInfoMapper.selectOrderInfoById(orderNo);if (null == orderInfo) {return "根據orderNo:" + orderNo + "查詢沒有該訂單";}ResponseEntity<PayInfo> responseEntity = restTemplate.getForEntity(PAY_URI + orderInfo.getProductNo(), PayInfo.class);PayInfo payInfo = responseEntity.getBody();if (payInfo == null) {return "沒有對應的支付信息";}OrderAndPayVo orderAndPayVo = new OrderAndPayVo();orderAndPayVo.setOrderNo(orderNo);orderAndPayVo.setProductNo(orderInfo.getProductNo());orderAndPayVo.setProductCount(orderInfo.getProductCount());orderAndPayVo.setPayNo(payInfo.getPayNo());orderAndPayVo.setPayTime(payInfo.getPayTime());orderAndPayVo.setUserName(orderInfo.getUserName());return orderAndPayVo;}}

可以看到,實現了我們最開始的需求 。


配置實現細粒度配置 (推薦)

接著上面的工程,首先屏蔽掉 CustomRibbonConfig

然后再 artisan-cloud-customcfg-ribbon-order 子模塊的配置文件 application.yml

增加

#自定義Ribbon的細粒度配置 artisan-pay-center:ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRuleartisan-product-center:ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

然后啟動多個Pay和Product服務進行測試

結果如下:

訪問4次

訪問 10次


源碼

https://github.com/yangshangwei/SpringCloudAlibabMaster

總結

以上是生活随笔為你收集整理的Spring Cloud Alibaba - 08 Ribbon 两种方式实现细粒度自定义配置控制微服务的负载均衡策略的全部內容,希望文章能夠幫你解決所遇到的問題。

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