SpringBoot integrates RabbitMQ demo

Rabbit Windows Installation
Tutorial

1. Producers

1. application.properties
configure the basic information of Rabbit

#rabbit 主机IP
spring.rabbitmq.host=127.0.0.1
#rabbit 端口
spring.rabbitmq.port=5672
#rabbit 账号 可自行创建 这里是默认的
spring.rabbitmq.username=guest
#rabbit 密码 可自行创建 这里是默认的
spring.rabbitmq.password=guest
#rabbit 虚拟机地址 可自行指定 这里是默认的
spring.rabbitmq.virtual-host=/
#主程序的端口 和 rabbit无关
server.port=8081

2. The pom file
imports two maven dependencies,
the first is the rabbit integrated with SpringBoot
and the second is the web dependency, which is used to facilitate sending messages

<!--RabbitMq-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--Web 方便用来发消息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3. Rabbit configuration class
The theme mode is used here, that is, the fuzzy matching mode.
The setting here is that as long as it is rabbit
.

@Configuration
public class RabbitConfig {
    
    

    public static final String EXCHANGE_NAME = "rabbit_exchange";
    public static final String QUEUE_NAME = "rabbit_queue";
    /**
     * 创建交换机
     * ExchangeBuilder有四种交换机模式
	 * Direct Exchange:直连交换机,根据Routing Key(路由键)进行投递到不同队列。
     * Fanout Exchange:扇形交换机,采用广播模式,根据绑定的交换机,路由到与之对应的所有队列。
     * Topic Exchange:主题交换机,对路由键进行模式匹配后进行投递,符号#表示一个或多个词,*表示一个词。
     * Header Exchange:头交换机,不处理路由键。而是根据发送的消息内容中的headers属性进行匹配。
     * durable 交换器是否持久化(false 不持久化,true 持久化)
     **/
    @Bean
    public Exchange rabbitExchange(){
    
    
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
	/**
	 * 创建队列
	 * durable 队列是否持久化 队列调用此方法就是持久化 可查看方法的源码
	 * deliveryMode 消息是否持久化(1 不持久化,2 持久化)
	 **/
    @Bean
    public Queue rabbitQueue(){
    
    
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
	/**
	* 绑定交换机和队列
	* bing 方法参数可以是队列和交换机
	* to 方法参数必须是交换机
	* with 方法参数是路由Key 这里是以rabbit.开头
	* noargs 就是不要参数的意思
	* 这个方法的意思是把rabbit开头的消息 和 上面的队列 和 上面的交换机绑定
	**/
    @Bean
    public Binding rabbitBinding(@Qualifier("rabbitExchange") Exchange exchange, @Qualifier("rabbitQueue") Queue queue){
    
    
        return BindingBuilder.bind(queue).to(exchange).with("rabbit.#").noargs();
    }

}

4. Controller
here sends information in the form of a web page call

@RestController
public class ProducerWebController {
    
    

    @Autowired
    ProducerService producerService;

    @RequestMapping("/sendMq")
    public String sendMq(@RequestParam("mq") String mq){
    
    
        return producerService.sendMq(mq);
    }

}

5. Service
injects RabbitTemplate convertAndSend in the service layer
, which is the method of message sending

public interface ProducerService {
    
    
    public String sendMq(String mq);
}
@Service
public class ProducerServiceImpl implements ProducerService {
    
    

    @Autowired
    RabbitTemplate rabbitTemplate;

	/**
	* convertAndSend 参数
	* 第一个是交换机的名字
	* 第二个是队列的具体名字 因为是rabbit开头 因为RabbitConfig设置rabbit开头通配 所以会被监听到
	* 第三个是具体的消息
	**/
    @Override
    public String sendMq(String mq) {
    
    
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, "rabbit.demo", mq);
        return "发送成功,消息是:" + mq;
    }
}

6. Send a message
Start the producer program
Browser input 127.0.0.1/sendMq?mq=test
insert image description here
insert image description here

2. Consumers

1.application.properties

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
server.port=8082

2. Rabbit monitor
You only need to create a monitor in the consumer to monitor the messages of the queue, and
you can directly print out the Message message

@Component
public class ConsumerListener {
    
    

	/**
	* 设置监听哪一个队列 这个队列是RabbitConfig里面设置好的队列名字
	**/
    @RabbitListener(queues = "rabbit_queue")
    public void listenQueue(Message message){
    
    
        System.out.println(new String(message.getBody()));
    }

}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45580378/article/details/131756797