Ribbon service call / load balancing and request retry configuration

Ribbon Load Balancing

After understanding of Eureka, the Eureka clusters and build, has been basically getting started in Eureka. Before we get after the registration lists for direct service to the caller to get the first registration information by get (0) manner. And when we also set up a service provider after the cluster. This approach is not desirable. So how to choose a suitable provider to provide services?

First, rule out the option of our own hard-coded way.

Zookeeper before contact with friends should not be unfamiliar word for load balancing, and Ribbon is another kind of load balancing program, and Eureka same development as the company NetFlix, and the integration of Ribbon in Eureka client. Generally with use.

The main role of the Ribbon

Service call

Service call basis Ribbon achieve, it is to make up a list of all services (+ name service request path) by pulling the mapping relationship, with RestTemplate implementation calls.

note:

When using the Ribbon service call, the name of the application can only be used - underlined the connection , you can not use an underscore. Otherwise the service will not be recognized.

  1. While injecting RestTemplate plus @LoadBalanced comment
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}
  1. When the service call, do not go get url request service from the list manually, direct replacement of the service name
@GetMapping("teacher/users")
public List getAllUser(){
return restTemplate.getForObject("http://SERVICE-PROVIDER/api/v1/users", List.class);
}

Load Balancing

The built-in load balancing algorithm, when there are multiple service providers, selecting an appropriate one. Ribbon provides load balancing algorithm are:

img

May be directly modified by the configuration:

# 可以通过 服务名:ribbon:NFLoadBalancerRuleClassName: 对应的策略全类名
SERVICE-PROVIDER:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule

Retry mechanism

In addition to service calls and load balancing, Ribbon also provides family retry when the interface is allowed to call. Use as follows:

  1. Introducing retry coordinates
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
  1. Configure the parameters in the configuration file
spring:
  cloud:
    loadbalancer:
      retry:
        enabled: true # 重试功能的开关 默认 true
SERVICE-PROVIDER:
  ribbon:
    ConnectTimeout: 250 # 与服务提供方建立Http连接的超时时间
    ReadTimeout: 1000 # 接收返回数据的超时时间
    OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
    MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
    MaxAutoRetries: 1 # 对当前实例的重试次数(包含第一次请求,即配置1相当于请求超时就切换)

If you follow the above configuration, when the consumer attempts to establish a direction connection provider failed to 250ms, it will move directly to the next party service tries to connect (autoRetries = MaxAutoRetries = 1). At this time, if it fails (autoRetriesNextServer = MaxAutoRetriesNextServer = 1), the request fails. Can be configured according to the actual business needs

Guess you like

Origin www.cnblogs.com/keatsCoder/p/12398650.html