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写错时,不会报错会发送,但是接受端不会接受到任何信息

猜你喜欢

转载自blog.csdn.net/java_chegnxuyuan/article/details/89929747