Skip to content

Commit cb41ccb

Browse files
committed
try to use event bus in project
尝试使用事件总线
1 parent 9a68985 commit cb41ccb

30 files changed

Lines changed: 268 additions & 41 deletions

Blog.Core.Api/Blog.Core.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core.Api/Controllers/ValuesController.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Blog.Core.Common.HttpContextUser;
44
using Blog.Core.Common.HttpRestSharp;
55
using Blog.Core.Common.WebApiClients.HttpApis;
6+
using Blog.Core.EventBus;
7+
using Blog.Core.EventBus.EventHandling;
68
using Blog.Core.Filter;
79
using Blog.Core.IServices;
810
using Blog.Core.Model;
@@ -13,6 +15,7 @@
1315
using System;
1416
using System.Collections.Generic;
1517
using System.ComponentModel.DataAnnotations;
18+
using System.Threading;
1619
using System.Threading.Tasks;
1720

1821
namespace Blog.Core.Controllers
@@ -132,15 +135,32 @@ public async Task<MessageModel<ResponseEnum>> Get()
132135
return data;
133136
}
134137

138+
/// <summary>
139+
/// 测试Redis消息队列
140+
/// </summary>
141+
/// <param name="_redisBasketRepository"></param>
142+
/// <returns></returns>
135143
[HttpGet]
136144
[AllowAnonymous]
137145
public async Task RedisMq([FromServices] IRedisBasketRepository _redisBasketRepository)
138146
{
139147
var msg = $"这里是一条日志{DateTime.Now}";
140148
await _redisBasketRepository.ListLeftPushAsync(RedisMqKey.Loging, msg);
149+
}
150+
151+
/// <summary>
152+
/// 测试RabbitMQ事件总线
153+
/// </summary>
154+
/// <param name="_eventBus"></param>
155+
/// <param name="blogId"></param>
156+
/// <returns></returns>
157+
[HttpGet]
158+
[AllowAnonymous]
159+
public void EventBusTry([FromServices] IEventBus _eventBus, string blogId = "1")
160+
{
161+
var blogDeletedEvent = new BlogDeletedIntegrationEvent(blogId);
141162

142-
//var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
143-
//_eventBus.Publish(evt);
163+
_eventBus.Publish(blogDeletedEvent);
144164
}
145165

146166
/// <summary>
@@ -167,7 +187,7 @@ public ActionResult<string> Get(int id)
167187
/// <returns></returns>
168188
[HttpGet]
169189
[Route("/api/values/RequiredPara")]
170-
public string RequiredP([Required]string id)
190+
public string RequiredP([Required] string id)
171191
{
172192
return id;
173193
}
@@ -221,7 +241,7 @@ public string Destination()
221241
/// <param name="id">独立参数</param>
222242
[HttpPost]
223243
[AllowAnonymous]
224-
public object Post([FromBody] BlogArticle blogArticle, int id)
244+
public object Post([FromBody] BlogArticle blogArticle, int id)
225245
{
226246
return Ok(new { success = true, data = blogArticle, id = id });
227247
}

Blog.Core.Api/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public void ConfigureServices(IServiceCollection services)
6363
services.AddHttpApi();
6464
services.AddRedisInitMqSetup();
6565

66+
services.AddRabbitMQSetup();
67+
services.AddEventBusSetup();
68+
6669
// 授权+认证 (jwt or ids4)
6770
services.AddAuthorizationSetup();
6871
if (Permissions.IsUseIds4)
@@ -196,8 +199,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyContex
196199
app.UseSeedDataMildd(myContext, Env.WebRootPath);
197200
// 开启QuartzNetJob调度服务
198201
app.UseQuartzJobMildd(tasksQzServices, schedulerCenter);
199-
//服务注册
202+
// 服务注册
200203
app.UseConsulMildd(Configuration, lifetime);
204+
// 事件总线,订阅服务
205+
app.ConfigureEventBus();
206+
201207
}
202208

203209
}

Blog.Core.Api/appsettings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"Enabled": false,
2525
"Connection": "118.25.251.13",
2626
"UserName": "",
27-
"Password": "!"
27+
"Password": "!",
28+
"RetryCount": 3
29+
},
30+
"EventBus": {
31+
"Enabled": false,
32+
"SubscriptionClientName": "Blog.Core"
2833
},
2934
"AppSettings": {
3035
"RedisCachingAOP": {

Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Name": "博客管理",
44
"JobGroup": "博客测试组",
55
"TriggerType": 1,
6-
"Cron": "0 */1 * * * ?",
6+
"Cron": "0 */10 * * * ?",
77
"AssemblyName": "Blog.Core.Tasks",
88
"ClassName": "Job_Blogs_Quartz",
99
"Remark": "",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Blog.Core.Common.Extensions
5+
{
6+
public static class GenericTypeExtensions
7+
{
8+
public static string GetGenericTypeName(this Type type)
9+
{
10+
var typeName = string.Empty;
11+
12+
if (type.IsGenericType)
13+
{
14+
var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray());
15+
typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>";
16+
}
17+
else
18+
{
19+
typeName = type.Name;
20+
}
21+
22+
return typeName;
23+
}
24+
25+
public static string GetGenericTypeName(this object @object)
26+
{
27+
return @object.GetType().GetGenericTypeName();
28+
}
29+
}
30+
}

Blog.Core.Common/Helper/JsonHelper.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public static string GetJSON<T>(object obj)
2424
result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
2525
}
2626
}
27-
catch (Exception ex)
27+
catch (Exception)
2828
{
29-
throw ex;
29+
throw;
3030
}
3131
return result;
3232
}
@@ -52,9 +52,8 @@ public string JSON<T>(List<T> vals)
5252
}
5353
}
5454
}
55-
catch (Exception ex)
55+
catch (Exception)
5656
{
57-
throw ex;
5857
}
5958

6059
return st.ToString();
@@ -93,9 +92,8 @@ public string JSON1<SendData>(List<SendData> vals)
9392
}
9493
}
9594
}
96-
catch (Exception ex)
95+
catch (Exception)
9796
{
98-
throw ex;
9997
}
10098

10199
return st.ToString();

Blog.Core.Common/HttpRestSharp/HttpHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public static T PostApi<T>(string url, object body = null)
5050
IRestRequest queest = new RestRequest();
5151
queest.Method = Method.POST;
5252
queest.AddHeader("Accept", "application/json");
53-
queest.RequestFormat = DataFormat.Json;
54-
queest.AddBody(body); // 可以使用 JsonSerializer
53+
queest.AddJsonBody(body); // 可以使用 JsonSerializer
5554
var result = client.Execute(queest);
5655
if (result.StatusCode != HttpStatusCode.OK)
5756
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
8+
<ItemGroup>
9+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
10+
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="5.0.0" />
11+
12+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
13+
14+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
15+
<PackageReference Include="Polly" Version="7.2.1" />
16+
<PackageReference Include="RabbitMQ.Client" Version="6.2.1" />
17+
</ItemGroup>
18+
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\Blog.Core.Common\Blog.Core.Common.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

Blog.Core.Extensions/RabbitMQPersistent/InMemoryEventBusSubscriptionsManager.cs renamed to Blog.Core.EventBus/EventBusSubscriptions/InMemoryEventBusSubscriptionsManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5-
namespace Blog.Core.Extensions.RabbitMQPersistent
5+
namespace Blog.Core.EventBus
66
{
77
public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
88
{

0 commit comments

Comments
 (0)