Ribbon 如何检索服务的可用实例列表

How Ribbon retrieves the list of available instances of a service

我在 API 网关和 eureka 服务器上使用 ribbon 作为负载平衡器。当客户端请求到达我的 API 网关时,它是每次都查询服务注册表以获取服务的可用实例,还是 Ribbon 将可用实例存储到其缓存中?

Spring Cloud Ribbon 与发现客户端对话以获取有关给定服务的 运行 个实例的信息。发现客户端保留尤里卡注册的内存缓存,以加快查找速度。

您可以查看 Spring Cloud Netflix 文档以获取更多信息:

https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html

The Eureka server does not have a back end store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). Clients also have an in-memory cache of Eureka registrations (so they do not have to go to the registry for every request to a service).

此外,对于带 Eureka 的 Ribbon: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html#_using_ribbon_with_eureka

When Eureka is used in conjunction with Ribbon (that is, both are on the classpath), the ribbonServerList is overridden with an extension of DiscoveryEnabledNIWSServerList, which populates the list of servers from Eureka.

来自任何区域的客户端可以查找注册表信息(每 30 秒发生一次)以定位他们的服务(可以在任何区域中)并进行远程调用。更多信息,请访问Official Netflix Eureka Documentation

我知道 ribbon 为每个服务都有一个计划任务,该任务将从注册表中获取新的服务器列表。

你可以找这个:ScheduledThreadPoolExecutor

任务到运行的默认间隔为30秒,您可以通过配置更改它:

your-service-name:
  ribbon:
    ServerListRefreshInterval: 200(the time you want to set in ms)