fix naming + update README

This commit is contained in:
rmanach 2023-07-12 09:06:05 +02:00
parent 5762654b4b
commit 06642b84ca
4 changed files with 52 additions and 6 deletions

View File

@ -1,3 +1,49 @@
# redis-queueing-sample
This repo aims to show how to communicate between two `services` using **Redis** as a message broker with pub/sub channels.
This repo aims to show how to communicate between two `services` using **Redis** as a message broker with [pub/sub channels](https://redis.io/docs/interact/pubsub/).
## How it works ?
```ascii
┌────────────┐
│server │
┌───────────────► │
│ │ │
│ │ │
┌────────────┐ ┌─────┴──────┐ │ │
│client │ │redis │ └────────────┘
│ │ │ │
│ ├───────► │
│ │ │ │
│ │ │:6379 │ ┌────────────┐
└────────────┘ └─────┬──────┘ │server │
│ │ │
│ │ │
│ │ │
└───────────────► │
└────────────┘
```
* A client publish a message to the Redis channel
* Servers subscribe to the channel and handle the message
## Run
* Start Redis and the server
```bash
make run
```
* Publish messages with the client
```bash
make run-client
```
You should see the following server logs:
```
listening message...
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
Received data: Message { id: 1, action: Download }
```

View File

@ -1,5 +1,5 @@
use redis_queueing_sample::model::{Action, Message};
use redis_queueing_sample::{GenericError, REDIS_QUEUE_NAME, REDIS_URL};
use redis_queueing_sample::{GenericError, REDIS_CHANNEL_NAME, REDIS_URL};
use redis::{AsyncCommands, Client};
#[tokio::main]
@ -10,7 +10,7 @@ async fn main() -> Result<(), GenericError> {
let message = Message::new(1, Action::Download);
let json_str = serde_json::to_string(&message)?;
let _: () = conn.rpush(REDIS_QUEUE_NAME, json_str).await?;
let _: () = conn.rpush(REDIS_CHANNEL_NAME, json_str).await?;
Ok(())
}

View File

@ -1,11 +1,11 @@
use redis::AsyncCommands;
use redis_queueing_sample::model::Message;
use redis_queueing_sample::{GenericError, REDIS_QUEUE_NAME, REDIS_URL};
use redis_queueing_sample::{GenericError, REDIS_CHANNEL_NAME, REDIS_URL};
async fn read_queue(mut conn: redis::aio::Connection) -> Result<(), GenericError> {
loop {
let (_, json_data): (String, String) = conn.blpop(REDIS_QUEUE_NAME, 0).await?;
let (_, json_data): (String, String) = conn.blpop(REDIS_CHANNEL_NAME, 0).await?;
let data: Message = serde_json::from_str(&json_data)?;
println!("Received data: {:?}", data);

View File

@ -3,7 +3,7 @@ pub static REDIS_URL: &str = "redis://:11677f0c-ead4-4434-ad14-3d54ce2521ce@127.
pub type GenericError = Box<dyn std::error::Error>;
pub const REDIS_PATH: &'static str = "queueing-sample";
pub const REDIS_QUEUE_NAME: &'static str = "messages";
pub const REDIS_CHANNEL_NAME: &'static str = "messages";
pub mod model {
use serde::{Deserialize, Serialize};