RU | EN | DE

1. Reaktive Programmierung (Spring WebFlux)

📌 Wann verwenden:

  • sehr viele gleichzeitige Verbindungen (Chat, Streaming, IoT),
  • asynchrone Ein- und Ausgabe erforderlich,
  • API mit hoher Last.

📄 Abhängigkeit:

<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> → ein Objekt (Analogon zu Optional)
Flux<T> → ein Fluss von Objekten (Analogon zu Stream)

2. Messaging (RabbitMQ / Kafka)

RabbitMQ (AMQP)

📄 Abhängigkeit:

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

📄 Konfiguration:

@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 📄 Abhängigkeit:

<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 → für hochdurchsatzfähige eventbasierte Systeme. 📌 RabbitMQ → für klassische Nachrichtenwarteschlangeninteraktion.

3. GraphQL

📌 Wird für flexiblere APIs anstelle von REST verwendet.

📄 Abhängigkeit:

<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"));
  }
}

📌 Testen über GraphiQL → http://localhost:8080/graphiql.

4. Microservices (Spring Cloud)

Komponenten:

  • Spring Cloud Config → Konfigurationen,
  • Eureka → Service-Discovery,
  • Spring Cloud Gateway → API-Gateway,
  • Resilience4j → Circuit Breaker,
  • Sleuth + Zipkin → verteilte Tracing.

Beispiel: Eureka Server

📄 Abhängigkeit:

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

📄 Start:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {}

Client:

@EnableEurekaClient
@SpringBootApplication
public class DemoService {}

5. Keycloak / Auth0 (SSO)

📌 Für zentrale Authentifizierung (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();
  }
}

📌 Jetzt Zugriff → nur mit JWT-Token von Keycloak.

6. Event-Driven + Saga Pattern

📌 Für komplexe Geschäftsabläufe in Microservices (z.B. Bestellung → Zahlung → Versand). Werden verwendet:

  • Kafka/RabbitMQ für Events,
  • Orchestration (Camunda, Temporal),
  • oder Choreography (jeder Service reagiert auf Events).

7. Best Practices

  • ✅ Wenn hohe Last erforderlich ist → WebFlux.
  • ✅ Wenn Microservices → Spring Cloud + Eureka + Gateway.
  • ✅ Für Integration → Kafka (Streams) oder RabbitMQ (Warteschlangen).
  • ✅ Für API der neuen Generation → GraphQL.
  • ✅ Für Sicherheit im Enterprise → Keycloak**/Auth****0**.
  • ✅ Für Monitoring verteilter Systeme → Sleuth + Zipkin**/Jaeger.