MQ助手 - 简化配置,注解带飞!
配置灵活,基于pulsar包,没有改变任何框架结构,只为简化; 简单注解,即可实现消息MQ
implementation("cool.doudou:mq-assistant-boot-starter:latest")
依赖spring自动注入PulsarClient,配置属性如下:
pulsar:
service-url: pulsar://127.0.0.1:6650
subscription-name: sub-celery
subscription-type: Shared
消息订阅
- 生产者与topic进行关联绑定
/**
* 生产者主题绑定
*/
@MqProducer(topics = {"celery"})
@Component
public class MqComponent {
}
- 消费者与topic进行关联绑定,注意:每个消费者须绑定一个subscription-name后才能进行消费
/**
* 消费者主题绑定
*/
@Component
public class MqComponent {
@MqConsumer(topics = {"celery"})
public void receive(String topic, byte[] msg) {
System.out.println("consumer: topic[" + topic + "] => " + new String(msg));
}
}
消息发送
- send():发送
- sendAsync():异步发送
/**
* 消息发送
*/
@AllArgsConstructor
@Service
public class MqServiceImpl {
private MqHelper mqHelper;
public void test() {
// 同步
String msgId = mqHelper.send("celery", "hello");
System.out.println("send: " + msgId);
// 异步
mqHelper.sendAsync("celery", "您好Async", System.out::println);
// 同步
String msgId = mqHelper.send("celery", new byte[]{0x01, 0x02, 0x03, 0x04});
System.out.println("send: " + msgId);
// 异步
mqHelper.sendAsync("celery", new byte[]{0x01, 0x02, 0x03, 0x04}, System.out::println);
}
}
欢迎提出宝贵意见,不断完善 MQ-Assistant