以下是springcloud整合stream-rabbit:3.0.0的使用案例:

  1. 添加依赖

在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    <version>3.0.0</version>
</dependency>
  1. 配置文件

在application.yml文件中添加以下配置:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

  cloud:
    stream:
      rabbit:
        bindings:
          input:
            destination: test-queue
            content-type: application/json
          output:
            destination: test-queue
            content-type: application/json
  1. 创建消息生产者

创建一个消息生产者,用于发送消息到队列中。

@EnableBinding(Source.class)
public class MessageProducer {

    @Autowired
    private Source source;

    public void sendMessage(String message) {
        source.output().send(MessageBuilder.withPayload(message).build());
    }

}
  1. 创建消息消费者

创建一个消息消费者,用于从队列中接收消息。

@EnableBinding(Sink.class)
public class MessageConsumer {

    @StreamListener(Sink.INPUT)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }

}
  1. 测试

在测试类中使用上述生产者发送消息,然后使用消费者接收消息。

@RunWith(SpringRunner.class)
@SpringBootTest
public class StreamRabbitTest {

    @Autowired
    private MessageProducer messageProducer;

    @Test
    public void testSendAndReceiveMessage() {
        messageProducer.sendMessage("Hello RabbitMQ!");
    }

}

以上就是springcloud整合stream-rabbit:3.0.0的使用案例

springcloud 整合stream-rabbit300使用案例

原文地址: https://www.cveoy.top/t/topic/cRaP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录