Spring Cloud Spring Cloud Service Discovery & Registration 2 — Questions and Answers
Question 1: Which Spring Cloud project provides service discovery support using HashiCorp Consul?
- Spring Cloud Zookeeper
- Spring Cloud Consul (Correct answer)
- Spring Cloud Kubernetes
- Spring Cloud Netflix
Correct answer: Spring Cloud Consul
Spring Cloud Consul integrates with HashiCorp Consul for service discovery, distributed configuration, and health checking.
Question 2: What is the purpose of the `spring.application.name` property in a Spring Cloud microservice?
- Sets the JVM process name
- Acts as the service ID used for registration and discovery (Correct answer)
- Defines the HTTP server port
- Configures the log file name
Correct answer: Acts as the service ID used for registration and discovery
spring.application.name serves as the virtual hostname and service ID under which the application registers with the discovery server.
Question 3: Which interface must be implemented to create a custom Spring Cloud ServiceRegistry?
- org.springframework.cloud.client.serviceregistry.ServiceRegistry (Correct answer)
- org.springframework.cloud.discovery.DiscoveryClient
- org.springframework.cloud.client.ServiceInstance
- org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer
Correct answer: org.springframework.cloud.client.serviceregistry.ServiceRegistry
The ServiceRegistry interface in Spring Cloud provides the contract for registering and deregistering service instances with a registry.
Question 4: How does Spring Cloud Kubernetes perform service discovery within a Kubernetes cluster?
- It deploys its own Eureka server inside the cluster
- It queries the Kubernetes API server for Services and Endpoints (Correct answer)
- It uses DNS-SD records published by CoreDNS
- It reads environment variables injected by the kubelet
Correct answer: It queries the Kubernetes API server for Services and Endpoints
Spring Cloud Kubernetes uses the Kubernetes API server to discover services and their endpoints, enabling native Kubernetes-based service discovery.
Question 5: What does the `@LoadBalanced` annotation do when applied to a RestTemplate bean in Spring Cloud?
- Enables circuit breaking on all requests
- Intercepts requests to resolve logical service names via the discovery client (Correct answer)
- Adds retry logic on HTTP 5xx responses
- Enables connection pooling for HTTP connections
Correct answer: Intercepts requests to resolve logical service names via the discovery client
@LoadBalanced marks a RestTemplate so that a LoadBalancerInterceptor is applied, translating service names like http://my-service/api into real IP/port using the discovery client.
Question 6: Which Eureka configuration property controls how long the server waits before evicting an instance that has stopped sending heartbeats?
- eureka.instance.lease-expiration-duration-in-seconds (Correct answer)
- eureka.server.eviction-interval-timer-in-ms
- eureka.client.registry-fetch-interval-seconds
- eureka.instance.heartbeat-interval
Correct answer: eureka.instance.lease-expiration-duration-in-seconds
eureka.instance.lease-expiration-duration-in-seconds sets the time (default 90s) after which Eureka considers an instance expired if no heartbeat is received.
Which Spring Cloud project provides service discovery support using HashiCorp Consul?