Microservices with Spring Cloud

David Kumar Jan 2026
3 tabs
package com.example.demo.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@FeignClient(name = "user-service", fallback = UserServiceFallback.class)
public interface UserServiceClient {

    @GetMapping("/api/users/{id}")
    User getUserById(@PathVariable("id") Long id);

    @GetMapping("/api/users")
    List<User> getAllUsers();
}

@Component
class UserServiceFallback implements UserServiceClient {

    @Override
    public User getUserById(Long id) {
        // Return default or cached data
        User fallbackUser = new User();
        fallbackUser.setId(id);
        fallbackUser.setName("Service Unavailable");
        return fallbackUser;
    }

    @Override
    public List<User> getAllUsers() {
        return Collections.emptyList();
    }
}
3 files · java, yaml Explain with highlit

Spring Cloud provides tools for building distributed microservices. I use Spring Cloud Netflix Eureka for service discovery—services register themselves and discover others dynamically. Ribbon enables client-side load balancing. Feign creates declarative REST clients with interface definitions. Spring Cloud Config externalizes configuration to a central server. Circuit breakers with Resilience4j prevent cascade failures. API Gateway routes requests and handles cross-cutting concerns like authentication. Distributed tracing with Sleuth and Zipkin correlates logs across services. Spring Cloud Stream connects services via message brokers. Configuration refresh updates properties without restart. These patterns address microservices challenges—service location, resilience, distributed configuration, and observability—enabling scalable, fault-tolerant systems.