java yaml 122 lines · 3 tabs

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.


Related snips

graphql
type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
    createdAt: String!

GraphQL API with Spring Boot

java graphql spring-boot
by David Kumar 3 tabs
java
package com.example.starter.config;

import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

Custom Spring Boot starters

java spring-boot starter
by David Kumar 4 tabs
java
package com.example.demo.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

Messaging with Apache Kafka

java kafka messaging
by David Kumar 3 tabs
java
package com.example.demo.controller;

import com.example.demo.dto.FileMetadata;
import com.example.demo.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;

File upload and download handling

java spring-boot file-upload
by David Kumar 2 tabs
sql
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(100) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

Database migration with Flyway

java flyway database-migration
by David Kumar 6 tabs
java
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

WebSocket for real-time communication

java websocket spring-boot
by David Kumar 3 tabs

Share this code

Here's the card — post it anywhere.

Microservices with Spring Cloud — share card
Link copied