eureka:
+ instance:
+ metadataMap:
+ hostname: ${vcap.application.uris[0]}
+ nonSecurePort: 80
+ instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
+From 1b0b3b25ab703c00cba56f0c7b9f4a0243f52bca Mon Sep 17 00:00:00 2001
From: Dave Syer
It’s worth spending a bit of time understanding how the Eureka metadata works, so you can use it in a way that makes sense in your platform. There is standard metadata for things like hostname, IP address, port numbers, status page and health check. These are published in the service registry and used by clients to contact the services in a straightforward way. Additional metadata can be added to the instance registration in the eureka.instance.metadataMap, and this will be accessible in the remote clients, but in general will not change the behaviour of the client, unless it is made aware of the meaning of the metadata. There are a couple of special cases described below where Spring Cloud already assigns meaning to the metadata map.
Cloudfoundry has a global router so that all instances of the same app have the same hostname (it’s the same in other PaaS solutions with a similar architecture). This isn’t necessarily a barrier to using Eureka, but if you use the router (recommended, or even mandatory depending on the way your platform was set up), you need to explicitly set the hostname and port numbers (secure or non-secure) so that they use the router. You might also want to use instance metadata so you can distinguish between the instances on the client (e.g. in a custom load balancer). For example:
+eureka:
+ instance:
+ metadataMap:
+ hostname: ${vcap.application.uris[0]}
+ nonSecurePort: 80
+ instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
+Depending on the way the security rules are set up in your Cloudfoundry instance, you might be able to register and use the IP address of the host VM for direct service-to-service calls. This feature is not (yet) available on Pivotal Web Services (PWS).
+By default a eureka instance is registered with an ID that is equal to its host name (i.e. only one service per host). Using Spring Cloud you can override this by providing a unique identifier in eureka.instance.metadataMap.instanceId. For example:
With this meatdata, and multiple service instances deployed on
+localhost, the random value will kick in there to make the instance
+unique. In Cloudfoundry the spring.application.instance_id will be
+populated automatically in a Spring Boot Actuator application, so the
+random value will not be needed.
Don’t use the DiscoveryClient in @PostConstruct method (or
-anywhere where the ApplicationContext might not be started yet). It
-is initialized in a SmartLifecycle (with phase=0) so the earliest
-you can rely on it being available is in another SmartLifecycle with
-higher phase.
Don’t use the DiscoveryClient in @PostConstruct method or in a
+@Scheduled method (or anywhere where the ApplicationContext might
+not be started yet). It is initialized in a SmartLifecycle (with
+phase=0) so the earliest you can rely on it being available is in
+another SmartLifecycle with higher phase.
When Eureka is used in conjunction with Ribbon the ribbonServerList
is overridden with an extension of DiscoveryEnabledNIWSServerList
@@ -3340,7 +3373,7 @@ ProxyAuthenticationProperties for full details.