fix naming + update README
This commit is contained in:
parent
5762654b4b
commit
06642b84ca
48
README.md
48
README.md
@ -1,3 +1,49 @@
|
|||||||
# redis-queueing-sample
|
# 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 }
|
||||||
|
```
|
||||||
@ -1,5 +1,5 @@
|
|||||||
use redis_queueing_sample::model::{Action, Message};
|
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};
|
use redis::{AsyncCommands, Client};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@ -10,7 +10,7 @@ async fn main() -> Result<(), GenericError> {
|
|||||||
let message = Message::new(1, Action::Download);
|
let message = Message::new(1, Action::Download);
|
||||||
let json_str = serde_json::to_string(&message)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use redis::AsyncCommands;
|
use redis::AsyncCommands;
|
||||||
|
|
||||||
use redis_queueing_sample::model::Message;
|
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> {
|
async fn read_queue(mut conn: redis::aio::Connection) -> Result<(), GenericError> {
|
||||||
loop {
|
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)?;
|
let data: Message = serde_json::from_str(&json_data)?;
|
||||||
println!("Received data: {:?}", data);
|
println!("Received data: {:?}", data);
|
||||||
|
|||||||
@ -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 type GenericError = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
pub const REDIS_PATH: &'static str = "queueing-sample";
|
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 {
|
pub mod model {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user