Check the example here: https://github.com/kgoralski/go-kotlin-java-kafka
Java - demo-java-source with Spring Cloud Stream - localhost:8080
Kotlin - demo-kotlin-sink with Spring Cloud Stream - localhost:8081 (Consumer)
Go aka Golang - demo-go-sink with Shopify/sarama (Kafka Client) - localhost:8082 (Consumer)
Apache Kafka as Message Broker
Go Shopify/sarama - Apache Kafka Client
Go HTTP/2 Push example (just for fun)
Gradle
Running Kafka inside Docker (spotify/kafka) to play with it locally :
docker run --name spotifykafka -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=localhost --env ADVERTISED_PORT=9092 spotify/kafka
to get Shopify/sarama lib - Kafka client:
go get github.com/Shopify/sarama
How to run it
- Run docker spotify/kafka
- Run Java/Kotlin/Go app from IDE (Prefer IDEA & Gogland)
- Send request to Java Source App - POST http://localhost:8080/message {“name”: “Krzysztof”}
- Both Go and Kotlin app will print the messages on their consoles, something like this:
INFO 22360 --- [afka-listener-1] com.example.SampleSink Kotlin received: Krzysztof
Golang received: Krzysztof
Content-Type with Go Application Problem
If you have Spring Applications, you can just set content-type inside properties like this:
spring.cloud.stream.bindings.output.content-type=application/json
But Go Shopify/sarama client has problem with it, I used binary format, this way:
spring.cloud.stream.bindings.output.producer.headerMode=raw
And my source in Java looks like this:
@PostMapping(path = "/message", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody void sendMessage(@RequestBody MessageDto message) throws JsonProcessingException {
byte[] bytes = objectMapper.writeValueAsBytes(message);
// normally you can use something like this instead of bytes :
// MessageBuilder.withPayload(message).build()
output.send(new GenericMessage<>(bytes));
}
If you got better solution, tell me :)
Avro Content-Type
My example doesn’t include it, but it is a popular Content-Type used with Apache Kafka.
- Spring Cloud Stream Docs
- BeeWorks Blog: Start Streaming with Kafka and Spring Cloud
- Github: Spring Cloud Stream, Kafka, Avro examples
References
- Implementing CQRS using Kafka and Sarama Library in Golang
- Event Driven Microservices with Spring Cloud Stream
- Go 1.8 HTTP/2 Push - in my example it is almost the same
HTTP/2 Go Push Bonus (only for fun)
- Go 1.8beta2 required
- Run crypto/tls/generate_cert.go to generate cert.pem and key.pem. and put them to the project
- Go to https://localhost:8082/ (yes, https)
- Resources will be pushed to browser, check Network (devtools)