SpringBoot整合RocketMQ示例

上一篇 << 下一篇 >>>RocketMQ保证顺序消费demo


消息队列rocketmq是Apache旗下的开源项目(原是Alibaba开源的项目),当springboot盛行后,Apache团队开源了rocketmq-spring来帮助我们在springboot中快速集成rocketmq,只需引入rocketmq-spring-boot-starter即可。
rocketmq-spring不仅实现了自动配置功能,最主要还是封装了spring-message风格的rocketmq操作,使得我们在spring中,能够像使用rabbitmq一样方便快捷的使用rocketmq。

1.加入依赖包


    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.apache.rocketmq
        rocketmq-spring-boot-starter
        2.0.3
    
    
        org.projectlombok
        lombok
    

2.配置修改

rocketmq:
  #只要配置nameserver即可
  name-server: 10.211.55.16:9876;
  producer:
    #生产者必须要有group,要不然启动不了
    group: tt1226
    #超时时间设置长一点,要不然会报错sendDefaultImpl call timeout
    send-message-timeout: 50000

3.核心代码

@RestController
public class ProducerController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/sendMsg")
    public String sendMsg() {
        OrderEntity orderEntity = new OrderEntity("123456","蚂蚁课堂会员");
        rocketMQTemplate.convertAndSend("myTest",JSON.toJSONString(orderEntity));
        return "success";
    }
}


@Service
@RocketMQMessageListener(topic = "myTest", consumerGroup = "myTest")
public class OrdeConsumer implements RocketMQListener {
    @Override
    public void onMessage(String o) {
        System.out.println("o:" + JSON.toJSONString(o));
    }
}

推荐阅读:
<<<消息中间件的核心思想
<<<消息中间件常见问题汇总
<<<基于Netty简单手写消息中间件思路
<<<消息队列常用名词与中间件对比
<< << << << << << << << << << << << << << << << << << << << << << << << << << << << << << << <<

你可能感兴趣的:(SpringBoot整合RocketMQ示例)