Skip to content

Commit bf24f51

Browse files
authored
Merge pull request #50 from ojdev/dev
Dev
2 parents 87604ec + b8e255d commit bf24f51

6 files changed

Lines changed: 40 additions & 35 deletions

File tree

.github/workflows/dotnetcore.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
name: .NET Core
1+
name: .NET Core App CI to Docker Hub
22

3-
on:
3+
on:
44
push:
5-
branches:
6-
- master
5+
tags:
6+
- v*
7+
78

89
jobs:
910
build:
10-
11-
runs-on: [ubuntu-latest]
12-
11+
12+
runs-on: ubuntu-latest
13+
env:
14+
TZ: Asia/Shanghai
15+
TAG_NUMBER: $GITHUB_RUN_NUMBER
1316
steps:
14-
- uses: actions/checkout@v1
15-
- name: Setup .NET Core
17+
- uses: actions/checkout@v2
18+
- name: Setup .NET
1619
uses: actions/setup-dotnet@v1
1720
with:
1821
dotnet-version: 5.0.301
22+
- name: Get tag
23+
id: tag
24+
uses: dawidd6/action-get-tag@v1
25+
with:
26+
# Optionally strip `v` prefix
27+
strip_v: true
28+
- name: Set VERSION variable from tag
29+
run: echo ${{steps.tag.outputs.tag}}
1930
- name: Build with dotnet
2031
run: dotnet build --configuration Release src/RabbitMQ.EventBus.AspNetCore
2132
- name: Pack

RabbitMQ.EventBus.AspNetCore.Sample/Controllers/ValuesController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public async Task<ActionResult<string>> Get()
2424
// Body = "rabbitmq.eventbus.test=>发送消息",
2525
// Time = DateTimeOffset.Now
2626
//}, exchange: "RabbitMQ.EventBus.Simple", routingKey: "rabbitmq.eventbus.test");
27+
2728
for (int i = 0; i < 1000; i++)
2829
{
2930
_eventBus.Publish(new

RabbitMQ.EventBus.AspNetCore.Sample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void ConfigureServices(IServiceCollection services)
2727
services.AddControllers();
2828
services.AddHealthChecks();
2929

30-
services.AddRabbitMQEventBus(() => "amqp://guest:guest@192.168.0.250:5672/", eventBusOptionAction: eventBusOption =>
30+
services.AddRabbitMQEventBus(() => "amqp://guest:guest@localhost:5672/", eventBusOptionAction: eventBusOption =>
3131
{
3232
eventBusOption.ClientProvidedAssembly(assemblyName);
3333
eventBusOption.EnableRetryOnFailure(true, 5000, TimeSpan.FromSeconds(30));

src/RabbitMQ.EventBus.AspNetCore/Configurations/DeadLetterExchangeConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public class DeadLetterExchangeConfig
2424
/// <summary>
2525
///
2626
/// </summary>
27-
/// <param name="enabled">是否开启(默认开启)</param>
27+
/// <param name="enabled">是否开启(默认关闭)</param>
2828
/// <param name="exchangeNamePrefix">交换机名前缀(默认为"dead-")</param>
2929
/// <param name="exchangeNameSuffix">交换机名后缀</param>
3030
/// <param name="customizeExchangeName">自定义交换机名(留空则使用原有的交换机名)</param>
31-
public DeadLetterExchangeConfig(bool enabled = true, string exchangeNamePrefix = "dead-", string exchangeNameSuffix = null, string customizeExchangeName = null)
31+
public DeadLetterExchangeConfig(bool enabled = false, string exchangeNamePrefix = "dead-", string exchangeNameSuffix = null, string customizeExchangeName = null)
3232
{
3333
Enabled = enabled;
3434
ExchangeNameSuffix = exchangeNameSuffix;

src/RabbitMQ.EventBus.AspNetCore/DefaultRabbitMQEventBus.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class DefaultRabbitMQEventBus : IRabbitMQEventBus
2323
private readonly ILogger<DefaultRabbitMQEventBus> _logger;
2424
private readonly IServiceProvider _serviceProvider;
2525
private readonly IEventHandlerModuleFactory _eventHandlerFactory;
26+
private readonly Dictionary<string, IModel> subscribes;
2627
/// <summary>
2728
///
2829
/// </summary>
@@ -36,9 +37,10 @@ public DefaultRabbitMQEventBus(IRabbitMQPersistentConnection persistentConnectio
3637
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
3738
_eventHandlerFactory = eventHandlerFactory ?? throw new ArgumentNullException(nameof(eventHandlerFactory));
3839
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
40+
subscribes = new Dictionary<string, IModel>();
41+
_logger.LogInformation("消息队列准备就绪");
3942
}
4043

41-
private IModel _publishChannel;
4244
/// <summary>
4345
///
4446
/// </summary>
@@ -50,16 +52,9 @@ public DefaultRabbitMQEventBus(IRabbitMQPersistentConnection persistentConnectio
5052
public void Publish<TMessage>(TMessage message, string exchange, string routingKey, string type = ExchangeType.Topic)
5153
{
5254
string body = message.Serialize();
53-
if (_publishChannel?.IsOpen != true)
54-
{
55-
if (_persistentConnection.IsConnected)
56-
{
57-
_persistentConnection.TryConnect();
58-
}
59-
_publishChannel = _persistentConnection.ExchangeDeclare(exchange, type: type);
60-
_publishChannel.BasicReturn += async (se, ex) => await Task.Delay((int)_persistentConnection.Configuration.ConsumerFailRetryInterval.TotalMilliseconds).ContinueWith(t => Publish(body, ex.Exchange, ex.RoutingKey));
61-
}
62-
55+
using var _publishChannel = _persistentConnection.ExchangeDeclare(exchange, type: type);
56+
_publishChannel.BasicReturn += async (se, ex) => await Task.Delay((int)_persistentConnection.Configuration.ConsumerFailRetryInterval.TotalMilliseconds).ContinueWith(t => Publish(body, ex.Exchange, ex.RoutingKey));
57+
6358
IBasicProperties properties = _publishChannel.CreateBasicProperties();
6459
properties.DeliveryMode = 2; // persistent
6560
_publishChannel.BasicPublish(exchange: exchange,
@@ -81,11 +76,9 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
8176
string queue = attr.Queue ?? (_persistentConnection.Configuration.Prefix == QueuePrefixType.ExchangeName
8277
? $"{ attr.Exchange }.{ eventType.Name }"
8378
: $"{_persistentConnection.Configuration.ClientProvidedName}.{ eventType.Name }");
84-
if (!_persistentConnection.IsConnected)
85-
{
86-
_persistentConnection.TryConnect();
87-
}
88-
IModel channel;
79+
80+
var onlyKey = $"{attr.Exchange}_{queue}_{attr.RoutingKey}";
81+
subscribes.TryGetValue(onlyKey, out IModel channel);
8982
#region snippet
9083
var arguments = new Dictionary<string, object>();
9184

@@ -135,6 +128,7 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
135128
#endregion
136129
channel.QueueBind(queue, attr.Exchange, attr.RoutingKey, null);
137130
channel.BasicQos(0, _persistentConnection.Configuration.PrefetchCount, false);
131+
subscribes[onlyKey] = channel;
138132
EventingBasicConsumer consumer = new(channel);
139133
consumer.Received += async (model, ea) =>
140134
{

src/RabbitMQ.EventBus.AspNetCore/Factories/DefaultRabbitMQPersistentConnection.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,19 @@ public bool TryConnect()
8181
);
8282

8383
policy.Execute(() =>
84-
{
85-
string connectionString = _connectionAction.Invoke();
86-
_logger.WriteLog(LogLevel.Information, $"[ConnectionString]:\t{connectionString}");
87-
_connectionFactory.Uri = new Uri(connectionString);
88-
_connection = _connectionFactory.CreateConnection(clientProvidedName: Configuration.ClientProvidedName);
89-
});
84+
{
85+
string connectionString = _connectionAction.Invoke();
86+
_logger.WriteLog(LogLevel.Information, $"[ConnectionString]:\t{connectionString}");
87+
_connectionFactory.Uri = new Uri(connectionString);
88+
_connection = _connectionFactory.CreateConnection(clientProvidedName: Configuration.ClientProvidedName);
89+
});
9090

9191
if (IsConnected)
9292
{
9393
_connection.ConnectionShutdown += OnConnectionShutdown;
9494
_connection.CallbackException += OnCallbackException;
9595
_connection.ConnectionBlocked += OnConnectionBlocked;
9696
_logger.WriteLog(LogLevel.Information, $"RabbitMQ persistent connection acquired a connection {_connection.Endpoint.HostName} and is subscribed to failure events");
97-
9897
return true;
9998
}
10099
else

0 commit comments

Comments
 (0)