NewLife.RocketMQ使用
百度连接rocketmq,然后报错了
找到了内网的ip地址,报
然后百度,修改brokerIP1的地址
:https://www.cnblogs.com/smail-bao/p/6905460.html;
https://blog.csdn.net/chuanhejiu9868/article/details/100956381
#进入rocketmq根目录 cd incubator-rocketmq/distribution/target/apache-rocketmq #编写配置文件,并写好配置 echo "brokerIP1=10.19.73.64的外网IP" > broker.properties #启动 mqnamesrv nohup sh bin/mqnamesrv & #重点:mrbroker 启动时通过 -c 加载配置文件 nohup sh bin/mqbroker -n ${namesrvIp}:9876 -c /opt/rocketmq/incubator-rocketmq/distribution/target/apache-rocketmq/broker.properties & 这里我们停止RocketMQ使用 ./bin/mqshutdown broker ./bin/mqshutdown namesrv
然后发布消息
再测试下消费
代码
static void Main(string[] args) { //XTrace.UseConsole(); //ConsumerData(); for (int i = 0; i <= 10; i++) { Console.WriteLine("请发布消息"); string wxMessage = Console.ReadLine(); ProducerData(wxMessage, i.ToString()); } Console.Read(); } /// <summary> /// 生产者 /// </summary> private static void ProducerData(string wxMessage, string key) { try { var producer = new Producer { Topic = "nx_test1", NameServerAddress = "ip:9876", Group = "测试", //Log = XTrace.Log, }; producer.Start(); //发送消息方式一,可以设置key var msg = new Message() { BodyString = wxMessage, Keys = key, Tags = "TagC", Flag = 0, WaitStoreMsgOK = true }; var data = producer.Publish(msg); //var data = producer.PublishAsync(msg); Console.WriteLine(JsonConvert.SerializeObject(data)); //producer.Publish(JsonConvert.SerializeObject(wxMessage), "测试", "111", 6000); Console.WriteLine("生产者" + JsonConvert.SerializeObject(msg)); //释放连接 producer.Dispose(); } catch (Exception ex) { Console.WriteLine("写入消息队列出错:" + ex.ToString()); } } /// <summary> /// 消费者 /// </summary> private static void ConsumerData() { var consumer = new Consumer { //Server = "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet", //AccessKey = "LTAINsp1qKfO61c5", //SecretKey = "BvX6DpQffUz8xKIQ0u13EMxBW6YJmp", Topic = "nx_test1", Group = "测试", NameServerAddress = "ip:9876", BatchSize = 1, //Log = XTrace.Log, }; 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()}】,内容>{System.Text.Encoding.Default.GetString(item.Body)}"; Console.WriteLine(msg); } // return false;//通知消息队:不消费消息 return true; //通知消息队:消费了消息 }; consumer.Start(); }