-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
182 additions
and
156 deletions.
There are no files selected for viewing
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Realtime Feed | ||
|
||
This example features a very busy blogging platform, with thousands of messages showing up on your feed. | ||
|
||
There are two separate applications (microservices) integrating over a Kafka topic. The [`producer`](producer/) generates | ||
thousands of "posts" and publishes them to the topic. The [`consumer`](consumer/) subscribes to this topic and | ||
displays each post on the standard output. | ||
|
||
The consumer has a throttling middleware enabled, so you have a chance to actually read the posts. | ||
|
||
To understand the background and internals, see [getting started guide](https://watermill.io/docs/getting-started/). | ||
|
||
## Requirements | ||
|
||
To run this example you will need Docker and docker-compose installed. See the [installation guide](https://docs.docker.com/compose/install/). | ||
|
||
## Running | ||
|
||
```bash | ||
docker-compose up | ||
``` | ||
|
||
You should see the live feed of posts on the standard output. | ||
|
||
## Exercises | ||
|
||
1. Peek into the posts counter published on `posts_count` topic. | ||
|
||
``` | ||
docker-compose exec kafka kafka-console-consumer --bootstrap-server localhost:9092 --topic posts_count | ||
``` | ||
|
||
2. Add a persistent storage for incoming posts in the consumer service, instead of displaying them. | ||
Consider using the [SQL Publisher](https://github.com/ThreeDotsLabs/watermill-sql). |
File renamed without changes.
File renamed without changes.
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
14 changes: 7 additions & 7 deletions
14
_examples/simple-app/docker-compose.yml → .../basic/2-realtime-feed/docker-compose.yml
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"time" | ||
|
||
"github.com/brianvoe/gofakeit" | ||
|
||
"github.com/ThreeDotsLabs/watermill" | ||
"github.com/ThreeDotsLabs/watermill-kafka/pkg/kafka" | ||
"github.com/ThreeDotsLabs/watermill/message" | ||
"github.com/ThreeDotsLabs/watermill/message/router/middleware" | ||
) | ||
|
||
var ( | ||
brokers = []string{"kafka:9092"} | ||
|
||
messagesPerSecond = 100 | ||
numWorkers = 20 | ||
) | ||
|
||
func main() { | ||
logger := watermill.NewStdLogger(false, false) | ||
logger.Info("Starting the producer", watermill.LogFields{}) | ||
|
||
rand.Seed(time.Now().Unix()) | ||
|
||
publisher, err := kafka.NewPublisher(brokers, kafka.DefaultMarshaler{}, nil, logger) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer publisher.Close() | ||
|
||
closeCh := make(chan struct{}) | ||
workersGroup := &sync.WaitGroup{} | ||
workersGroup.Add(numWorkers) | ||
|
||
for i := 0; i < numWorkers; i++ { | ||
go worker(publisher, workersGroup, closeCh) | ||
} | ||
|
||
// wait for SIGINT | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt) | ||
<-c | ||
|
||
// signal for the workers to stop publishing | ||
close(closeCh) | ||
|
||
// Waiting for all messages to be published | ||
workersGroup.Wait() | ||
|
||
logger.Info("All messages published", nil) | ||
} | ||
|
||
// worker publishes messages until closeCh is closed. | ||
func worker(publisher message.Publisher, wg *sync.WaitGroup, closeCh chan struct{}) { | ||
ticker := time.NewTicker(time.Duration(int(time.Second) / messagesPerSecond)) | ||
|
||
for { | ||
select { | ||
case <-closeCh: | ||
ticker.Stop() | ||
wg.Done() | ||
return | ||
|
||
case <-ticker.C: | ||
} | ||
|
||
msgPayload := postAdded{ | ||
OccurredOn: time.Now(), | ||
Author: gofakeit.Username(), | ||
Title: gofakeit.Sentence(rand.Intn(5) + 1), | ||
Content: gofakeit.Sentence(rand.Intn(10) + 5), | ||
} | ||
|
||
payload, err := json.Marshal(msgPayload) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
msg := message.NewMessage(watermill.NewUUID(), payload) | ||
|
||
// Use a middleware to set the correlation ID, it's useful for debugging | ||
middleware.SetCorrelationID(watermill.NewShortUUID(), msg) | ||
err = publisher.Publish("posts_published", msg) | ||
if err != nil { | ||
fmt.Println("cannot publish message:", err) | ||
continue | ||
} | ||
} | ||
} | ||
|
||
type postAdded struct { | ||
OccurredOn time.Time `json:"occurred_on"` | ||
|
||
Author string `json:"author"` | ||
Title string `json:"title"` | ||
|
||
Content string `json:"content"` | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.