A hub to let each services to find each other

  1. Create another Spring Project: service-registry

  2. Dependencies: spring-cloud-starter-netflix-eureka-server

  3. Main class enable eureka service

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
  4. Setup application.yml

    server:
      port: 8761
    
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    
  5. Register EACH services with Eureka, for each services:

    1. Add dependency : spring-cloud-starter-netflix-eureka-client
    2. Add eureka in application.yml
    spring:
      application:
        name: user-service
      
      # to let the service know Config Server 
      cloud:
        config:
          uri: <http://localhost:8888>
    
    eureka:
      client:
        service-url:
          defaultZone: <http://localhost:8761/eureka>
    
  6. Now when the service start, will registers itself with Eureka. (can check in eureka port)

  7. Create service config in service that want to call other services

    @Bean
    @LoadBalanced
    public WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }
    
  8. When other service need another service, call the service name

    @Service
    public class OrderService {
    
        private final WebClient webClient;
    
        public Mono<UserDto> getUser(String userId) {
            return webClient
                    .get()
                    **.uri("<http://user-service/users/{id}>", userId)**
                    .retrieve()
                    .bodyToMono(UserDto.class);
        }
    }