Add cloud-stream module: Spring Cloud Stream 5.1 on Boot 4.2 with Kafka and RabbitMQ binders
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5uEyg58znuchZ8C8i
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
e54cf840fd
commit
e2c42dc268
@@ -0,0 +1,12 @@
|
||||
package com.ankurm.cloudstream;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CloudStreamApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudStreamApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.ankurm.cloudstream;
|
||||
|
||||
/** What the function emits and the consumer receives. */
|
||||
public record Invoice(String orderId, String customerId, double total) {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.ankurm.cloudstream;
|
||||
|
||||
/** What the supplier emits and the function consumes. Serialized as JSON by Spring Cloud Stream. */
|
||||
public record Order(String orderId, String customerId, int quantity, double unitPrice) {
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.ankurm.cloudstream;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The whole application: three plain java.util.function beans. Nothing here imports a binder or a
|
||||
* broker client, which is what makes switching brokers a configuration change.
|
||||
*
|
||||
* <pre>
|
||||
* orderSource (Supplier) --> orders --> priceOrder (Function) --> invoices --> bill (Consumer)
|
||||
* </pre>
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class Pipeline {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Pipeline.class);
|
||||
private static final List<String> CUSTOMERS = List.of("alice", "bob", "carol", "dave");
|
||||
|
||||
/**
|
||||
* Polled by the framework (every second by default), one order per call. A supplier that returns
|
||||
* {@code null} sends nothing, which is how {@code demo.max-orders} makes the demos finite (0 = unbounded).
|
||||
*/
|
||||
@Bean
|
||||
public Supplier<Order> orderSource(@Value("${demo.max-orders:0}") int maxOrders) {
|
||||
AtomicInteger n = new AtomicInteger();
|
||||
return () -> {
|
||||
int i = n.incrementAndGet();
|
||||
if (maxOrders > 0 && i > maxOrders) {
|
||||
return null;
|
||||
}
|
||||
return new Order("o-" + i, CUSTOMERS.get(i % CUSTOMERS.size()), 1 + i % 3, 9.5);
|
||||
};
|
||||
}
|
||||
|
||||
/** Input and output are both typed; the framework converts JSON to and from the records. */
|
||||
@Bean
|
||||
public Function<Order, Invoice> priceOrder() {
|
||||
return order -> new Invoice(order.orderId(), order.customerId(), order.quantity() * order.unitPrice());
|
||||
}
|
||||
|
||||
/** A negative total is the "poison" in the dead-letter demos: this consumer throws on it. */
|
||||
@Bean
|
||||
public Consumer<Invoice> bill() {
|
||||
return invoice -> {
|
||||
log.info("bill attempt for {}", invoice.orderId());
|
||||
if (invoice.total() < 0) {
|
||||
throw new IllegalArgumentException("Refusing to bill " + invoice.orderId() + ": negative total");
|
||||
}
|
||||
log.info("billed {} for {}", invoice.orderId(), invoice.total());
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
spring:
|
||||
cloud:
|
||||
stream:
|
||||
default-binder: kafka
|
||||
bindings:
|
||||
priceOrder-in-0:
|
||||
consumer:
|
||||
concurrency: 3
|
||||
kafka:
|
||||
bindings:
|
||||
bill-in-0:
|
||||
consumer:
|
||||
enable-dlq: true
|
||||
dlq-name: invoices.billing.dlq
|
||||
@@ -0,0 +1,27 @@
|
||||
spring:
|
||||
cloud:
|
||||
stream:
|
||||
default-binder: rabbit
|
||||
bindings:
|
||||
priceOrder-out-0:
|
||||
producer:
|
||||
# Rabbit routes through an exchange: a message published before the group's queue exists is
|
||||
# dropped. required-groups makes the producer declare the consumer queue itself.
|
||||
required-groups: billing
|
||||
priceOrder-in-0:
|
||||
consumer:
|
||||
# Rabbit has no consumer groups across partitions: every instance owns exactly one partition queue.
|
||||
partitioned: true
|
||||
instance-count: 3
|
||||
instance-index: 0
|
||||
rabbit:
|
||||
bindings:
|
||||
priceOrder-out-0:
|
||||
producer:
|
||||
# The producer declares the consumer's queue for required-groups, so it must declare it with
|
||||
# the same arguments as the consumer does, or the broker answers PRECONDITION_FAILED.
|
||||
auto-bind-dlq: true
|
||||
bill-in-0:
|
||||
consumer:
|
||||
auto-bind-dlq: true
|
||||
republish-to-dlq: true
|
||||
@@ -0,0 +1,37 @@
|
||||
# Everything broker-neutral lives in this file. The broker-specific blocks live in the two
|
||||
# profile files next to it.
|
||||
spring:
|
||||
application.name: cloud-stream-demo
|
||||
main:
|
||||
# The Rabbit binder drags spring-boot-starter-webflux in transitively, which would turn this broker-only
|
||||
# worker into a reactive web server on port 8080 (see scripts/webflux-path.sh).
|
||||
web-application-type: none
|
||||
cloud:
|
||||
function:
|
||||
# The functions to bind, separated by semicolons. Left empty, nothing was bound to 'orders' (output/09).
|
||||
definition: orderSource;priceOrder;bill
|
||||
stream:
|
||||
poller:
|
||||
fixed-delay: 1000
|
||||
bindings:
|
||||
orderSource-out-0:
|
||||
destination: orders
|
||||
producer:
|
||||
partition-key-expression: payload.customerId
|
||||
partition-count: 3
|
||||
priceOrder-in-0:
|
||||
destination: orders
|
||||
group: pricing
|
||||
priceOrder-out-0:
|
||||
destination: invoices
|
||||
bill-in-0:
|
||||
destination: invoices
|
||||
group: billing
|
||||
consumer:
|
||||
max-attempts: 3
|
||||
back-off-initial-interval: 200
|
||||
back-off-multiplier: 1.0
|
||||
logging:
|
||||
level:
|
||||
root: WARN
|
||||
com.ankurm: INFO
|
||||
Reference in New Issue
Block a user