Skip to content

Commit

Permalink
feat: add rabbitmq demo
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoy8 committed Nov 26, 2023
1 parent 62cdfb3 commit 7a07b85
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
13 changes: 12 additions & 1 deletion Blog.Core.Api/Blog.Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions Blog.Core.Api/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using System.Text;

namespace Blog.Core.Controllers
{
Expand All @@ -39,6 +42,7 @@ public class ValuesController : BaseApiController
private readonly IPasswordLibServices _passwordLibServices;
readonly IBlogArticleServices _blogArticleServices;
private readonly IHttpPollyHelper _httpPollyHelper;
private readonly IRabbitMQPersistentConnection _persistentConnection;
private readonly SeqOptions _seqOptions;

/// <summary>
Expand All @@ -52,6 +56,7 @@ public class ValuesController : BaseApiController
/// <param name="user"></param>
/// <param name="passwordLibServices"></param>
/// <param name="httpPollyHelper"></param>
/// <param name="persistentConnection"></param>
/// <param name="seqOptions"></param>
public ValuesController(IBlogArticleServices blogArticleServices
, IMapper mapper
Expand All @@ -60,6 +65,7 @@ public ValuesController(IBlogArticleServices blogArticleServices
, IRoleModulePermissionServices roleModulePermissionServices
, IUser user, IPasswordLibServices passwordLibServices
, IHttpPollyHelper httpPollyHelper
, IRabbitMQPersistentConnection persistentConnection
, IOptions<SeqOptions> seqOptions)
{
// 测试 Authorize 和 mapper
Expand All @@ -77,9 +83,69 @@ public ValuesController(IBlogArticleServices blogArticleServices
_blogArticleServices = blogArticleServices;
// httpPolly
_httpPollyHelper = httpPollyHelper;
_persistentConnection = persistentConnection;
_seqOptions = seqOptions.Value;
}

/// <summary>
/// 测试Rabbit消息队列发送
/// </summary>
[HttpGet]
[AllowAnonymous]
public void TestRabbitMqPublish()
{
if (!_persistentConnection.IsConnected)
{
_persistentConnection.TryConnect();
}
using var channel = _persistentConnection.CreateModel();
var message = " < i am a sender! > ";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
channel.BasicPublish(
exchange: "blogcore",
routingKey: "eventName",
mandatory: true,
basicProperties: properties,
body: body);
}

/// <summary>
/// 测试Rabbit消息队列订阅
/// </summary>
[HttpGet]
[AllowAnonymous]
public void TestRabbitMqSubscribe()
{
if (!_persistentConnection.IsConnected)
{
_persistentConnection.TryConnect();
}

string QueueName = "testq";
using var channel = _persistentConnection.CreateModel();
var consumer = new AsyncEventingBasicConsumer(channel);

consumer.Received += new AsyncEventHandler<BasicDeliverEventArgs>(
async (a, b) =>
{
var Headers = b.BasicProperties.Headers;
var msgBody = b.Body.ToArray();
bool Dealresult = await Dealer(b.Exchange, b.RoutingKey, msgBody, Headers);
if (Dealresult) channel.BasicAck(b.DeliveryTag, false);
else channel.BasicNack(b.DeliveryTag, false, true);
}
);
channel.BasicConsume(QueueName, false, consumer);
}

private async Task<bool> Dealer(string exchange, string routingKey, byte[] msgBody, IDictionary<string, object> headers)
{
await Task.CompletedTask;
Console.WriteLine("我是消费者,这里消费了一条信息是:" + Encoding.UTF8.GetString(msgBody));
return true;
}

[HttpGet]
public MessageModel<List<ClaimDto>> MyClaims()
{
Expand Down
15 changes: 8 additions & 7 deletions Blog.Core.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
"InstanceName": "" //前缀
},
"RabbitMQ": {
"Enabled": false,
"Connection": "118.25.251.13",
"UserName": "",
"Password": "!",
"RetryCount": 3
"Enabled": true,
"Connection": "101.35.125.157",
"UserName": "admin",
"Password": "admin",
"Port": "5672",
"RetryCount": 2
},
"Kafka": {
"Enabled": false,
Expand Down Expand Up @@ -181,8 +182,8 @@
"Audience": {
"Secret": "sdfsdfsrty45634kkhllghtdgdfss345t678fs", //不要太短,16位+
"SecretFile": "C:\\my-file\\blog.core.audience.secret.txt", //安全。内容就是Secret
"Issuer": "Blog.Core",
"Audience": "wr"
"Issuer": "Blog.Core", //这个值一定要在自己的项目里修改!!
"Audience": "wr" //这个值一定要在自己的项目里修改!!
},
"Mongo": {
"ConnectionString": "mongodb://nosql.data",
Expand Down

0 comments on commit 7a07b85

Please sign in to comment.