Ribbon Load Balancing
The difference between Ribbon and Nginx
- Client Load Balancing Ribbbon:
- The Ribbon retrieves the registry information list from the Eureka registry server, caches it to the local server, and polls the local load balancing policy. That is, load balancing is implemented on clients.
- Server load balancing Nginx:
- Ngnix uniformly sends all requests from the client to Nginx, which implements load balancing request forwarding and belongs to server-side load balancing. That is, the request is forwarded by the Nginx server.
- Differences in application scenarios:
- Nginx is suitable for server-side load balancing :Tomcat,Jetty
- The Ribbon applies to local load balancing for RPC remote calls in microservices :Dubbo,SpringCloud
The Ribbon is a low-level implementation of load balancing
- Ribbon Load Balancing:
- The client obtains the registration information list from the Eureka registry. After obtaining the registration information list, the client caches it to the local PC and implements load balancing on the local PC. That is, load balancing is implemented by clients.
- Load balancing algorithm:
- The total number of requests for the interface is modulated by the number of servers to obtain the actual server subscript (starting from 0) to get the server to call the service
- Implementation:
- Obtain the remote call address of the server DiscoveryClient
List<ServiceInstance> instances=discoveryClient.getInstance("eureka_ticket"); Copy the code
- Use REST to send requests using remote calls
String result=restTemplate.getForObject(instanceUrl,String.class); Copy the code
Feign
- Two client-side invocation tools are supported in SpringCloud:
- The Rest (RestTemplate template)
- Feign
- Feign is a declarative Http client call tool, implemented by interface + annotation, easy to read.
@FeignClient(name=" service alias ") @FeignClient calls the service interface (name: service name) Annotate the @enableFeignClients annotation on the main class to enable Feign privilegesCopy the code
Api-service: indicates the interface of all services ticket-service: indicates the interface of a specific service ticket-serviceimpl: indicates the interface of a specific service ticket-serviceimpl: indicates the interface of a specific service ticket-serviceimpl: indicates the interface of a specific service. The implementation entity class and definition interface information for a particular service is stored in the interface package. The parameters in a particular service are preceded by @RequestParam("xx") so that the parameters are acceptedCopy the code
Feign Client timeout setting
Ribbon. ConnectTime =5000 ribbon.ReadTimeout=5000 ribbonCopy the code