{"id":3668,"date":"2025-09-22T10:00:45","date_gmt":"2025-09-22T14:00:45","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3668"},"modified":"2025-09-22T10:00:45","modified_gmt":"2025-09-22T14:00:45","slug":"monitoring-microservices-health-with-spring-discovery-client-and-actuator-2","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_discovery\/monitoring-microservices-health-with-spring-discovery-client-and-actuator-2\/","title":{"rendered":"Monitoring Microservices Health with Spring Discovery Client and Actuator"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>In the world of microservices, where applications are decomposed into smaller, independent services, maintaining visibility into the health of each service is crucial. Spring Boot provides a powerful combination of the Spring Discovery Client and Actuator to simplify this task. In this blog post, we\u2019ll walk through building a Spring service that leverages these tools to monitor the health status of your microservices.<\/p>\n<h3>Building the Health Status Service<\/h3>\n<p>Let\u2019s dive into the code for the <code>HealthStatusService<\/code>:<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.cloud.client.ServiceInstance;\nimport org.springframework.cloud.client.discovery.DiscoveryClient;\nimport org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.stereotype.Service;\nimport org.springframework.web.client.RestTemplate;\n\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\n\n@Service\npublic class HealthStatusService {\n\n    @Autowired\n    private DiscoveryClient discoveryClient;\n\n    @Autowired\n    private RestTemplate restTemplate;\n\n    public Map&lt;String, Map&lt;String, String&gt;&gt; getHealthStatus() {\n        Map&lt;String, Map&lt;String, String&gt;&gt; result = new HashMap&lt;&gt;();\n\n        List&lt;String&gt; services = discoveryClient.getServices();\n        for (String serviceId : services) {\n            List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(serviceId);\n            for (ServiceInstance instance : instances) {\n                String url = instance.getUri().toString() + &quot;\/actuator\/health&quot;;\n                ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(url, String.class);\n\n                String status = (response.getStatusCode() == HttpStatus.OK) ? &quot;UP&quot; : &quot;DOWN&quot;;\n\n                Map&lt;String, String&gt; instanceStatus = new HashMap&lt;&gt;();\n                instanceStatus.put(instance.getInstanceId(), status);\n\n                result.computeIfAbsent(serviceId, k -&gt; new HashMap&lt;&gt;()).putAll(instanceStatus);\n            }\n        }\n\n        return result;\n    }\n}\n<\/code><\/pre>\n<h3>Project Setup<\/h3>\n<p><strong>Gradle:<\/strong><\/p>\n<pre><code class=\"language-gradle\">dependencies {\n    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' \n    implementation 'org.springframework.boot:spring-boot-starter-web'\n}\n<\/code><\/pre>\n<p><strong>Maven:<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;\n<\/code><\/pre>\n<p><strong>application.yml:<\/strong><\/p>\n<pre><code class=\"language-yaml\">spring:\n  application:\n    name: health-monitor-service \n\neureka:\n  client:\n    serviceUrl:\n      defaultZone: http:\/\/localhost:8761\/eureka\/ \n\nmanagement:\n  endpoints:\n    web:\n      exposure:\n        include: health \n<\/code><\/pre>\n<h3>Considerations<\/h3>\n<ul>\n<li><strong>Security:<\/strong> Exposing the Actuator endpoints, especially <code>\/health<\/code>, can have security implications. Consider using Spring Security to protect these endpoints.<\/li>\n<li><strong>Error Handling:<\/strong> Implement more robust error handling to gracefully manage scenarios like network failures or service unavailability.<\/li>\n<li><strong>Caching:<\/strong> Introduce caching to reduce the frequency of health checks and improve performance.<\/li>\n<li><strong>Custom Health Checks:<\/strong> Explore the possibility of adding custom health checks to your microservices to provide more detailed health information.<\/li>\n<li><strong>Monitoring and Alerting:<\/strong> Integrate your health monitoring service with a monitoring and alerting system to proactively identify and respond to issues.<\/li>\n<\/ul>\n<p>Monitoring the health of your microservices is essential for maintaining a resilient and reliable system. The Spring Discovery Client and Actuator offer a straightforward way to achieve this. By building a dedicated health monitoring service, you can gain valuable insights into the health of your microservices ecosystem.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[432],"tags":[69,319],"series":[],"class_list":["post-3668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_discovery","tag-java-2","tag-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/doctors-office-2610509_1280-jpg.avif","jetpack-related-posts":[{"id":3663,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_discovery\/monitoring-microservices-health-with-spring-discovery-client-and-actuator\/","url_meta":{"origin":3668,"position":0},"title":"Monitoring Microservices Health with Spring Discovery Client and Actuator","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"In the world of microservices, where applications are decomposed into smaller, independent services, maintaining visibility into the health of each service is crucial. Spring Boot provides a powerful combination of the Spring Discovery Client and Actuator to simplify this task. In this blog post, we\u2019ll walk through building a Spring\u2026","rel":"","context":"In &quot;Spring Discovery&quot;","block_context":{"text":"Spring Discovery","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_discovery\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/checklist-2077020_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/checklist-2077020_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/checklist-2077020_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/checklist-2077020_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/08\/checklist-2077020_1280-jpg.avif 3x"},"classes":[]},{"id":3444,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_discovery\/spring-boot-admin-server-with-spring-cloud-discovery\/","url_meta":{"origin":3668,"position":1},"title":"Spring Boot Admin Server with Spring Cloud Discovery","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Boot Admin Server is a powerful tool for monitoring and managing Spring Boot applications. It provides a centralized dashboard for viewing application health, metrics, and logs. Spring Cloud Discovery, on the other hand, enables service registration and discovery for microservices-based applications. By integrating Spring Boot Admin Server with Spring\u2026","rel":"","context":"In &quot;Spring Discovery&quot;","block_context":{"text":"Spring Discovery","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_discovery\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/manhattan-3866140_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/manhattan-3866140_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/manhattan-3866140_640.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3438,"url":"https:\/\/www.mymiller.name\/wordpress\/spring\/architecting-with-spring-and-spring-cloud\/","url_meta":{"origin":3668,"position":2},"title":"Architecting with Spring and Spring Cloud","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Building a Multi-Service Architecture with Spring 3.1.x and Spring Cloud: Unlocking the Power of Microservices In the ever-evolving landscape of software development, microservices have emerged as a powerful architectural paradigm, enabling organizations to build scalable, resilient, and agile applications. Spring, a widely adopted Java framework, provides a comprehensive suite of\u2026","rel":"","context":"In &quot;Spring&quot;","block_context":{"text":"Spring","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/field-5236879_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3701,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_discovery\/simplifying-microservices-communication-with-the-java-spring-discovery-client\/","url_meta":{"origin":3668,"position":3},"title":"Simplifying Microservices Communication with the Java Spring Discovery Client","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"In the world of microservices, services need to find and communicate with each other dynamically. This is where the Java Spring Discovery Client comes in, offering a streamlined way to interact with a service registry (like Eureka, Consul, or Zookeeper). Let\u2019s explore its core APIs and illustrate their usage with\u2026","rel":"","context":"In &quot;Spring Discovery&quot;","block_context":{"text":"Spring Discovery","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_discovery\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/compass-4713642_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/compass-4713642_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/compass-4713642_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/compass-4713642_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/compass-4713642_1280-jpg.avif 3x"},"classes":[]},{"id":3441,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_discovery\/spring-cloud-gateway-with-spring-cloud-discovery\/","url_meta":{"origin":3668,"position":4},"title":"Spring Cloud Gateway with Spring Cloud Discovery","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Cloud Gateway and Spring Cloud Discovery are powerful tools for building microservices architectures. Spring Cloud Gateway acts as an API gateway, routing requests to the appropriate microservices. Spring Cloud Discovery provides a registry for microservices, enabling dynamic service discovery and load balancing. In this comprehensive guide, we'll delve into\u2026","rel":"","context":"In &quot;Spring Discovery&quot;","block_context":{"text":"Spring Discovery","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_discovery\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/trees-2900064_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/trees-2900064_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/trees-2900064_640.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3830,"url":"https:\/\/www.mymiller.name\/wordpress\/spring-gateway\/scaling-the-gatekeeper-load-balancing-multiple-instances-of-spring-cloud-gateway\/","url_meta":{"origin":3668,"position":5},"title":"Scaling the Gatekeeper: Load Balancing Multiple Instances of Spring Cloud Gateway","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"As your microservices ecosystem grows and traffic increases, a single instance of Spring Cloud Gateway might become a bottleneck or a single point of failure. To ensure high availability, fault tolerance, and improved performance, you\u2019ll likely need to run multiple instances of your Spring Cloud Gateway and load balance traffic\u2026","rel":"","context":"In &quot;Spring Gateway&quot;","block_context":{"text":"Spring Gateway","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring-gateway\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/network-3152677_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/network-3152677_640.jpg?fit=640%2C427&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/network-3152677_640.jpg?fit=640%2C427&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3668"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3668\/revisions"}],"predecessor-version":[{"id":3670,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3668\/revisions\/3670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3669"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3668"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}