Ribbon [load balancing strategy]

ribbon has 7 load balancing strategies to choose from:

< tr>

Strategy class Name Description
RandomRule RandomRule Random Strategy Random Select server
RoundRobinRule Polling strategy Select server in order (ribbon default strategy)
RetryRule Retry Strategy During a configuration period, when the server selection is unsuccessful, it will always try to select an available server
BestAvailableRule Minimum Concurrency Strategy Inspect the servers one by one, if the server circuit breaker is open, ignore it, and then select the server with the lowest concurrent link< /td>
AvailabilityFilteringRule Available filtering strategies Filter out servers that have failed and are marked as circuit tripped, and filter out those high-concurrency links Server (active connections exceed the configured threshold)
ResponseTimeWeightedRule Response Time Weighted Strategy According to the server’s response time allocation Weight, the longer the response time, the lower the weight, and the lower the probability of being selected. The shorter the response time, the higher the weight, and the higher the probability of being selected. This strategy is very appropriate. It combines various factors, such as: network, disk, io, etc., which directly affect the response time
ZoneAvoidanceRule Zone weighting strategy Comprehensively judge the performance of the region where the server is located, and the availability of the server, polling to select the server and judging whether the operating performance of an AWS Zone is available , Remove all servers in the unavailable zone

If you want to create a global load strategy, just add one The configuration class can also be extended and added 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 unique strategy for a service source, you can add the @RibbonClient annotation to the project startup class Of course, the corresponding configuration code also needs to be adjusted:

/**

* Custom-mark 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 strategy configuration class, IClientConfig is a management configurator for the client, 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 class
*/
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name
= "client-a", configuration = RibbonConfiguration.class)// indicates that the responsible strategy used for the client-a service is through the RibbonConfiguration configuration class.
//@RibbonClients(value = {
// @RibbonClient(name = "client-a", configuration = RibbonConfiguration.class),
// @RibbonClient(name = "client-b", configuration = RibbonConfiguration.class)
//})//This method is similar to @RibbonClient, but this is a policy specification for multiple services.
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {AvoidScan.class})})// means that when the project is started, Spring is not allowed to scan classes marked by @AvoidScan annotations,
//Because the configuration is a load strategy for special services, it’s not Globally, if you do not rule out, an error will be reported when you start.
public class RibbonLoadbalancerApplication {

public static void main(String[] args) {
SpringApplication.run(RibbonLoadbalancerApplication.
class, args);
}

}

If you want to use the configuration file, configure the responsible policy, the syntax is client name.ribbon. *, the 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 #Use random strategy for client-a service

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();
}
}

/**

* Custom-mark 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 strategy configuration class, IClientConfig is a management configurator for the client, 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 class
*/
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name
= "client-a", configuration = RibbonConfiguration.class)// indicates that the responsible strategy used for the client-a service is through the RibbonConfiguration configuration class.
//@RibbonClients(value = {
// @RibbonClient(name = "client-a", configuration = RibbonConfiguration.class),
// @RibbonClient(name = "client-b", configuration = RibbonConfiguration.class)
//})//This method is similar to @RibbonClient, but this is a policy specification for multiple services.
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {AvoidScan.class})})// means that when the project is started, Spring is not allowed to scan classes marked by @AvoidScan annotations,
//Because the configuration is a load strategy for special services, it’s not Globally, if you do not rule out, an error will be reported when you start.
public class RibbonLoadbalancerApplication {

public static void main(String[] args) {
SpringApplication.run(RibbonLoadbalancerApplication.
class, args);
}

}

client-a:

ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Use random strategy for client-a service

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2408 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.