rocketMq整合SpringBoot,使用RocketMQTemplate

rocketMq整合SpringBoot

pom

 	<dependency>
        <groupId>org.apache.rocketmq</groupId>
        <artifactId>rocketmq-spring-boot-starter</artifactId>
        <version>2.0.3</version>
    </dependency>

properties

rocketmq.name-server=192.168.205.34:9876
rocketmq.producer.group=demo_producer

Producers

@RestController
public class DemoProducers {
    @Autowired
    private RocketMQTemplate template;

    @RequestMapping("/producer")
    public String producersMessage() {
        User user = new User("sharfine", "123456789");
        template.convertAndSend("demo-topic", user);
        return JSON.toJSONString(user);
    }
}

Consumers

同一consumerGroup会实现均衡负载,同Group只有一个消费者接收

不同consumerGroup每一个Group都会接收到一次

@Service
@RocketMQMessageListener(topic = "demo-topic", consumerGroup = "demo_consumer")
public class DemoConsumers1 implements RocketMQListener<user> {
    @Override
    public void onMessage(user user) {
        System.out.println("Consumers1接收消息:" + demoEntity.toString());
    }
}

@Service
@RocketMQMessageListener(topic = "demo-topic", consumerGroup = "demo_consumer")
public class DemoConsumers2 implements RocketMQListener<user> {
    @Override
    public void onMessage(user user) {
        System.out.println("Consumers2接收消息:" + demoEntity.toString());
    }
}

entity

@Data
public class user implements Serializable {
    String userName;
    String userId;

    public user(){

    }

    public user(String userName, String userId) {
        this.userName = userName;
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "demoEntity{" +
                "userName='" + userName + '\'' +
                ", userId='" + userId + '\'' +
                '}';
    }

测试
在这里插入图片描述
发送了三次请求
在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页