创建spring boot工程,添加pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
添加消息接收SinkReceiver
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.annotation.StreamListener; import org.springframework.cloud.stream.messaging.Sink; @EnableBinding(Sink.class) public class SinkReceiver { private static Logger logger= LoggerFactory.getLogger(SinkReceiver.class); @StreamListener(Sink.INPUT) public void receive(Object payload){ logger.info("Received: "+payload); } }
配置
spring.application.name=stream-hello spring.rabbitmq.host=10.202.203.29 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=123456
运行程序,打开rabbitmq监控界面,可以看到
推送消息
在控制台查看结果
原文地址:https://www.cnblogs.com/uptothesky/p/8288974.html
时间: 2024-11-13 04:04:40