-
Notifications
You must be signed in to change notification settings - Fork 27
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
10 changed files
with
163 additions
and
49 deletions.
There are no files selected for viewing
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
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,14 @@ | ||
namespace Thor.RabbitMQEvent; | ||
|
||
public class EventEto | ||
{ | ||
public string FullName { get; set; } | ||
|
||
public byte[] Data { get; set; } | ||
|
||
public EventEto(string fullName, byte[] data) | ||
{ | ||
FullName = fullName; | ||
Data = data; | ||
} | ||
} |
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,16 @@ | ||
using System.Text.Json; | ||
using Raccoon.Stack.Rabbit; | ||
using Thor.BuildingBlocks.Data; | ||
|
||
namespace Thor.RabbitMQEvent; | ||
|
||
public class RabbitMQEventBus<TEvent>(RabbitClient rabbitClient) : IEventBus<TEvent> where TEvent : class | ||
{ | ||
public async ValueTask PublishAsync(TEvent @event) | ||
{ | ||
var eto = new EventEto(@event.GetType().FullName, JsonSerializer.SerializeToUtf8Bytes(@event)); | ||
|
||
await rabbitClient.PublishAsync("Thor:EventBus:exchange", "Thor:EventBus:key", | ||
JsonSerializer.SerializeToUtf8Bytes(eto)); | ||
} | ||
} |
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,42 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using RabbitMQ.Client.Events; | ||
using Raccoon.Stack.Rabbit; | ||
using Raccoon.Stack.Rabbit.Handler; | ||
using Thor.BuildingBlocks.Data; | ||
|
||
namespace Thor.RabbitMQEvent; | ||
|
||
public class RabbitMQEventHandler : IRabbitHandler | ||
{ | ||
public bool Enable(ConsumeOptions options) | ||
{ | ||
return options.Queue.Equals("Thor:EventBus", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public async Task Handle(IServiceProvider sp, BasicDeliverEventArgs args, ConsumeOptions options) | ||
{ | ||
var eto = JsonSerializer.Deserialize<EventEto>(args.Body.ToArray()); | ||
|
||
// type : Thor.Service.Domain.ChatLogger | ||
|
||
var type = Assembly.GetEntryAssembly()?.GetType(eto.FullName); | ||
|
||
if (type == null) | ||
{ | ||
return; | ||
} | ||
|
||
var @event = JsonSerializer.Deserialize(eto.Data, type); | ||
|
||
// IEventHandler<ChatLogger> | ||
var handlerType = typeof(IEventHandler<>).MakeGenericType(type); | ||
|
||
var handler = sp.GetRequiredService(handlerType); | ||
|
||
var method = handlerType.GetMethod("HandleAsync"); | ||
|
||
await (Task)method.Invoke(handler, new object[] { @event }); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/framework/Thor.RabbitMQEvent/ServiceCollectionExtensions.cs
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,45 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using RabbitMQ.Client; | ||
using Raccoon.Stack.Rabbit; | ||
using Thor.BuildingBlocks.Data; | ||
|
||
namespace Thor.RabbitMQEvent; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddRabbitMQEventBus(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
// 是否启用RabbitMQ | ||
var connection = configuration["RabbitMQ:ConnectionString"]; | ||
if (string.IsNullOrWhiteSpace(connection)) | ||
{ | ||
return services; | ||
} | ||
|
||
services | ||
.AddSingleton(typeof(IEventBus<>), typeof(RabbitMQEventBus<>)); | ||
|
||
services.AddRabbitBoot((options => | ||
{ | ||
options.ConnectionString = connection; | ||
options.Consumes = | ||
[ | ||
new ConsumeOptions | ||
{ | ||
AutoAck = false, | ||
FetchCount = 10, | ||
Queue = "Thor:EventBus", | ||
Declaration = (declaration => | ||
{ | ||
declaration.QueueDeclareAsync("Thor:EventBus", true); | ||
declaration.ExchangeDeclareAsync("Thor:EventBus:exchange", ExchangeType.Direct, true); | ||
declaration.QueueBindAsync("Thor:EventBus", "Thor:EventBus:exchange", "Thor:EventBus:key"); | ||
}) | ||
} | ||
]; | ||
}), typeof(RabbitMQEventBus<>).Assembly); | ||
|
||
return services; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/framework/Thor.RabbitMQEvent/Thor.RabbitMQEvent.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Raccoon.Stack.Rabbit" Version="0.0.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Thor.BuildingBlocks.Event\Thor.BuildingBlocks.Event.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |