https://github.com/june-it/mystack.distributedmessage4rabbitmq
An open-source lightweight message bus library (RabbitMQ) that supports publish/subscribe and RPC.
https://github.com/june-it/mystack.distributedmessage4rabbitmq
distributed-event dotnet dotnet-core event-bus mystack rabbitmq rpc
Last synced: 4 months ago
JSON representation
An open-source lightweight message bus library (RabbitMQ) that supports publish/subscribe and RPC.
- Host: GitHub
- URL: https://github.com/june-it/mystack.distributedmessage4rabbitmq
- Owner: june-it
- License: mit
- Created: 2024-08-22T05:18:02.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-02-25T00:47:25.000Z (4 months ago)
- Last Synced: 2025-03-16T01:12:27.622Z (4 months ago)
- Topics: distributed-event, dotnet, dotnet-core, event-bus, mystack, rabbitmq, rpc
- Language: C#
- Homepage:
- Size: 134 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MyStack.DistributedMessage4RabbitMQ
An open-source lightweight message bus library (RabbitMQ) that supports publish/subscribe and RPC.
| nuget | stats |
| ----------- | ----------- |
| [](https://www.nuget.org/packages/MyStack.DistributedMessage4RabbitMQ) | [](https://www.nuget.org/stats/packages/MyStack.DistributedMessage4RabbitMQ?groupby=Version) |# Install MyStack.DistributedMessage4RabbitMQ
You can install via NuGet:
```csharp
Install-Package MyStack.DistributedMessage4RabbitMQ# Getting Started
## Add Service Support
```csharp
services.AddDistributedMessage4RabbitMQ(configure =>
{
configure.HostName = "localhost";
configure.VirtualHost = "/";
configure.Port = 5672;
configure.UserName = "admin";
configure.Password = "admin";
configure.QueueOptions.Name = "MyStack";
configure.ExchangeOptions.Name = "MyStack";
configure.ExchangeOptions.ExchangeType = "topic";
configure.RPCTimeout = 2000;
},
Assembly.GetExecutingAssembly());
```## Event Subscription
### Define Event
```
public class HelloMessage : DistributedEventBase
{
public string Message { get; set; }
}or
[ExchangeDeclare("Hello")]
[QueueDeclare("Hello")]
[QueueBind("HelloMessage")]
public class HelloMessage : DistributedEventBase
{
public string Message { get; set; } = default!;
}
```### Subscribe to Event
```
public class HelloMessageHandler : IDistributedEventHandler
{
public async Task HandleAsync(HelloMessage message, CancellationToken cancellationToken)
{
Console.WriteLine("Hello");
await Task.CompletedTask;
}
}
```
### Publish Event
```
await messageBus.PublishAsync(new HelloMessage() { Message = "Hello" });or
var hello = new HelloMessage()
{
Message = "Hello World"
};
hello.Metadata.Add("key1", "value");
messageBus.PublishAsync(hello);
```## Event Wrapper Subscription
### Define Wrapped Data
```
public class WrappedData
{
public string Message { get; set; }
}```
### Subscribe to Event
```
public class DistributedEventWrapperHandler : IDistributedEventHandler>
{
public async Task HandleAsync(DistributedEventWrapper eventData, CancellationToken cancellationToken = default)
{
await Task.CompletedTask;
Console.WriteLine("DistributedEventWrapper");
}
}
```
### Publish Event
```
await messageBus.PublishAsync(new WrappedData());
```## 3、RPC Request
### Define Request
```
public class Ping : IRpcRequest
{
public string SendBy { get; set; }
}
```
### Define Response
```
public class Pong
{
public string ReplyBy { get; set; }
}
```### Subscribe to Request
```
public class PingHandler : IRpcRequestHandler
{public Task HandleAsync(Ping message, CancellationToken cancellationToken = default)
{
Console.WriteLine("Ping");
return Task.FromResult(new Pong() { ReplyBy = "B" });
}
}
```
### Send Request
```
var pongMessage = messageBus.SendAsync(ping);
```# License
MIT