Recommended reading:

SpringCloud source code read 0-SpringCloud essential knowledge

SpringCloud source code read 1-EurekaServer source code secrets

SpringCloud source code read 2-Eureka client secrets

Load balancing Provides the software load balancing algorithm of the client. Understanding the internal structure of a load balancing helps you understand other types of load balancing.

1. Core components

Load balancing components include:

  • Rule: indicates a load balancing policy
  • Ping: Heartbeat detection
  • ServerList: Indicates the service list
  • ServerListUpdater: Updates the service list
  • ServerListFilter: Filters the service list

Spring Cloud Ribbon is the encapsulation of Netflix Ribbon.

By default, Spring Cloud Ribbon provides the following implementations for Ribbon core components:

2. Customize the Ribbon client

For individual microservices, what if we want to replace individual components?

2.1 @ RibbonClient

1. Use @RibbonClient to specify which components of the requested services to be replaced.

@Configuration
@RibbonClient(name = "user", configuration = UserConfiguration.class)
public class UserRibbonConfiguration {}@Configuration
protected static class UserConfiguration{
    @Bean
    public IPing ribbonPing(a) {
        return newMyPingUrl(); }}Copy the code

Replace the IPing component in the Ribbon client of the service corresponding to the user with the MyPingUrl.

Note: Note that custom classes must be annotated with @Configuration and cannot be included in the package scanned by @ComponentScan, otherwise custom classes will be shared by all places annotated with @RibbonClient. If you use @ComponentScan (or @SpringBootApplication), you should take steps to prevent it from being included in the scope of the scan.

2.1 Attribute Configuration

From version 1.2.0, Spring Cloud and Netflix support custom Ribbon client configurations. The following properties are supported:

  • . Ribbon. NFLoadBalancerClassName: configuration ILoadBalancer implementation class
  • . Ribbon. NFLoadBalancerRuleClassName: configuration IRule implementation class
  • IPing. Ribbon. NFLoadBalancerPingClassName: configuration implementation class
  • . Ribbon. NIWSServerListClassName: configuration ServerList implementation class
  • . Ribbon. NIWSServerListFilterClassName: configuration ServerListFilter implementation class

The classes defined in these properties take precedence over the beans defined using @RibbonClient() and the default values provided by Spring Cloud Netflix

For example: application. Yml.

users:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
Copy the code

Replace the ServerList and LoadBalancerRule components corresponding to the Users service client

3. Customize all default ribbons

3.1 @ RibbonClients

When we wanted to replace a component of all clients, @RibbonClient was too weak.

The @RibbonClients annotation is used to override all client configurations.

@RibbonClients(defaultConfiguration = DefaultRibbonConfig.class)
public class RibbonClientDefaultConfigurationTestsConfig {

	public static class BazServiceList extends ConfigurationBasedServerList {

		public BazServiceList(IClientConfig config) {
			super.initWithNiwsConfig(config); }}}@Configuration
class DefaultRibbonConfig {

	@Bean
	public IRule ribbonRule(a) {
		return new BestAvailableRule();
	}

	@Bean
	public IPing ribbonPing(a) {
		return new PingUrl();
	}

	@Bean
	public ServerList<Server> ribbonServerList(IClientConfig config) {
		return new RibbonClientDefaultConfigurationTestsConfig.BazServiceList(config);
	}

	@Bean
	public ServerListSubsetFilter serverListFilter(a) {
		ServerListSubsetFilter filter = new ServerListSubsetFilter();
		returnfilter; }}Copy the code

4. Use with Eureka

When used with Eureka, some components are replaced:

  • (ribbonServerList) ConfigurationBasedServerList DiscoveryEnabledNIWSServerList replace the default. DiscoveryEnabledNIWSServerList consists of Eureka maintenance service management.
  • NIWSDiscoveryPing replaces the default (IPing) DummyPing. NIWSDiscoveryPing is also maintained by Eureka.
  • By default installation ServerList is a DomainExtractingServerList (DomainExtractingServerList here is an agent of coveryEnabledNIWSServerList). The purpose is to make physical metadata available to the load balancer without using AWS AMI metadata (which Netflix relies on)
  • Spring Cloud Ribbon default eureka instance metadata provided in the “zone” information build server list, by setting the eureka. Instance. Metadatamap. The value of the zone can achieve cross-regional instance configuration
  • If there is no configuration area, you can use the server host name of the domain names as a proxy for regional (premise is to set the mark approximateZoneFromHostname)
  • ServerListFilter: Service instance list filtering mechanism, the default with org.springframework.cloud.net flix. Ribbon. ZonePreferenceServerListFilter implementation, this strategy can filter out priority and request the caller in with regional service instance

If no other regional data source is available, guesses are made based on the client configuration (as opposed to the instance configuration). We will eureka. Client. AvailabilityZones (from regional name is mapped to a list), and the instance of the area of the first area (namely the eureka. Client. Region, its default for “us” – east – 1 for compatibility with the machine Netflix).

How to use it:

  1. RestTemplate Adds the @loadBalanced annotation to enable RestTemplate client load balancing
	@LoadBalanced
	@Bean
	public RestTemplate restTemplate(a){
		return new RestTemplate();
	}
Copy the code
  1. Use the Ribbon API directly to implement load balancing
public class MyClass {
    @Autowired
    private LoadBalancerClient loadBalancer;

    public void doStuff(a) {
        ServiceInstance instance = loadBalancer.choose("stores");
        URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
        // ... do something with the URI}}Copy the code

5. Remove from Eureka

When using the Ribbon alone, we can disable Eureka.

application.yml.

ribbon:
  eureka:
   enabled: false
Copy the code

6. Cache Ribbon configuration

The caching Ribbon configuration is actually an eagle-load mode. When the Ribbon first starts up, it needs to get a list of services from the registry. Generally the creation is slow, for this case. Starvation mode can be used to speed up the creation of client context.

application.yml

ribbon:
  eager-load:
    enabled: true
    clients: client1, client2, client3
Copy the code

Reference:

  • The official documentation
  • Chinese translation