1、maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、application.yml 配置文件
rocketmq:
name-server: 106.13.1.171:9876;
producer:
group: MyProducer
server:
port: 8080
3、实体类
@Data
public class OrderEntity implements Serializable {
private String orderId;
private String orderName;
public OrderEntity(String orderId, String orderName) {
this.orderId = orderId;
this.orderName = orderName;
}
@Override
public String toString() {
return "OrderEntity{" +
"orderId='" + orderId + '\'' +
", orderName='" + orderName + '\'' +
'}';
}
}
4、生产者
@RestController
public class ProducerController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@RequestMapping("/sendMsg")
public String sendMsg() {
OrderEntity orderEntity = new OrderEntity("123456", "Hly");
rocketMQTemplate.convertAndSend("MyTopic", orderEntity);
return "success";
}
public static void main(String[] args) {
SpringApplication.run(ProducerController.class);
}
}
5、消费者
@Service
@RocketMQMessageListener(topic = "MyTopic", consumerGroup = "MyConsumerGroup")
public class OrdeConsumer implements RocketMQListener<OrderEntity> {
@Override
public void onMessage(OrderEntity o) {
System.out.println("o:" + o.toString());
}
}
6、访问
http://localhost:8080/sendMsg
—— 完
ABOUT
公众号:【星尘Pro】
github:https://github.com/huangliangyun