How to add and modify Eureka Client Instance's Metadata Map
search cancel

How to add and modify Eureka Client Instance's Metadata Map

book

Article ID: 297095

calendar_today

Updated On:

Products

Support Only for Spring

Issue/Introduction

You have a microservice that is registering to an SCS service registry. You've written a service registry client by using @EnableDiscoveryClient. When using this method you noticed that there is a default metadata map.

What if you need to put a custom metadata map for other microservices to be able to distinguish a specific microservice?

This article will provide instructions on how to add or modify a service registry client's metadata. We will be using the sample greeter app as an example.

Resolution

You can add entries to a metadataMap in a Eureka client. Bound it to the SCS Eureka server by adding them in your Spring configuration

Following the greeter app sample app, you can add the following line to greeter-messages's  application.yml and that will add a value-pair to the metadata:
eureka:
  instance:
    metadata-map:
      author: my-dev-team
      department : our-dept


View the registered app details from Eureka Service using the /eureka/apps endpoint:

<applications>
  <versions__delta>1</versions__delta>
  <apps__hashcode>UP_1_</apps__hashcode>
  <application>
    <name>greeter-message</name>
    <instance>
      <instanceId>eureka-client.apps.smoke.springapps.io:58e84ed7-714e-4d83-4c08-9288</instanceId>
      <hostName>eureka-client.apps.smoke.springapps.io</hostName>
      <app>greeter-messages</app>
      ...
      </dataCenterInfo>
      ...
      <metadata>
        <author>my-dev-team</author>
        <department>our-dept</department>
      </metadata>
   </application>
</applications>


One thing to note when attempting to add metadata to a Eureka server is that the metadata may take a moment to appear in the Eureka server if the client was already registered prior to the metadata being added. If you had an already running client with no metadata then add a metadata and restart the client; it may take about 1.5 minutes for the metadata to appear in Eureka Server. This is expected, as Eureka tends to cache things and also will wait for heartbeat to refresh cache.

Another useful scenario of specifying a custom metadata map is for customizing your spring.application.name. You can combine this to reference CF env variables. Below is a sample config:

eureka:
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id}:${vcap.application.application_version}:${vcap.application.application_uris}:${CF_INSTANCE_IP}:${CF_INSTANCE_PORT}
    metadata-map:
      appContainerIp: ${CF_INSTANCE_IP}
      appContainerPort: ${CF_INSTANCE_PORT}