RabbitMQ:第三章:Springboot集成RabbitMQ(直连模式,工作队列模式,发布订阅模式,路由模式,通配符模式)

1.7

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-amqp

org.springframework.boot

spring-boot-starter-web

junit

junit

4.11

test

springboot_rabbitmq

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

2.application.properties配置

server.port=8080

#spring.rabbitmq.host=localhost

spring.rabbitmq.port=5672

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.addresses=110.42.239.246

spring.rabbitmq.virtual-host=springboot

#spring.rabbitmq.addresses=110.42.239.246:5672,110.42.239.247:5672,110.42.239.248:5672

说明:这里免费提供rabbitmq连接方式给大家使用学习

3.config配置

HelloWorldConfig

package com.sky.springbootrabbitmqmodule.config;

import org.springframework.amqp.core.Queue;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

  • HelloWorld rabbitmq课上讲解的第一个工作模式

  • 直连模式只需要声明队列,所有消息都通过队列转发。

  • 无需设置交换机

*/

@Configuration

public class HelloWorldConfig {

@Bean

public Queue setQueue() {

return new Queue(“helloWorldqueue”);

}

}

FanoutConfig

package com.sky.springbootrabbitmqmodule.config;

import org.springframework.amqp.core.Binding;

import org.springframework.amqp.core.BindingBuilder;

import org.springframework.amqp.core.FanoutExchange;

import org.springframework.amqp.core.Queue;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

  • Fanout模式需要声明exchange,并绑定queue,由exchange负责转发到queue上。

  • 广播模式 交换机类型设置为:fanout

*/

@Configuration

public class FanoutConfig {

//声明队列

@Bean

public Queue fanoutQ1() {

return new Queue(“fanout.q1”);

}

@Bean

public Queue fanoutQ2() {

return new Queue(“fanout.q2”);

}

//声明exchange

@Bean

public FanoutExchange setFanoutExchange() {

return new FanoutExchange(“fanoutExchange”);

}

//声明Binding,exchange与que

你可能感兴趣的:(程序员,java-rabbitmq,rabbitmq,spring,boot)