In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about whether Ribbon can be replaced by spring-cloud-loadbalancer. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Background
Scan the circle in the morning to see the news of Spring Cloud Hoxton.M2 Released, casually posted to my knowledge planet, after a friend will come to the following questions. Take half a day to learn the source code of spring-cloud-loadbalancer and sort out the summary of this article.
Spring Cloud Hoxton.M2 is the first version to integrate a new loadbalancer implementation to replace Ribbon
Spring Cloud Hoxton.M2 is the first release containing both blocking and non-blocking load balancer client implementations as an alternative to Netflix Ribbon which has entered maintenance mode.
The Origin of spring-cloud-loadbalancer
In 2017, spring began to try to develop a new project, spring-cloud-loadbalancer to replace ribbon, which is hosted in the spring-cloud-incubator incubator (by the way, spring cloud alibaba and other top projects are mostly hatched from then on, representing the development direction of spring cloud).
After N months of non-maintenance, I thought that when spring abandoned the project, it suddenly marked the project as archived and migrated to spring-cloud-commons.
Release 2.2.0.M2 version
How to use
It can only be used here based on the latest version of hoxton.m2, so configure spring's proxy maven library
Org.springframework.cloud spring-cloud-dependencies Hoxton.M2 pom import
Join nacos-client, use version 2.1.0, and pay special attention to excluding ribbon dependencies, otherwise loadbalancer is invalid
Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-netflix-ribbon
Add loadbalancer pom coordinates
Org.springframework.cloud spring-cloud-loadbalancer
Configuration use or the same configuration as ribbon
@ Configurationpublic class LbConfiguration {@ Bean @ LoadBalanced RestTemplate restTemplate () {return new RestTemplate ();}} @ GetMapping ("/ demo") public String doOtherStuff () {return restTemplate.getForObject ("http://big-provider-server/demo", String.class);} Source Code parsing LoadBalancerClient implementation
The current version only provides the implementation of BlockingLoadBalancerClient. Please pay attention to the Chinese notes.
/ / remove only the core code Note public class BlockingLoadBalancerClient implements LoadBalancerClient {@ Override public T execute (String serviceId, LoadBalancerRequest request) throws IOException {/ / query the available instances ServiceInstance serviceInstance = choose (serviceId) based on the service name; return execute (serviceId, serviceInstance, request) } @ Override public ServiceInstance choose (String serviceId) {/ / get load balancing policy ReactiveLoadBalancer loadBalancer = loadBalancerClientFactory .getInstance (serviceId) / / execute the load balancing policy to get the instance Response loadBalancerResponse = Mono.from (loadBalancer.choose ()) .block (); return loadBalancerResponse.getServer ();}} loadBalancer load balancing policy implementation
Currently, there is only one RoundRobinLoadBalancer polling way to select server.
Public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {public Mono choose (Request request) {ServiceInstanceSupplier supplier = this.serviceInstanceSupplier.getIfAvailable (); return supplier.get () .collectList () .map (instances-> {if (instances.isEmpty ()) {log.warn ("No servers available for service:" + this.serviceId) Return new EmptyResponse ();} / / TODO: enforce order? Int pos = Math.abs (this.position.incrementAndGet ()); ServiceInstance instance = instances.get (pos% instances.size (); return new DefaultResponse (instance);});}} and ribbon compare default load balancer comparison
Ribbon provides 7 default load balancing strategies, which are often overridden. Generally speaking, we use ZoneAvoidanceRule composition to judge the performance of the region where server is located and the availability of server to choose server.
Richness in configuration
Currently, spring-cloud-loadbalancer only supports configuration of retry operations.
Ribbon supports timeout, lazy load handling, retry and integration of advanced properties with hystrix, etc.
After reading the above, do you have any further understanding of whether Ribbon can be replaced by spring-cloud-loadbalancer? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.