-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated versions, replaced Spring Cloud Streams by Spring Kafka, remo…
…ved C7 from Kafka example (related to #73)
- Loading branch information
1 parent
71d1c67
commit 24c0470
Showing
69 changed files
with
573 additions
and
2,704 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 50 additions & 34 deletions
84
kafka/java/checkout/src/main/java/io/flowing/retail/checkout/messages/MessageSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
package io.flowing.retail.checkout.messages; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.stream.annotation.EnableBinding; | ||
import org.springframework.cloud.stream.messaging.Source; | ||
import org.springframework.integration.support.MessageBuilder; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
@Component | ||
@EnableBinding(Source.class) | ||
public class MessageSender { | ||
|
||
@Autowired | ||
private MessageChannel output; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
public void send(Message<?> m) { | ||
try { | ||
// avoid too much magic and transform ourselves | ||
String jsonMessage = objectMapper.writeValueAsString(m); | ||
// wrap into a proper message for the transport (Kafka/Rabbit) and send it | ||
output.send( | ||
MessageBuilder.withPayload(jsonMessage).setHeader("type", m.getType()).build()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not tranform and send message due to: "+ e.getMessage(), e); | ||
} | ||
} | ||
} | ||
package io.flowing.retail.checkout.messages; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Helper to send messages, currently nailed to Kafka, but could also send via AMQP (e.g. RabbitMQ) or | ||
* any other transport easily | ||
*/ | ||
@Component | ||
public class MessageSender { | ||
|
||
@Value( "${flowing-retail.topic-name:flowing-retail}") | ||
public String topicName; | ||
|
||
@Autowired | ||
private KafkaTemplate<String, String> kafkaTemplate; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Bean | ||
public NewTopic autoCreateTopicOnStartupIfNotExistant() { | ||
return TopicBuilder.name(topicName).partitions(1).replicas(1).build(); | ||
} | ||
|
||
public void send(Message<?> m) { | ||
try { | ||
// avoid too much magic and transform ourselves | ||
String jsonMessage = objectMapper.writeValueAsString(m); | ||
|
||
// wrap into a proper message for Kafka including a header | ||
ProducerRecord<String, String> record = new ProducerRecord<String, String>("flowing-retail", jsonMessage); | ||
record.headers().add("type", m.getType().getBytes()); | ||
|
||
// and send it | ||
kafkaTemplate.send(record); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not transform and send message: "+ e.getMessage(), e); | ||
} | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
kafka/java/checkout/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
spring.cloud.stream.bindings.output.destination=flowing-retail | ||
spring.cloud.stream.bindings.output.content-type=application/json | ||
|
||
spring.cloud.stream.kafka.binder.zkNodes=localhost:2181 | ||
spring.cloud.stream.kafka.binder.brokers=localhost:29092 | ||
# Kafka | ||
flowing-retail.topic-name=flowing-retail | ||
spring.kafka.bootstrap-servers=localhost:29092 | ||
spring.kafka.consumer.auto-offset-reset=earliest | ||
|
||
server.port = 8091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 29 additions & 14 deletions
43
kafka/java/inventory/src/main/java/io/flowing/retail/inventory/messages/MessageSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
package io.flowing.retail.inventory.messages; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.stream.annotation.EnableBinding; | ||
import org.springframework.cloud.stream.messaging.Source; | ||
import org.springframework.integration.support.MessageBuilder; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* Helper to send messages, currently nailed to Kafka, but could also send via AMQP (e.g. RabbitMQ) or | ||
* any other transport easily | ||
*/ | ||
@Component | ||
@EnableBinding(Source.class) | ||
public class MessageSender { | ||
|
||
public static final String TOPIC_NAME = "flowing-retail"; | ||
|
||
@Autowired | ||
private MessageChannel output; | ||
|
||
private KafkaTemplate<String, String> kafkaTemplate; | ||
@Autowired | ||
private ObjectMapper objectMapper; | ||
private ObjectMapper objectMapper; | ||
|
||
@Bean | ||
public NewTopic autoCreateTopicOnStartupIfNotExistant() { | ||
return TopicBuilder.name(TOPIC_NAME).partitions(1).replicas(1).build(); | ||
} | ||
|
||
public void send(Message<?> m) { | ||
try { | ||
// avoid too much magic and transform ourselves | ||
String jsonMessage = objectMapper.writeValueAsString(m); | ||
// wrap into a proper message for the transport (Kafka/Rabbit) and send it | ||
output.send( | ||
MessageBuilder.withPayload(jsonMessage).setHeader("type", m.getType()).build()); | ||
|
||
// wrap into a proper message for Kafka including a header | ||
ProducerRecord<String, String> record = new ProducerRecord<String, String>("flowing-retail", jsonMessage); | ||
record.headers().add("type", m.getType().getBytes()); | ||
|
||
// and send it | ||
kafkaTemplate.send(record); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not tranform and send message due to: "+ e.getMessage(), e); | ||
throw new RuntimeException("Could not transform and send message: "+ e.getMessage(), e); | ||
} | ||
} | ||
} |
11 changes: 4 additions & 7 deletions
11
kafka/java/inventory/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
spring.cloud.stream.bindings.output.destination=flowing-retail | ||
spring.cloud.stream.bindings.output.content-type=application/json | ||
spring.cloud.stream.bindings.input.destination=flowing-retail | ||
spring.cloud.stream.bindings.input.content-type=application/json | ||
spring.cloud.stream.bindings.input.group=inventory | ||
spring.application.name=inventory | ||
|
||
spring.cloud.stream.kafka.binder.zkNodes=localhost:2181 | ||
spring.cloud.stream.kafka.binder.brokers=localhost:29092 | ||
# Kafka | ||
spring.kafka.bootstrap-servers=localhost:29092 | ||
spring.kafka.consumer.auto-offset-reset=earliest |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.