The ribbon has seven load balancing strategies to choose from:
Strategy class | name | describe |
RandomRule | Stochastic strategy | Randomly select server |
RoundRobinRule | round-robin policy | Select server s in order (ribbon default policy) |
RetryRule | Retry strategy | During a configuration period, if the server is not selected successfully, try to select an available server all the time. |
BestAvailableRule | Minimum concurrency policy | Inspect the servers one by one. If the server circuit breaker is open, ignore it. Then select the server with the lowest concurrent link. |
AvailabilityFilteringRule | Available filtering policies | Filter out the servers that fail all the time and are marked as circuit tripped, and filter out the servers with high concurrent links (active connections exceed the configured threshold) |
ResponseTimeWeightedRule | Response time weighting strategy | According to the response time of the server, the longer the response time is, the lower the weight is, and the lower the probability of being selected is. The shorter the response time, the higher the weight, and the higher the probability of being selected. This strategy is very appropriate. It integrates various factors, such as network, disk, io, etc., which directly affect the response time. |
ZoneAvoidanceRule | Region weight strategy | Comprehensively judge the performance of the region where the server is located and the availability of the server, poll and select the server, judge whether the operation performance of an AWS Zone is available, and eliminate all servers in the unavailable Zone. |
If you want to create a global load policy, you only need to add a configuration class, or you can expand it by yourself and add logic, as follows:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; @Configuration public class RibbonConfiguration { @Bean public IRule ribbonRule() { return new RandomRule(); } }
If you want to set a specific policy for a service source, you can add @ RibbonClient annotation on the project startup class. Of course, the corresponding configuration code needs to be adjusted:
/** * Custom - Tag annotation */ public @interface AvoidScan { }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; /** * Ribbon Load policy configuration class. IClientConfig is a management configurator for clients, which is used with @ RibbonClient annotation. */ @Configuration @AvoidScan public class RibbonConfiguration { @Autowired private IClientConfig config; @Bean public IRule ribbonRule(IClientConfig config) { return new RandomRule(); } }
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import cn.springcloud.book.config.AvoidScan; import cn.springcloud.book.config.TestConfiguration; /** * Project startup */ @SpringBootApplication @EnableDiscoveryClient @RibbonClient(name = "client-a", configuration = RibbonConfiguration.class)//Indicates client-a The responsible strategy for service usage is RibbonConfiguration Of the configuration class. //@RibbonClients(value = { // @RibbonClient(name = "client-a", configuration = RibbonConfiguration.class), // @RibbonClient(name = "client-b", configuration = RibbonConfiguration.class) //})//This way@RibbonClient Similar, but this is a policy specification for multiple services. @ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {AvoidScan.class})})//When the project is started, it is not allowed to Spring Scanned@AvoidScan Annotated classes, //Because the load policy configured for special services is not global. If it is not excluded, an error will be reported at startup. public class RibbonLoadbalancerApplication { public static void main(String[] args) { SpringApplication.run(RibbonLoadbalancerApplication.class, args); } }
If you want to use the configuration file method to configure the responsible policy, the syntax is client name.ribbon. *, and client name is the name we give to the service, that is, the value set by spring.application.name. As follows:
client-a:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Using random strategy for client-a service