spirng cloud stream rabbitmq

本文介绍如何使用Spring Cloud Stream与RabbitMQ进行消息传递,包括生产者和消费者的配置,以及如何发送和接收消息。通过配置文件绑定交换机、路由等关键参数,实现消息的高效传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本Edgware.SR4

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

application.properties

spring.rabbitmq.port=5672
spring.rabbitmq.virtualHost=/
spring.rabbitmq.host=192.168.74.164
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

生产者

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;


public interface RefreshChannelOutput {

    String  OUTPUT = "refresh_channel_output";

    @Output(OUTPUT)
    MessageChannel messageOutput();
}

生产者添加配置文件
默认情况是topic 模式,绑定交换机,路由

spring.cloud.stream.bindings.refresh_channel_output.destination=ticket_bus_exchange
spring.cloud.stream.rabbit.bindings.refresh_channel_output.producer.routing-key-expression='refresh_channel'

消息发送,使用直接注入到其它类就行了
@Autowired
private RabbitmqProducer sendMessage;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;


@EnableBinding({ RefreshChannelOutput.class})
public class RabbitmqProducer {

    @Autowired
    @Output(RefreshChannelOutput.OUTPUT)
    private MessageChannel refreshChannelChannel;


    public void sendResChannel(String message) {
        refreshChannelChannel.send(MessageBuilder.withPayload(message).build());
    }
}

消费者
添加配置文件
绑定交换机,路由,消费者个数,消息格式

spring.cloud.stream.bindings.refresh_channel.destination=ticket_bus_exchange
spring.cloud.stream.bindings.refresh_channel.contentType=text/plain
spring.cloud.stream.bindings.refresh_channel.consumer.concurrency=1
spring.cloud.stream.rabbit.bindings.refresh_channel.consumer.bindingRoutingKey=refresh_channel

添加类

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;


public interface RefreshChannelInput {

    String INPUT = "refresh_channel";

    @Input(RefreshChannelInput.INPUT)
    SubscribableChannel input();
}

消息接收


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

import com.hhly.ticket.cms.rabbit.input.MonitorInput;
import com.hhly.ticket.cms.rabbit.input.RefreshChannelInput;



@EnableBinding({RefreshChannelInput.class})

public class RabbitmqReceiver {

    @StreamListener(RefreshChannelInput.INPUT)
    public void refreshCahnnel(Object playload) {
        System.out.println(playload.toString());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值