Ribbon provides a set of microservice load balancing solutions.
At present, the mainstream load balancing schemes in the industry can be divided into two categories:
- Centralized load balancing: that is, an independent load balancing facility (either hardware, such as F5, or software, such as nginx) is used between the consumer and the provider, and the facility is responsible for forwarding the access request to the provider through some policy;
- In process load balancing: integrate the load balancing logic into the consumer. The consumer knows which addresses are available from the service registry, and then selects an appropriate provider from these addresses.
Load balancing strategies are common:
Policy class | name | describe |
RandomRule | Random strategy | Randomly select server |
RoundRobinRule | round-robin policy | Select server in order (ribbon default policy) |
RetryRule | Retry policy | During a configuration period, if the selection of server is unsuccessful, you will always try to select an available server |
BestAvailableRule | Minimum concurrency policy | Check the servers one by one. If the server circuit breaker is open, ignore it, and 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 those servers with high concurrent links (active connections exceed the configured threshold) |
ResponseTimeWeightedRule | Response time weighted weight strategy | The weight is allocated according to the response time of the server. 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 and integrates various factors, such as network, disk, io, etc., which directly affect the response time |
ZoneAvoidanceRule | Regional weight strategy | Comprehensively judge the performance of the area 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 |
Case: (because the Ribbon stops and the load balancing in eureka is replaced by the loadbalancer, the following is the loadbalancer case)
config add configuration
package com.example.eurekaclient2.config; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer; import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.web.client.RestTemplate; @Configuration public class RibbonConfig { @Bean @LoadBalanced //Enable load balancing annotation public RestTemplate getRestTemplate(){ return new RestTemplate(); } /** * Change strategy to random */ @Bean public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){ String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); return new RandomLoadBalancer(loadBalancerClientFactory .getLazyProvider(name, ServiceInstanceListSupplier.class), name); } }
The startup class adds the reference @ LoadBalancerClients to modify the policy
@LoadBalancerClients(defaultConfiguration = {RibbonConfig.class}) @SpringBootApplication //If the registry related configuration is configured in the configuration file, the registry annotation (@ EnableEurekaClient) is enabled by default public class EurekaClient2Application { public static void main(String[] args) { SpringApplication.run(EurekaClient2Application.class, args); } }
controller
@RestController public class ClientConntroller { @Autowired private RestTemplate restTemplate; @RequestMapping("/postRest") private String postRest(){ Persion ps = new Persion(); ps.setUserName("Lu Dabao"); //Contains some information about the response ResponseEntity<Persion> pp = restTemplate.postForEntity("http://LUDB-CLIENT-1/postUser", ps, Persion.class); Persion p = pp.getBody(); return p.getUserName(); } }