Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka

160 views

Published on

Spring Connect Charlotte
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Speakers: Rohini Rajaram & Mayuresh Krishna

Published in: Software
  • Be the first to comment

  • Be the first to like this

Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka

  1. 1. ➔ ➔ ➔ ➔ ➔ ➔ ➔ ➔ ➔
  2. 2. ➔ ➔ ➔ ➔ ➔ ➔ ➔ ➔ ➔
  3. 3. Events As Continuous Streams Business Transactions Future Of Data Stored As Events Reconstruct A Shopping Cart Shopping Cart Time Constrained State Store Future Of Data
  4. 4. Publish & Subscribe To Events Store Events Process & Analyze Events
  5. 5. Relational Data Store Stream Processing Capture Transactions Real Time Aggregations Publish Results
  6. 6. Properties config = new Properties(); config.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-starter-app"); config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "server-lb:9092"); config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass()); config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); KStreamBuilder builder = new KStreamBuilder(); KStream<String, String> kStream = builder.stream("input-topic-name"); // do stuff kStream.to("word-count-output"); KafkaStreams streams = new KafkaStreams(builder, config); streams.start(); // Do aggregation making the word as the key and count as the value // shutdown hook to correctly close the streams application Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  7. 7. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 P0 P1 P2 Writes Topic Logs
  8. 8. spring.cloud.stream.kafka.binder.brokers=<Broker LB> spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde=or g.apache.kafka.common.serialization.Serdes$StringSerde spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde =org.apache.kafka.common.serialization.Serdes$StringSerde spring.cloud.stream.bindings.input.destination=input-topic-name @StreamListener @SendTo (Binding.OUTPUT) public KStream<String, Integer> wordCountProcessing ( @Input (Binding.INPUT) KStream<String, String> wordStream) { }
  9. 9. Maven Dependency: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> Code: /** * Constructor with bindings to the output channel for the order topic * @param binding */ public OrderService(ResourceBinding binding) { this.orderOut = binding.orderOut(); } public String createOrder(OrderDTO orderDTO) { orderOut.send(MessageBuilder.withPayload(order).setHeader("kafka_message Key", order.getOrderID()).build());
  10. 10. Topic A Partition 0 Topic A Partition 0 Producer Consumer
  11. 11. lineItemStream = orderEventStream .filter((key, value) -> value.getState().equals(State.placed)) .flatMapValues(value -> value.getLineItems()) .selectKey((orderId, lineItem) -> lineItem.getSku().toString()) .join(inventoryStream, new KeyValueMapper<String, LineItem, String>() { @Override public String apply(String key, LineItem value) { log.info("Sku Key " + key.toString()); return key; } }, new ValueJoiner<LineItem, Inventory, LineItem>() { @Override public LineItem apply(LineItem value1, Inventory value2) { if (value2.getQuantity() > 0) { value1.setState(LineItemState.validated); } else { value1.setState(LineItemState.insufficient_inventory); } return value1; } });
  12. 12. Name Account Balance Alice $100 Bob $200 Domain Event Alice gives Bob $50 Bob give Alice $100 Name Account Balance Alice $150 Bob $150 Change Log Event Stream Alice’s Acct. Balance is $50 Bob’s Acct. Balance is $250 Alice’s Acct. Balance is $150 Bob’s Acct Balance is $150 Current State
  13. 13. https://spring.io/guides/gs/spring-cloud-stream/ https://www.confluent.io/designing-event-driven-systems https://www.youtube.com/watch?v=P0SEOL_AGro&t=493s

×