Learn more about Spring Cloud Ribbon configuration

Regular placement

1. Disable Eureka.
After we add @LoadBalanced annotation to RestTemplate, we can use the service name to call the interface. When there are multiple services, load balancing can also be done.

This is because the service information in Eureka has been pulled locally on the client. If we don't want to integrate with Eureka, we can disable it through the following configuration method.

# 禁用 Eureka
ribbon.eureka.enabled=false

When we disable Eureka, the service name cannot be used to call the interface, and the service address must be specified.

2. Configure the interface address list. As
we mentioned above, Eureka can be disabled. After disabling, you need to manually configure the calling service address. The configuration is as follows:

# 禁用 Eureka 后手动配置服务地址
ribbon-config-demo.ribbon.listOfServers=localhost:8081,localhost:8083

This configuration is for specific services. The prefix is ​​the service name. After the configuration, you can use the service name to call the interface as before.

3. Configure the load balancing strategy
Ribbon's default strategy is polling. As can be seen from the output of the example we explained earlier, Ribbon provides many strategies, which will be explained later. Through configuration, we can specify which strategy the service uses to perform load operations.
4**. Timeout time**
There are two time-related settings in the Ribbon, which are the timeout time for request connection and the timeout time for request processing. The setting rules are as follows:

# 请求连接的超时时间
ribbon.ConnectTimeout=2000
# 请求处理的超时时间
ribbon.ReadTimeout=5000

也可以为每个Ribbon客户端设置不同的超时时间, 通过服务名称进行指定:
ribbon-config-demo.ribbon.ConnectTimeout=2000
ribbon-config-demo.ribbon.ReadTimeout=5000

5. Concurrency parameters

# 最大连接数
ribbon.MaxTotalConnections=500
# 每个host最大连接数
ribbon.MaxConnectionsPerHost=500

Code configuration Ribbon

The easiest way to configure Ribbon is through configuration files. Of course, we can also configure it by way of code.

To configure the previously customized load strategy through code, you first need to create a configuration class and initialize the customized strategy. The code is shown below.

@Configuration
public class BeanConfiguration {
  @Bean
  public MyRule rule() {
    return new MyRule();
  }
}

Create a Ribbon client configuration class, associate BeanConfiguration, use name to specify the name of the called service, the code is shown below.

@RibbonClient(name = "ribbon-config-demo", configuration = BeanConfiguration.class)
public class RibbonClientConfig {
}

You can remove the policy configuration in the previous configuration file, then restart the service, and access the interface to see the same effect as before.

Configure Ribbon in configuration file mode

In addition to using code to configure the Ribbon, we can also specify the corresponding configuration for the Ribbon through the configuration file:

<clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer(负载均衡器操作接口)
<clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule(负载均衡算法)
<clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing(服务可用性检查)
<clientName>.ribbon.NIWSServerListClassName: Should implement ServerList(服务列表获取)
<clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerList­Filter(服务列表的过滤)

Retry mechanism

In a cluster environment, multiple nodes are used to provide services, and it is inevitable that one node will fail. When using Nginx for load balancing, if your application is stateless and can be released on a rolling basis, you need to restart the application one by one, so the impact on users is actually relatively small, because Nginx fails to forward the request The request will be forwarded to another instance again.

Since Eureka is built based on the AP principle, at the expense of data consistency, each Eureka service will save the registered service information. When the heartbeat between the registered client and Eureka cannot be maintained, it may be due to the network or the service. Hung up.

In this case, Eureka will also save the registration information for a period of time. At this time, the client may get the service information that has hung up, so Ribbon may get the service information that has expired, which will cause a failed request.

We can use the retry mechanism to avoid this kind of problem. The retry mechanism is to request another service again when Ribbon finds that the requested service is unreachable.

**1. RetryRule 重试**
解决上述问题,最简单的方法就是利用 Ribbon 自带的重试策略进行重试,此时只需要指定某个服务的负载策略为重试策略即可:

ribbon-config-demo.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RetryRule

**2. Spring Retry 重试**
除了使用 Ribbon 自带的重试策略,我们还可以通过集成 Spring Retry 来进行重试操作。

在 pom.xml 中添加 Spring Retry 的依赖,代码如下所示。
org.springframework.retry spring-retry ``` Configure information such as the number of retries: ``` # The number of retries for the current instance ribbon.maxAutoRetries=1 # The number of retries for switching the instance ribbon.maxAutoRetriesNextServer=3 # For all operations Retry the request ribbon.okToRetryOnAllOperations=true # Retry the Http response code ribbon.retryableStatusCodes=500,404,502 ```


The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/114418542