Rocket MQ学习笔记(4)——.net core 连接rocketmq,并且生产和消费mq (NewLift.RocketMQ)

NewLift.RocketMQ

Github地址https://github.com/NewLifeX/NewLife.RocketMQ
Nuget地址:https://www.nuget.org/packages/NewLife.RocketMQ/1.5.2021.410
或者在Nuget上自己搜索NewLife.RocketMQ。

Rocket Producer(生产者)

用于连接mq,并且生产数据

using NewLife.Log;
using NewLife.RocketMQ;
using System;
using System.Threading.Tasks;

namespace MQProducer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //mq对象
            using var mq = new Producer
            {
                //主题
                Topic = "lmi_datas",
                //服务地址
                NameServerAddress = "127.0.0.1:9876", 
            };

            mq.Start(); 
            int i = 1;
            //轮询发消息
            while (true)
            {
                var content = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss.fff"); 
                var message = new NewLife.RocketMQ.Protocol.Message()
                {
                    BodyString = content,
                    Keys = (i++).ToString(),
                    Tags = i % 2 == 0 ? "even" : "odd",
                    Flag = 0,
                    WaitStoreMsgOK = true
                };
                //发送消息(生产消息)
                var sr = mq.Publish(message);
                string log = $"发送成功的消息,内容>{content},MsgId={sr.MsgId},BrokerName= {sr.Queue.BrokerName} ,QueueId={sr.Queue.QueueId},QueueOffset= {sr.QueueOffset}";
                Console.WriteLine(log);
                Task.Delay(TimeSpan.FromSeconds(10)).Wait();
            }
        }
    }
}

RocketMQ Consumer(消费者)

连接指定主题lmi_datas(这个主题我是通过网页端创建的)

using System;
using System.Linq;
using NewLife;
using NewLife.RocketMQ;

namespace MQConsumer
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //测试消费消息
            var consumer = new NewLife.RocketMQ.Consumer
            {
                Topic = "lmi_datas", 
                NameServerAddress = "172.10.11.233:9876",
                //设置每次接收消息只拉取一条信息
                BatchSize = 1 
            };
            consumer.OnConsume = (q, ms) =>
            {
                string mInfo = $"BrokerName={q.BrokerName},QueueId={q.QueueId},Length={ms.Length}";
                Console.WriteLine(mInfo);
                foreach (var item in ms.ToList())
                {
                    string msg = $"消息:msgId={item.MsgId},key={item.Keys},产生时间【{item.BornTimestamp.ToDateTime()}】,内容>{item.Body.ToStr()}";
                    Console.WriteLine(msg);
                }
                //   return false;//通知消息队:不消费消息
                return true;		//通知消息队:消费了消息
            };

            consumer.Start();
            Console.WriteLine("消息接收测试");
            Console.ReadLine();
        } 
    }
}

运行效果
在这里插入图片描述

源码地址:https://github.com/LampardLiu/RocketMQTest

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值