Skip to content

Commit 3f11c16

Browse files
author
欧俊
committed
修改部分说明信息,强化包信息
1 parent fca1d3d commit 3f11c16

3 files changed

Lines changed: 301 additions & 3 deletions

File tree

RabbitMQ.EventBus.AspNetCore.sln

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29409.12
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33205.214
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AACDCE8A-FB3B-496A-9ADA-527265C9B334}"
77
EndProject
@@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "other", "other", "{1619DC26
1111
ProjectSection(SolutionItems) = preProject
1212
LICENSE = LICENSE
1313
README.md = README.md
14+
WIKI.md = WIKI.md
1415
EndProjectSection
1516
EndProject
1617
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F1EBF978-A647-4E88-A8E8-6B1F9381ADF2}"

WIKI.md

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
# [RabbitMQ.EventBus.AspNetCore](https://github.com/ojdev/RabbitMQ.EventBus.AspNetCore)
2+
3+
该包为一个基于官方RabbitMQ.Client的二次封装包,专门针对Asp.Net Core项目进行开发,在微服务中进行消息的传递使用起来比较方便。
4+
5+
目前功能:
6+
7+
- [x] 发布/订阅
8+
- [x] 死信队列
9+
- [x] RPC功能(实验性)
10+
11+
### 使用说明(>=6.0.0)
12+
13+
#### 1. 注册
14+
~~~ csharp
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
string assemblyName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
18+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
19+
services.AddRabbitMQEventBus("localhost", 5672, "guest", "guest", "", eventBusOptionAction: eventBusOption =>
20+
{
21+
eventBusOption.ClientProvidedAssembly(assemblyName);
22+
eventBusOption.EnableRetryOnFailure(true, 5000, TimeSpan.FromSeconds(30));
23+
eventBusOption.RetryOnFailure(TimeSpan.FromSeconds(1));
24+
eventBusOption.MessageTTL(2000);
25+
eventBusOption.SetBasicQos(10);
26+
eventBusOption.DeadLetterExchangeConfig(config =>
27+
{
28+
config.Enabled = false;
29+
config.ExchangeNameSuffix = "-test";
30+
});
31+
});
32+
33+
//or
34+
//
35+
//services.AddRabbitMQEventBus(() => "amqp://guest:guest@localhost:5672/", eventBusOptionAction: eventBusOption =>
36+
//{
37+
// eventBusOption.ClientProvidedAssembly(assemblyName);
38+
// eventBusOption.EnableRetryOnFailure(true, 5000, TimeSpan.FromSeconds(30));
39+
// eventBusOption.RetryOnFailure(TimeSpan.FromSeconds(1));
40+
// eventBusOption.MessageTTL(2000);
41+
// eventBusOption.SetBasicQos(10);
42+
// eventBusOption.DeadLetterExchangeConfig(config =>
43+
// {
44+
// config.Enabled = false;
45+
// config.ExchangeNameSuffix = "-test";
46+
// });
47+
//});
48+
}
49+
~~~
50+
#### 2. 发消息
51+
##### 2.1 直接发送消息
52+
~~~ csharp
53+
[Route("api/[controller]")]
54+
[ApiController]
55+
public class EventBusController : ControllerBase
56+
{
57+
private readonly IRabbitMQEventBus _eventBus;
58+
59+
public EventBusController(IRabbitMQEventBus eventBus)
60+
{
61+
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
62+
}
63+
64+
// GET api/values
65+
[HttpGet]
66+
public IActionResult Send()
67+
{
68+
_eventBus.Publish(new
69+
{
70+
Body = "发送消息",
71+
Time = DateTimeOffset.Now
72+
}, exchange: "RabbitMQ.EventBus.Simple", routingKey: "rabbitmq.eventbus.test");
73+
return Ok();
74+
}
75+
}
76+
~~~
77+
##### 2.1 发送消息并等待回复
78+
~~~ csharp
79+
[Route("api/[controller]")]
80+
[ApiController]
81+
public class EventBusController : ControllerBase
82+
{
83+
private readonly IRabbitMQEventBus _eventBus;
84+
85+
public EventBusController(IRabbitMQEventBus eventBus)
86+
{
87+
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
88+
}
89+
90+
// GET api/values
91+
[HttpGet]
92+
public async Task<ActionResult<string>> Get()
93+
{
94+
Console.WriteLine($"发送消息{1}");
95+
var body = new
96+
{
97+
requestId = Guid.NewGuid(),
98+
Body = $"rabbitmq.eventbus.test=>发送消息\t{1}",
99+
Time = DateTimeOffset.Now,
100+
};
101+
var r = await _eventBus.PublishAsync<string>(body, exchange: "RabbitMQ.EventBus.Simple", routingKey: "rabbitmq.eventbus.test");
102+
Console.WriteLine($"返回了{r}");
103+
await Task.Delay(500);
104+
return r;
105+
}
106+
}
107+
~~~
108+
#### 3. 订阅消息
109+
##### 1. 订阅消息(无回复)
110+
~~~ csharp
111+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test")]
112+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test1")]
113+
public class MessageBody : IEvent
114+
{
115+
public string Body { get; set; }
116+
public DateTimeOffset Time { get; set; }
117+
}
118+
public class MessageBodyHandle : IEventHandler<MessageBody>, IDisposable
119+
{
120+
private readonly ILogger<MessageBodyHandle> _logger;
121+
122+
public MessageBodyHandle(ILogger<MessageBodyHandle> logger)
123+
{
124+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
125+
}
126+
127+
public void Dispose()
128+
{
129+
Console.WriteLine("释放");
130+
}
131+
132+
public Task Handle(EventHandlerArgs<MessageBody1> args)
133+
{
134+
_logger.Information(args.Original);
135+
_logger.Information(args.Redelivered);
136+
_logger.Information(args.Exchange);
137+
_logger.Information(args.RoutingKey);
138+
139+
_logger.Information(args.Event.Body);
140+
return Task.CompletedTask;
141+
}
142+
}
143+
~~~
144+
##### 1. 订阅消息并回复
145+
~~~ csharp
146+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test")]
147+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test1")]
148+
public class MessageBody : IEvent
149+
{
150+
public string Body { get; set; }
151+
public DateTimeOffset Time { get; set; }
152+
}
153+
public class MessageBodyHandle : IEventResponseHandler<MessageBody, string>, IDisposable
154+
{
155+
private Guid id;
156+
private readonly ILogger<MessageBodyHandle> _logger;
157+
158+
public MessageBodyHandle(ILogger<MessageBodyHandle> logger)
159+
{
160+
id = Guid.NewGuid();
161+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
162+
}
163+
public void Dispose()
164+
{
165+
_logger.LogInformation("MessageBodyHandle Disposable.");
166+
}
167+
168+
169+
public Task<string> HandleAsync(HandlerEventArgs<MessageBody> args)
170+
{
171+
return Task.FromResult("收到消息,已确认" + DateTimeOffset.Now);
172+
}
173+
}
174+
~~~
175+
176+
### 使用说明(<=5.1.1)
177+
178+
#### 1. 注册
179+
~~~ csharp
180+
public void ConfigureServices(IServiceCollection services)
181+
{
182+
string assemblyName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
183+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
184+
services.AddRabbitMQEventBus(()=>"amqp://guest:guest@192.168.0.252:5672/", eventBusOptionAction: eventBusOption =>
185+
{
186+
eventBusOption.ClientProvidedAssembly(assemblyName);
187+
eventBusOption.EnableRetryOnFailure(true, 5000, TimeSpan.FromSeconds(30));
188+
eventBusOption.RetryOnFailure(TimeSpan.FromMilliseconds(100));
189+
eventBusOption.AddLogging(LogLevel.Warning);
190+
eventBusOption.MessageTTL(2000);
191+
eventBusOption.DeadLetterExchangeConfig(config =>
192+
{
193+
config.Enabled = true;
194+
config.ExchangeNameSuffix = "-test";
195+
});
196+
});
197+
services.AddButterfly(butterfly =>
198+
{
199+
butterfly.CollectorUrl = "http://192.168.0.252:6401";
200+
butterfly.Service = "RabbitMQEventBusTest";
201+
});
202+
}
203+
~~~
204+
#### 2. 订阅消息
205+
##### 2.1 自动订阅消息
206+
~~~ csharp
207+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceTracer tracer)
208+
{
209+
if (env.IsDevelopment())
210+
{
211+
app.UseDeveloperExceptionPage();
212+
}
213+
app.RabbitMQEventBusAutoSubscribe();
214+
app.UseMvc();
215+
}
216+
~~~
217+
##### 2.2 手动订阅消息
218+
~~~ csharp
219+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IRabbitMQEventBus eventBus)
220+
{
221+
if (env.IsDevelopment())
222+
{
223+
app.UseDeveloperExceptionPage();
224+
}
225+
eventBus.Serialize<EventMessage, EventMessageHandler>();
226+
app.UseMvc();
227+
}
228+
~~~
229+
#### 3. 发消息
230+
~~~ csharp
231+
[Route("api/[controller]")]
232+
[ApiController]
233+
public class EventBusController : ControllerBase
234+
{
235+
private readonly IRabbitMQEventBus _eventBus;
236+
237+
public EventBusController(IRabbitMQEventBus eventBus)
238+
{
239+
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
240+
}
241+
242+
// GET api/values
243+
[HttpGet]
244+
public IActionResult Send()
245+
{
246+
_eventBus.Publish(new
247+
{
248+
Body = "发送消息",
249+
Time = DateTimeOffset.Now
250+
}, exchange: "RabbitMQ.EventBus.Simple", routingKey: "rabbitmq.eventbus.test");
251+
return Ok();
252+
}
253+
}
254+
~~~
255+
#### 4. 订阅消息
256+
~~~ csharp
257+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test")]
258+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test1")]
259+
[EventBus(Exchange = "RabbitMQ.EventBus.Simple", RoutingKey = "rabbitmq.eventbus.test2")]
260+
public class MessageBody : IEvent
261+
{
262+
public string Body { get; set; }
263+
public DateTimeOffset Time { get; set; }
264+
}
265+
public class MessageBodyHandle : IEventHandler<MessageBody>, IDisposable
266+
{
267+
private readonly ILogger<MessageBodyHandle> _logger;
268+
269+
public MessageBodyHandle(ILogger<MessageBodyHandle> logger)
270+
{
271+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
272+
}
273+
274+
public void Dispose()
275+
{
276+
Console.WriteLine("释放");
277+
}
278+
279+
public Task Handle(EventHandlerArgs<MessageBody1> args)
280+
{
281+
_logger.Information(args.Original);
282+
_logger.Information(args.Redelivered);
283+
_logger.Information(args.Exchange);
284+
_logger.Information(args.RoutingKey);
285+
286+
_logger.Information(args.Event.Body);
287+
return Task.CompletedTask;
288+
}
289+
}
290+
~~~

src/RabbitMQ.EventBus.AspNetCore/RabbitMQ.EventBus.AspNetCore.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<PackageIconUrl></PackageIconUrl>
77
<RepositoryUrl>https://github.com/ojdev/RabbitMQ.EventBus.AspNetCore</RepositoryUrl>
8-
<RepositoryType>github</RepositoryType>
8+
<RepositoryType>git</RepositoryType>
99
<PackageTags>rqbbitmq,dnc,eventbus</PackageTags>
1010
<Company>欧俊</Company>
1111
<Authors>欧俊</Authors>
@@ -15,6 +15,9 @@
1515
<Version>6.0.0</Version>
1616
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1717
<ImplicitUsings>enable</ImplicitUsings>
18+
<Title>RabbitMQ.EventBus.AspNetCore</Title>
19+
<PackageReadmeFile>README.md</PackageReadmeFile>
20+
<Copyright>Corp. 2018 luacloud</Copyright>
1821

1922
</PropertyGroup>
2023

@@ -32,6 +35,10 @@
3235
<Pack>True</Pack>
3336
<PackagePath></PackagePath>
3437
</None>
38+
<None Include="..\..\README.md">
39+
<Pack>True</Pack>
40+
<PackagePath>\</PackagePath>
41+
</None>
3542
</ItemGroup>
3643

3744
</Project>

0 commit comments

Comments
 (0)