Eureka Wiki has a word on the effect that it may take up to 2 minutes for a service to be perceived by other services, but the document does not explain why there are 2 minutes. In fact, this is caused by a three-place cache + a delay.
first, the HTTP response is cached by the Eureka. ApplicationResource
you can see a row in the 109 row of the Controller class in Eureka
String payLoad = responseCache.get(cacheKey);
Invocation, the function of the method in which the code resides getApplication()
is to respond to the client's request for a service message:
// 从cache中拿响应数据if (payLoad != null) { logger.debug("Found: {}", appName); return Response.ok(payLoad).build();} else { logger.debug("Not Found: {}", appName); return Response.status(Status.NOT_FOUND).build();}
In the above code, the responseCache
reference is ResponseCache
a type, the type is an interface, its get()
method first will go to the cache to query the data, if not the generation of data return (that is, to actually query the registration list), and the cache is valid time is 30s. In other words, the client's response to Eureka is not necessarily instantaneous, and most of the time it just caches information.
second, the Eureka client has also made 30s caches of registered information that it has acquired . That is, the service will cache the result after the first query to the available service address through the Eureka client, and will not actually initiate an HTTP request to Eureka the next time it is called.
* * Again, the Load Balancer component Ribbon also has a 30s cache. **ribbon will get the list of services from the Eureka client mentioned above and then cache the results 30s.
Finally, if you are not using these components in the Spring cloud environment (Eureka, Ribbon), your service will not register with Eureka immediately after it is started, but will need to wait until the heartbeat request is first sent . The heartbeat request's send interval is also 30s. (Spring Cloud has made changes to this and will register as soon as the service starts)
These four 30 seconds are the reason why the official wiki has a maximum of 2 minutes to write service registrations.
Spring-eureka Source Interpretation----Why a service is perceived by other services for up to two minutes