概述
MQ消息队列的SDK目前支持Java、C/C++及.NET三种语言,关于.NET SDK的使用目前因为版本更新的问题,用户在使用的过程中多少会出现这样或那样的问题,特别是SDK中example的使用问题。本文主要介绍.NET SDK的使用注意事项及用户可能遇到的问题。
准备工作
1、SDK下载 下载地址链接,建议下载最新版本的SDK;
2、加载相关文件到已经创建的.NET控制台项目,配置参考链接;
3、配置管理器示例:
4、如果找不到x64,可以通过如下方式新建:
示例程序运行注意事项
1、基本信息的录入,具体创建方法可以在管理门户完成,注意绝大部分的用户测试环境都是在本地,所以请在公网区域创建Topic、生产者及消费者;
2、SDK sample 默认代码的运行测试文件是: ConsumerAndProducerForEx.cs,默认的示例程序并未给出ONSAddr的设置,且默认指向的是内网的地址,所以用户完成了规定参数的配置后仍然无法连通,所以需要添加ONSAddr参数的配置,参数对应的是生产者或消费者的接入点,代码修改示例如下:
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
factoryInfo.setFactoryProperty(ONSFactoryProperty.ONSAddr, "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
dll文件的复制粘贴问题
直接复制SDK->lib->x64下的文件到项目的bin->x64->Debug。
测试运行效果
新版本SDK使用参考
截止2019.2.16,目前最新的SDK版本为1.1.3。基本配置方式与之前一致。只是在参数设置的地方略有不同,示例如下:
private static ONSFactoryProperty getFactoryProperty()
{
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "http://MQ_INST_1848217816617278_BaQUuiNE.mq-internet-access.mq-internet.aliyuncs.com:80");
factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, Ons_AccessKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, Ons_SecretKey);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, Ons_ConsumerId);
factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, Ons_ProducerID);
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, Ons_Topic);
return factoryInfo;
}
中文编码处理问题
处理思路:对发送的消息进行base64编码,消费的消息进行base64解码。
using System;
using System.Text;
using System.Threading;
using ons;
namespace test
{
/// <summary>
/// 消费监听类
/// </summary>
public class MyMsgListener : MessageListener
{
public MyMsgListener()
{
}
~MyMsgListener()
{
}
public override ons.Action consume(Message value, ConsumeContext context)
{
Byte[] text = Encoding.Default.GetBytes(value.getBody());
string s = System.Text.Encoding.UTF8.GetString(text, 0, text.Length);
Console.WriteLine(base64tstring.UnBase64String(s));
return ons.Action.CommitMessage;
}
}
/// <summary>
/// base64编码解码类
/// </summary>
public class base64tstring {
public static string ToBase64String(string value)
{
if (value == null || value == "")
{
return "";
}
byte[] bytes = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(bytes);
}
public static string UnBase64String(string value)
{
if (value == null || value == "")
{
return "";
}
byte[] bytes = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(bytes);
}
}
class ConsumerAndProducerForEx
{
static void Main(string[] args)
{
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "******");
factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "******");
factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, "GID_test");
factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "GID_test");
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "newtopictest");
factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "http://******.mq-internet-access.mq-internet.aliyuncs.com:80");
factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "E://代码//示例//消息队列//阿里消息MQ//AliMQTest//log//AliSend");
//// 消息发送
Producer producer = ONSFactory.getInstance().createProducer(factoryInfo);
producer.start();
string myString = "Example message body测试"; //中英文混合字符
myString = base64tstring.ToBase64String(myString);
Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", myString);
msg.setKey(Guid.NewGuid().ToString());
for (int i = 0; i < 10; i++)
{
try
{
try
{
SendResultONS sendResult = producer.send(msg);
Console.WriteLine("send success {0}", sendResult.getMessageId());
}
catch (Exception ex)
{
Console.WriteLine("Error message: " + ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("send failure{0}", ex.ToString());
}
}
producer.shutdown();
// 创建消费者实例
PushConsumer consumer = ONSFactory.getInstance().createPushConsumer(factoryInfo);
Console.WriteLine("开始消费消息:");
// 订阅 Topics
consumer.subscribe("newtopictest", "*", new MyMsgListener());
// 启动客户端实例
consumer.start();
//该设置仅供 demo 使用,实际生产中请保证进程不退出
Thread.Sleep(300000);
Console.ReadLine();
}
}
}