In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "introduction to the composition of SpringCloud Ribbon and the principle of load balancing". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Ribbon Hunger loading
By default, Ribbon is lazily loaded. When the service is started, the first request is very slow, and after the second, it is much faster.
Solution: enable hunger loading ribbon: eager-load: enabled: true # enable hunger loading clients: server-1,server-2,server-3 # enable hunger loading for which service names are enabled, multiple Ribbon are separated by commas to form the API function default value IclientConfig reads and configures DefaultClientConfigImplIRule load balancing rules, select instance ZoneAvoidanceRuleIPing to filter out instance DumyPing that cannot be connected with ping (this class does nothing and thinks that each instance is available All can ping) the list of instances given by ServerList to Ribbon Ribbon:ConfigurationBasedServerList
Spring Cloud Alibaba:NacosServerListServerListFilter filters out the entry ZoneAwareLoadBalancerServerListUpdater of the ineligible instance ZonePreferenceServerListFilterILoadBalancerRibbon to update the policy PollingServerListUpdater given to the List of Ribbon
Each of these items can be customized by IclientConfigRibbon to support very flexible configuration, that is, the IRule provided by this component provides rules for Ribbon to select an instance, and this component is the core component, for example: code approach
@ Configurationpublic class RibbonRuleConfig {@ Bean public IRule ribbonRulr () {return new RandomRule ();} @ Bean public IPing iPing () {return new PingUrl ();}}
Configure attribute mode
: ribbon: NFLoadBalancerClassName: # ILoadBalancer the interface implementation class NFLoadBalancerRuleClassName: # IRule the interface implementation class NFLoadBalancerPingClassName: # Iping the interface implementation class NIWSServerListClassName: # ServerList the interface implementation class NIWSServerListFilterClassName: # ServiceListFilter the interface implementation class
The classes defined in these properties take precedence over the bean defined using @ RibbonClient (configuration=RibbonConfig.class) Spring and the default values provided by Spring Cloud Netflix. Description: ribbon precedence code definition defined in configuration file
There are eight algorithms for Ribbon load balancing, among which ~ ResponseTimeWeightedRule~~ has abolished the rule name feature. AvailabilityFilteringRule filters out the back-end Service marked circuit tripped (circuit trip) that has been failed all the time, and filters out those highly concurrent back-end Server or uses an AvailabilityPredicate to include the logic of filtering Server, which is actually checking the running status of each Server recorded by status. BestAvailableRule selects a minimum concurrent request Server, and examines Server one by one, if Server is tripped. Then skip RandomRule and randomly select a ServerResponseTimeWeightedRule that has been discarded, which acts like WeightedResponseTimeRuleRetryRule on the selected balance policy machine recharge mechanism. If the selection of Server is not successful within a configuration period, you have been trying to use subRule to select an available ServerRoundRobinRule polling option, poll index, and select the corresponding location ServerWeightedResponseTimeRule of index. Weighted according to the corresponding time, the longer the corresponding time, the smaller the weight. The less likely it is to be selected. ZoneAvoidanceRule (default is this) is responsible for determining the performance of Server Zone and the availability of Server. Select Server, which is similar to polling (RoundRobinRule) to implement load balancing configuration-random mode 1: JAVA code mode in an environment without Zone.
First define RestTemplate and add the annotation @ LoadBalanced, so that RestTemplate achieves load balancing
@ LoadBalanced@Beanpublic RestTemplate restTemplate () {/ / template.getMessageConverters () .set (1, new StringHttpMessageConverter (StandardCharsets.UTF_8)); / / resolve Chinese garbled return new RestTemplate ();}
Add a configuration class under the SpringBootApplication main class. This class is mainly used for which service to do load balancing. The default is rotation training.
@ Configuration@RibbonClient (name = "${Service name}", configuration = GoodsRibbonRuleConfig.class) / / configuration: points to the configuration class public class GoodsRibbonConfig {} of the load balancing rule
Add a configuration class for Ribbon, and note that this class must be configured under a package other than the @ SpringBootApplication main class. Otherwise, all services will be implemented according to this rule. Will be shared by all RibbonClient. The main reason is that the main context of the main class conflicts with the subcontext of Ribbon. Parent-child contexts cannot overlap. Related connection: https://blog.csdn.net/qq_32588349/article/details/52097943
@ Configurationpublic class GoodsRibbonRuleConfig {@ Bean public IRule ribbonRulr () {return new RandomRule ();}}
Or use custom annotations to exclude the class
Method 1: configure attribute method server-1: # Service name Service-ID ribbon: # attribute configuration method [recommended] NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # configuration file configure load balancing algorithm-I use a custom Ribbon load balancing algorithm here. Default
Priority: configuration (does not affect other services) > (greater than) hard coding (the class must be written outside the SpringBoot startup class package, otherwise it will affect other services)
Summary: there are some shortcomings in the way of configuration. Code configuration is based on code and is more flexible and tricky (parent-child context).
The online modification needs to be repackaged, and the release attribute configuration is easy to use and more intuitive.
Online changes do not need to be repackaged and released
In extreme scenarios with higher priority, there is no configuration method to flexibly implement load balancing configuration-random mode 1: the configuration class of Ribbon is defined under the main class.
Let ComponentScan contexts overlap (strongly deprecated)
Method 2: @ Configuration@RibbonClients (defaultConfiguration = GoodsRibbonRuleConfig.class) / / Ribbon load balancer global granularity configuration (all services are configured according to this configuration) public class RibbonConfig {} "introduction to the composition of SpringCloud Ribbon and the principle of load balancer" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.