DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

Day 1 of microservices

To keep myself updated with current technologies, I have started learning Microservices architecture, transitioning from my monolithic background. After getting a solid understanding of the basics, I stepped into my first major concept today: Service Discovery.

I learned this by following a tutorial from the Coder Ulagam channel. Here is a quick breakdown of what I covered:

Service Registration: How individual microservices register their details with a central server.
Service Discovery: How client services find other registered services dynamically without hardcoding IP addresses or ports.
Annotations: I learned exactly when and where to use

@EnableEurekaServer (for the registry application) and @EnableDiscoveryClient (for the microservices connecting to it).

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistryApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceRegistryApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

application.properties

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false 


Enter fullscreen mode Exit fullscreen mode

Product Controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @GetMapping("/products")
    public String getProducts() {
        return "List of Mobile Phones and Laptops"; 
    }
}
Enter fullscreen mode Exit fullscreen mode

application.properties

server.port=8081
spring.application.name=products-microservice
Enter fullscreen mode Exit fullscreen mode

Home Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HomeController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/home")
    public String getHomeDetails() {

        String productsResponse = restTemplate.getForObject("http://products-microservice/products", String.class);
        return "Welcome to Home Page! Here are the products: " + productsResponse;
    }
}
Enter fullscreen mode Exit fullscreen mode

application.properties

server.port=8080
spring.application.name=home-microservice
Enter fullscreen mode Exit fullscreen mode

Config Class


import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Enter fullscreen mode Exit fullscreen mode

Step by Step Flow:

  1. Starting the Registry: First, the Eureka Server starts running.
  2. Service Registration: When the Products Microservice and the Home Microservice start up, they automatically contact the Eureka Server. They register their details, such as their application names and the ports they are running on.
  3. Receiving a Request: A user makes a request to the Home Microservice by accessing the /home endpoint.
  4. Service Discovery: To complete this request, the Home Microservice needs data from the Products Microservice. Instead of using a hardcoded port number, the RestTemplate asks Eureka, "Where is 'products-microservice' located?"
  5. Fetching the Data: Eureka resolves the name and provides the correct port. The @LoadBalanced RestTemplate automatically routes the call to the Products Microservice.
  6. Returning the Output: The Products Microservice returns its data back to the Home Microservice. The Home Microservice combines this with its own text and sends the final output back to the user.

Top comments (0)