springboot整合rabbitmq入门(一)

网上关于rabbitmq的介绍挺多的,这里就不来介绍rabbitmq了。直接开始开始介绍springboot整合rabbitmq

首先创建springboot项目并引入rabbitmq的jar包,web和test包

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

在application.yml里面配置rabbitmq的信息

1.Fanout Exchange 模式

创建fanoutReceive和fanoutSend类

 fanoutSend类:

@Component
public class FanoutSend {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(){
        String context = "fanout msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
    }
}

 因为创建了两个对列,所以说有两个fanoutReceive类:

@RabbitListener注解表示监听的那个对列

@Component
@RabbitListener(queues = "fanout.a")
public class FanoutReceiveA {

    @RabbitHandler
    public void receive(String content){
        System.out.println("FanoutA "+content);
    }
}

测试类

控制台打印的结果

在绑定对列和交换机时的注意点

 绑定时,Binding bindingExchangeMessageB(Queue FbQueue, FanoutExchange fanoutExchange)里面的FbQueue和fanoutExchange分别是对列和交换机的方法名称,而不是创建的name。这里踩过坑,记录一下。

当发送端交换机name写错时,不会报错会发送,但是接受端不会接受到任何信息

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值