Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the custom load balancing policy of SpringCloud-Ribbon?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what SpringCloud-Ribbon custom load balancing strategy is like, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

The load policy can be customized by implementing the IRule interface, and the main selection service logic is in the choose method. We only demonstrate how to customize the load policy here, so instead of writing the logic of selection, we go straight back to the first service in the list of services. The specific code is shown below.

Public class MyRule implements IRule {private ILoadBalancer lb;@Overridepublic Server choose (Object key) {List servers = lb.getAllServers (); for (Server server: servers) {System.out.println (server.getHostPort ());} return servers.get (0);} @ Overridepublic void setLoadBalancer (ILoadBalancer lb) {this.lb = lb;} @ Overridepublic ILoadBalancer getLoadBalancer () {return lb;}}

In Spring Cloud, a custom load policy can be used in a configured way, and ribbon-config-demo is the name of the service that is invoked.

Ribbon-config-demo.ribbon.NFLoadBalancerRuleClassName=net.biancheng.ribbon_eureka_demo.rule.MyRule

Restart the service and access the interface that invokes other services. You can see that the output information of the console already contains the service information output from our custom policy, and the first service is called each time. This matches our logic.

The above is what the custom load balancing strategy of SpringCloud-Ribbon is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report