Preface
LoadBalancer is a future replacement for Ribbon
1. What is LoadBalancer?
What is LB load balancing?
- Simply put, it is to allocate user requests to multiple services equally, thereby achieving the system's HA (high availability)
- Common load balancing include software Nginx, LVS, hardware F5, etc.
What is the spring-cloud-starter-loadbalancer component
- Spring Cloud LoadBalancer is an open source, simple and easy to use provided by SpringCloud.Client Load Balancer, it is included in SpringCloud-commons to replace the previous Ribbon component with it.
- Compared with Ribbon, SpringCloud LoadBalancer can not only support RestTemplate, but also support WebClient (WeClient is a function provided in Spring Web Flux, which can implement responsive asynchronous requests)
Official website:/spring-cloud-commons/reference/spring-cloud-commons/
The difference between client load and server load:
- Nginx is server load balancing. All requests on the client will be handed over to nginx, and then nginx will forward the request, that is, load balancing is implemented by the server.
- Loadbalancer local load balancing. When calling the microservice interface, it will obtain the registration information service list in the registration center and then cache it to the JVM locally, thereby implementing RPC remote service call technology locally.
2. Use steps
1. Start consul
See:SpringCloud--consul service registration and discovery, configuration management, configuration persistence
2. Client joins dependencies
<!--loadbalancer--> <dependency> <groupId></groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
3. Called with the service name
public static final String PaymentSrv_URL = "http://cloud-payment-service";//The microservice name on the service registration center@GetMapping(value = "/consumer/pay/get/info") private String getInfoByConsul(){ return (PaymentSrv_URL + "/pay/get/info", ); }
When calling a service with restTemplate, after calling with the service name, the service with the same service name in the consultation will passloadbalancerLoad balancing is implemented so that each service with the same service name is accessed an average number of times.
3. Load balancing algorithm replacement
1. Default algorithm polling (all service enumeration calls under the same service name)
- restTemplate configuration:
@Configuration public class RestTemplateConfig { @Bean @LoadBalanced //Default load balancing when calling by service name. Add this annotation to support load balancing public RestTemplate restTemplate(){ return new RestTemplate(); } }
2. Replace the random algorithm (random calls to all services under the same service name)
- restTemplate configuration:
@Configuration @LoadBalancerClient( //The following value value must be the same as the name in consul, and must be the same value = "cloud-payment-service",configuration = ) public class RestTemplateConfig { @Bean @LoadBalanced //Use the @LoadBalanced annotation to give RestTemplate the ability to load balancing public RestTemplate restTemplate(){ return new RestTemplate(); } @Bean ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { String name = (LoadBalancerClientFactory.PROPERTY_NAME); return new RandomLoadBalancer((name, ), name); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.