Skip to content

Commit d9ffd50

Browse files
author
欧俊
committed
合并更新
2 parents 91d13c5 + 17538d2 commit d9ffd50

8 files changed

Lines changed: 81 additions & 42 deletions

File tree

.github/workflows/dotnetcore.yml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
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:
18-
dotnet-version: 5.0.301
21+
dotnet-version: 3.1.100
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
22-
run: dotnet pack src/RabbitMQ.EventBus.AspNetCore -c Release --include-symbols --include-source -p:PackageVersion=5.0.$GITHUB_RUN_NUMBER -o artifacts/
33+
run: dotnet pack src/RabbitMQ.EventBus.AspNetCore -c Release --include-symbols --include-source -p:PackageVersion=3.1.$GITHUB_RUN_NUMBER -o artifacts/
2334
- name: Publish Symbols to NuGet
2435
run: dotnet nuget push artifacts/*.symbols.nupkg --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ 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-
for (int i = 0; i < 1000; i++)
27+
for (int i = 0; i < 100; i++)
2828
{
2929
_eventBus.Publish(new
3030
{
31-
Body = $"rabbitmq.eventbus.test1=>发送消息/t{i}",
31+
Body = "rabbitmq.eventbus.test1=>发送消息",
3232
Time = DateTimeOffset.Now,
3333
}, exchange: "RabbitMQ.EventBus.Simple", routingKey: "rabbitmq.eventbus.test1");
34-
await Task.Yield();
35-
await Task.Delay(500);
3634
}
3735
return "Ok";
3836
}

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));

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-midnight

docs/index.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Welcome to GitHub Pages
2+
3+
You can use the [editor on GitHub](https://github.com/ojdev/RabbitMQ.EventBus.AspNetCore/edit/master/docs/index.md) to maintain and preview the content for your website in Markdown files.
4+
5+
Whenever you commit to this repository, GitHub Pages will run [Jekyll](https://jekyllrb.com/) to rebuild the pages in your site, from the content in your Markdown files.
6+
7+
### Markdown
8+
9+
Markdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for
10+
11+
```markdown
12+
Syntax highlighted code block
13+
14+
# Header 1
15+
## Header 2
16+
### Header 3
17+
18+
- Bulleted
19+
- List
20+
21+
1. Numbered
22+
2. List
23+
24+
**Bold** and _Italic_ and `Code` text
25+
26+
[Link](url) and ![Image](src)
27+
```
28+
29+
For more details see [GitHub Flavored Markdown](https://guides.github.com/features/mastering-markdown/).
30+
31+
### Jekyll Themes
32+
33+
Your Pages site will use the layout and styles from the Jekyll theme you have selected in your [repository settings](https://github.com/ojdev/RabbitMQ.EventBus.AspNetCore/settings/pages). The name of this theme is saved in the Jekyll `_config.yml` configuration file.
34+
35+
### Support or Contact
36+
37+
Having trouble with Pages? Check out our [documentation](https://docs.github.com/categories/github-pages-basics/) or [contact support](https://support.github.com/contact) and we’ll help you sort it out.

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 & 17 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,8 @@ 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));
6357
IBasicProperties properties = _publishChannel.CreateBasicProperties();
6458
properties.DeliveryMode = 2; // persistent
6559
_publishChannel.BasicPublish(exchange: exchange,
@@ -81,11 +75,9 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
8175
string queue = attr.Queue ?? (_persistentConnection.Configuration.Prefix == QueuePrefixType.ExchangeName
8276
? $"{ attr.Exchange }.{ eventType.Name }"
8377
: $"{_persistentConnection.Configuration.ClientProvidedName}.{ eventType.Name }");
84-
if (!_persistentConnection.IsConnected)
85-
{
86-
_persistentConnection.TryConnect();
87-
}
88-
IModel channel;
78+
79+
var onlyKey = $"{attr.Exchange}_{queue}_{attr.RoutingKey}";
80+
subscribes.TryGetValue(onlyKey, out IModel channel);
8981
#region snippet
9082
var arguments = new Dictionary<string, object>();
9183

@@ -135,7 +127,8 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
135127
#endregion
136128
channel.QueueBind(queue, attr.Exchange, attr.RoutingKey, null);
137129
channel.BasicQos(0, _persistentConnection.Configuration.PrefetchCount, false);
138-
EventingBasicConsumer consumer = new(channel);
130+
subscribes[onlyKey] = channel;
131+
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
139132
consumer.Received += async (model, ea) =>
140133
{
141134
string body = Encoding.UTF8.GetString(ea.Body.ToArray());

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)