RU | EN | DE

1. Reactive Programming (Spring WebFlux)

📌 When to use:

  • very many concurrent connections (chat, streaming, IoT),
  • non-blocking input-output is required,
  • high-load API.

📄 Dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

📄 Controller:

@RestController
@RequestMapping("/api/reactive")
class ReactiveController {
    @GetMapping("/numbers")
    public Flux<Integer> numbers() {
        return Flux.range(1, 5).delayElements(Duration.ofSeconds(1));
    }
    @GetMapping("/user/{id}")
    public Mono<User> getUser(@PathVariable Long id) {
        return Mono.just(new User(id, "Vitaliy"));
    }
}
Mono<T> → one object (equivalent to Optional)
Flux<T> → stream of objects (equivalent to Stream)

2. Messaging (RabbitMQ / Kafka)

RabbitMQ (AMQP)

📄 Dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

📄 Config:

@Configuration
public class RabbitConfig {
    @Bean
    Queue queue() { return new Queue("demo-queue", false); }
}

📄 Producer:

@Service
@RequiredArgsConstructor
public class Producer {
    private final AmqpTemplate template;
    public void send(String msg) {
        template.convertAndSend("demo-queue", msg);
    }
}

📄 Consumer:

@Service
public class Consumer {
    @RabbitListener(queues = "demo-queue")
    public void receive(String msg) {
        System.out.println("Received: " + msg);
    }
}

Kafka

📄 Dependency:

<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

📄 Producer:

@Service
@RequiredArgsConstructor
public class KafkaProducer {
    private final KafkaTemplate<String, String> template;
    public void send(String msg) {
        template.send("demo-topic", msg);
    }
}

📄 Consumer:

@Service
public class KafkaConsumer {
    @KafkaListener(topics = "demo-topic", groupId = "group1")
    public void listen(String msg) {
        System.out.println("Received: " + msg);
    }
}

📌 Kafka → for high-throughput event systems. 📌 RabbitMQ → for classic queue interaction.

3. GraphQL

📌 Used for more flexible APIs instead of REST.

📄 Dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

📄 Schema (src/main/resources/graphql/schema.graphqls):

type Query {
  user(id: ID!): User
  users: [User]
}
type User {
  id: ID!
  name: String
}

📄 Controller:

@Controller
public class UserGraphQL {
    @QueryMapping
    public User user(@Argument Long id) {
        return new User(id, "Vitaliy");
    }
    @QueryMapping
    public List<User> users() {
        return List.of(new User(1L, "Ann"), new User(2L, "Bob"));
    }
}

📌 Tested via GraphiQL → http://localhost:8080/graphiql.

4. Microservices (Spring Cloud)

Components:

  • Spring Cloud Config → configurations,
  • Eureka → service discovery,
  • Spring Cloud Gateway → API gateway,
  • Resilience4j → Circuit Breaker,
  • Sleuth + Zipkin → distributed tracing.

Example: Eureka Server

📄 Dependency:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

📄 Launch:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {}

Client:

@EnableEurekaClient
@SpringBootApplication
public class DemoService {}

5. Keycloak / Auth0 (SSO)

📌 For single authentication (OAuth2 / OpenID Connect).

Keycloak Integration

📄 application.yml:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8080/realms/demo

📄 Controller:

@RestController
public class SecureController {
    @GetMapping("/secure")
    public String secure(Principal principal) {
        return "Hello, " + principal.getName();
    }
}

📌 Access is now → only with a JWT token from Keycloak.

6. Event-Driven + Saga Pattern

📌 For complex business transactions in microservices (e.g., order → payment → delivery). Used:

  • Kafka/RabbitMQ for events,
  • Orchestration (Camunda, Temporal),
  • or Choreography (each service reacts to events).

7. Best Practices

  • ✅ If high load is required → WebFlux.
  • ✅ If microservices → Spring Cloud + Eureka + Gateway.
  • ✅ For integration → Kafka (streams) or RabbitMQ (queues).
  • ✅ For a new generation API → GraphQL.
  • ✅ For security in enterprise → Keycloak / Auth0.
  • ✅ For monitoring distributed systems → Sleuth + Zipkin / Jaeger.