WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

RESTful Web Services · Advanced · question 54 of 100

What is the role of the Service Registry in microservices architecture?

📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

In microservices architecture, services need to communicate with each other in order to function. The Service Registry pattern is used to manage service discovery and registration, allowing services to locate and communicate with each other. A Service Registry is essentially a database that stores the metadata of each service, including its network location, endpoints, version, and other information necessary for discovery.

The Service Registry pattern is particularly useful in a dynamic and distributed environment where services come and go frequently. When a service registers itself with the Service Registry, it is added to a list of available services that other services can query. This means that when a service needs to call another service, it can query the Service Registry to obtain the necessary information and then use that information to make a direct call to the service.

There are several tools available for implementing a Service Registry in a microservices architecture, including Eureka, ZooKeeper, and Consul. Eureka is a popular choice in the Java ecosystem and is part of the Netflix OSS suite of tools.

Here’s an example of how Eureka can be used to implement Service Registry in a microservices architecture:

    // Eureka server configuration
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
    // Service registration
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProductServiceApplication {
        
        public static void main(String[] args) {
            SpringApplication.run(ProductServiceApplication.class, args);
        }
        
        @Bean
        public CommandLineRunner registerInEureka(EurekaInstanceConfig instanceConfig,
        EurekaClient eurekaClient) {
            return args -> {
                instanceConfig.setInstanceId("product-service-" + UUID.randomUUID().toString());
                eurekaClient.registerInstance();
            };
        }
    }
    
    // Service discovery
    @SpringBootApplication
    @EnableDiscoveryClient
    public class OrderServiceApplication {
        
        @Autowired
        private RestTemplate restTemplate;
        
        public static void main(String[] args) {
            SpringApplication.run(OrderServiceApplication.class, args);
        }
        
        @GetMapping("/products/{productId}")
        public Product getProduct(@PathVariable String productId) {
            InstanceInfo productInstance = discoveryClient.getNextServerFromEureka("product-service", false);
            String productUrl = productInstance.getHomePageUrl() + "/products/" + productId;
            return restTemplate.getForObject(productUrl, Product.class);
        }
    }

In this example, the Eureka server is started as a separate application, and the ProductService and OrderService applications are registered with Eureka as clients. The ProductService application registers itself with Eureka on startup using the EurekaInstanceConfig and EurekaClient beans. The OrderService application uses the DiscoveryClient bean to query the Service Registry for the location of the ProductService instance and makes a REST call to it using a RestTemplate bean.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic RESTful Web Services interview — then scores it.
📞 Practice RESTful Web Services — free 15 min
📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

All 100 RESTful Web Services questions · All topics