FAQ
采用pull模型,消息的实时性有保证吗?
Metaq在消费端采用pull的模型,consumer主动去broker拉取数据,而不是类似notify那样由broker主动push数据给消费者。可能很多人担心采用pull模型后,会不会消息的实时性降低了,从发送到消费的整个时间周期拉长了。
实际上,meta中消息的实时性受很多因素影响,不能简单地说实时性一定会降低,主要影响因素如下
- broker上配置的批量force消息的阈值,默认是1000条force一次。这个值越大,则实时性越低。
- 消费者每次抓取的数据大小,这个值越大,则实时性越低,但是吞吐量越高。
- Topic的分区数目对实时性也有较大影响,分区数目越多,则磁盘压力越大,导致消息投递的实时性降低。
- 消费者重试抓取的时间间隔,越长则延迟越严重。
- 消费者抓取数据的线程数
可见,消息实时性在meta里受到很多因素的影响,meta可以让用户自己决定如何在响应性和吞吐量之间做平衡,通过配置来合理设置参数,达到应用方需要的实时性,实际测试,消息消费的延迟可以在几毫秒到几秒之间。
Metaq怎么做到环境隔离?
有的朋友可能想搭建自己的meta开发和测试环境,不想使用日常的。这也完全可以,meta环境隔离的主要问题是zookeeper环境的隔离,你可以搭建一台自己的zookeeper,然后配置下meta的broker使用你自己的zookeeper,就可以完全跟日常环境隔离开。这是在服务端server.properties中配置
#以下为zk配置,可以为空,为空将从diamond获取,不为空则优先使用下列配置 #zk的服务器列表 zk.zkConnect=localhost:2181 #zk心跳超时,单位毫秒,默认30秒 zk.zkSessionTimeoutMs=30000 #zk连接超时时间,单位毫秒,默认30秒 zk.zkConnectionTimeoutMs=30000 #zk数据同步时间,单位毫秒,默认5秒 zk.zkSyncTimeMs=5000
如果你不想写死zk配置在服务端,也可以在diamond里配置上述的这些参数,然后在broker的server.properties配置下使用的diamond的dataId和group,meta会使用diamond client获取zk配置
#zk在diamond中配置存储的dataId diamondZKDataId=Metaq.zkConfig #zk在diamond中配置存储的group diamondZKGroup=DEFAULT_GROUP
常见错误及处理办法
a.客户端发送消息时,报错: There is no aviable partition for topic xiafei-test-1,maybe you don't publish it at first?
答:
- 客户端发消息前有没有调用过publish? 如果没有则在发送消息前调用publish方法。
- server端是否有注册过对应的topic,如果没有注册过topic先在server端注册该topic。 在conf目录下topic.ini(老版本在server.ini)文件中添加topic的配置,并重启server(bin目录start.sh)或者重载(tool包bin目录下reloadconfig.sh)
消息过滤
metaq2.1.3版本开始支持消息过滤的功能。消息的生产者可以设置消息类型,消费者可以针对消息类型选择性的消费。
实现原理
消息生产者:每条消息可以设置一个消息类型,客户端中的消息类型借用attribute字段。
消息消费者:客户端向服务器汇报自己需要的消息类型列表。
metaq 服务器:客户端使用FetchCommand拉取消息的时候,服务器根据客户端订阅的消息类型过滤消息,过滤服务端的消息,把过滤后的消息发送给客户端,保证客户端只接收到订阅的消息类型。
示例代码
生产者示例:
// New session factory final MessageSessionFactory sessionFactory = new MetaMessageSessionFactory(new MetaClientConfig()); // create producer final MessageProducer producer = sessionFactory.createProducer(); // publish topic final String topic = "meta-test-20"; producer.publish(topic); final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = null; while (true) { System.out.println("type message content:"); line = reader.readLine(); Message message = new Message(topic, line.getBytes()); System.out.println("type message filter:"); line = reader.readLine(); message.setAttribute(line);//通过attribute字段设置消息类型 // send message final SendResult sendResult = producer.sendMessage(message); // check result if (!sendResult.isSuccess()) { System.err.println("Send message failed,error message:" + sendResult.getErrorMessage()); } else { System.out.println("Send message successfully,sent to " + sendResult.getPartition()); } }
消费者示例:
// New session factory final MessageSessionFactory sessionFactory = new MetaMessageSessionFactory(buildMetaClientConfig()); // subscribed topic final String topic = "meta-test-20"; // consumer group final String group = "meta-vintage-x-4"; // create consumer final MessageConsumer consumer = sessionFactory.createConsumer(new ConsumerConfig(group)); String[] types = {"type1","type2"};//需要接受的消息类型列表 // subscribe topic consumer.subscribe(topic, 1024 * 1024, new MessageListener() { public void recieveMessages(final Message message) { System.out.println("newId:" + message.getMsgNewId() + "\t attribute is:" + message.getAttribute()); } public Executor getExecutor() { return null; } }, types); // complete subscribe consumer.completeSubscribe();
注意事项
1.attribute字段,可能原先被使用用作分区选择器的key,attribute字段是会被存储在服务端的数据,如果只是用过分区选择器用,推荐使用message.setArg(Object arg)。这个字段不会被发送到服务器存储
2.集群中订阅信息的版本是客户端启动的时间,以时间最大的为准。如果应用修改了过滤条件重启,即使只重启了一台,服务端会强制刷新同一分组,同一topic所有机器的订阅关系。
3.消息类型只允许英文字符,数字,下划线。其他的特殊字符和中文不支持,订阅的时候会抛出异常。
