标签:model reac code 多重 local 账号 connect 业务 rem
下载安装 DotNetCore.RabbitMQ.Extensions
安装命令:Install-Package DotNetCore.RabbitMQ.Extensions
namespace TestCommon
{
public class TestDConnection : ConnectionChannelPool
{
public TestDConnection(ILogger<TestDConnection> logger) : base(logger)
{
}
public override RabbitMQOptions opt => new RabbitMQOptions
{
HostName = "localhost",
Port = 5672,
VHost = "testd.host",
UserName = "guest",
PassWord = "guest"
};
public override string ConnectionKey => nameof(TestDConnection);
}
}
namespace TestCommon
{
public class TestDPublish : PublishService
{
public TestDPublish(ILogger<TestDPublish> logger, IEnumerable<IConnectionChannelPool> connectionList) : base(logger, connectionList)
{
}
public override string ExchangeType => "direct";
public override string Exchange => "testd.ex";
public override string Queue => "testd.query";
public override string RoutingKey => "testd.key";
public override string ConnectionKey => nameof(TestDConnection);
public override string ServiceKey => nameof(TestDPublish);
}
}
namespace TestCommon
{
public class TestDConsumer : ConsumerService
{
ILogger logger;
public TestDConsumer(ILogger<TestDConsumer> logger, IEnumerable<IConnectionChannelPool> connectionList) : base(logger, connectionList)
{
this.logger = logger;
}
public override string Queue => "testd.query";
public override bool AutoAck => true;
public override string ServiceKey => nameof(TestDConsumer);
public override string ConnectionKey => nameof(TestDConnection);
public override void Received(object sender, BasicDeliverEventArgs e)
{
RemoveEnvironmentModel model = new RemoveEnvironmentModel();
try
{
model = JsonConvert.DeserializeObject<RemoveEnvironmentModel>(Encoding.UTF8.GetString(e.Body));
}
catch (Exception)
{
logger.LogError($"{ServiceKey}服务消费解析model错误");
throw;
}
Console.WriteLine($"消费者{ServiceKey}:收到消息");
}
}
}
IServiceCollection services = new ServiceCollection();
services.AddLogging();
//连接池
services.AddSingleton<IConnectionChannelPool, TestDConnection>();
//消费者
services.AddSingleton<IConsumerService, TestDConsumer>();
//生产者
services.AddSingleton<TestDPublish>();
//启动消费监听
IServiceProvider serviceProvider = services.BuildServiceProvider();
var consumerList = serviceProvider.GetService<IEnumerable<IConsumerService>>();
Task.Run(() =>
{
foreach (var e in consumerList)
{
e.Start();
}
});
标签:model reac code 多重 local 账号 connect 业务 rem
原文地址:https://www.cnblogs.com/shininggold/p/12050391.html