C#采用rabbitMQ搭建分布式日志系统
网上对于java有很多开源的组件可以搭建分布式日志系统,我参考一些组件自己开发一套简单的分布式日志系
全部使用采用.NET进行开发,所用技术:MVC、EF、RabbitMq、MySql、Autofac
整体构架如下:
文章图片
主要采用的RabbitMq进行处理,下面我主要讲讲.NET下运用RabbitMq
1.RabbitMQ Erlang 的安装,网上很多介绍,我这里就不多少了,请参考https://jingyan.baidu.com/article/a17d5285173ce68098c8f2e5.html
备注:安装好后一定注意保持.erlang.cookie这个文件的一致,在用户目录和windows下面。
2.RabbitMQ是使用,先安装RabbitMQ.Client,在VS中程序管理装入
RabbitMQ的辅助类
文章图片
文章图片
/// /// RabbitMQ消息队列处理 /// public class RabbitMQHelper { /// /// rabbitMQ地址 /// private string HostName = "localhost"; //ConfigurationManager.AppSettings["RabbitMQHostName"]; /// /// 账号 /// private string UserName = "guest"; //ConfigurationManager.AppSettings["RabbitMQUserName"]; /// /// 密码 /// private string Password = "guest"; // ConfigurationManager.AppSettings["RabbitMQPassword"]; /// /// 创建ConnectionFactory /// ///private ConnectionFactory factory { get; set; }public RabbitMQHelper() { if (factory == null) { factory = new ConnectionFactory(); factory.HostName = HostName; factory.UserName = UserName; factory.Password = Password; } } public RabbitMQHelper(string UserName,string Password):base() { this.UserName = UserName; this.Password = Password; } public RabbitMQHelper(string UserName, string Password,string HostName) :base() { this.UserName = UserName; this.Password = Password; this.HostName = HostName; } /// /// 消息发送 /// /// /// /// public void SendMsg (TEntity entity,string MqName) { if (entity == null || string.IsNullOrEmpty(MqName)) return; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { bool durable = true; channel.QueueDeclare(MqName, durable, false, false, null); string message = Newtonsoft.Json.JsonConvert.SerializeObject(entity); //持久化队列消息 var properties = channel.CreateBasicProperties(); properties.Persistent = true; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish("", MqName, properties, body); } } } /// /// 接受消息 /// /// /// /// /// public void AcceptMsg (string MqName,out TEntity entity,Action action)where TEntity:class { entity =null; if (string.IsNullOrEmpty(MqName)) return; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { bool durable = true; channel.QueueDeclare(MqName, durable, false, false, null); //公平分发 channel.BasicQos(0, 1, false); var consumer = new QueueingBasicConsumer(channel); channel.BasicConsume(MqName, false, consumer); while (true) { var ea = consumer.Queue.Dequeue(); var body = ea.Body; var message = Encoding.UTF8.GetString(body); entity = Newtonsoft.Json.JsonConvert.DeserializeObject (message); Thread.Sleep(1000); channel.BasicAck(ea.DeliveryTag, false); action(); } } } }}
View Code 3.在生成服务器只需要调用SendMsg将日志文件写入消息队列
4.在日志处理服务器采用 NLog进行日志持久化处理,目前采用2台服务,采用RabbitMQ公平分发到2台日志服务器中,最后进行持久化处理,NLog的使用这里就不作说明
如果对于日志处理有更好的方案欢迎指出,谢谢
【C#采用rabbitMQ搭建分布式日志系统】转载于:https://www.cnblogs.com/kq123321/p/8350537.html
推荐阅读
- 六步搭建ES6语法环境
- (1)redis集群原理及搭建与使用(1)
- 桁架搭建有什么价值()
- VM|VM ware 的 harbor 私有仓库搭建 (Ubuntu16.04)
- PHP开发-Mac搭建ThinkPHP5.0
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- sentry搭建错误监控系统(二)
- 【乐高创意作品】超智能!这款乐高搭建的除草机!
- 8、Flask构建弹幕微电影网站-搭建后台页面-密码修改、主页控制面板
- 适用于小白(VSCode搭建Vue项目,最详细的搭建步骤哦)