RabbitMQ与springboot整合

RabbitMQ与springboot整合
生产者
1.创建生产者SpringBoot工程

2.引入RabbitMQ相关依赖

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

  1. 编写yml配置
# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:
  rabbitmq:
    host: 192.168.0.11 # ip
    port: 5672
    username: wanghui
    password: 123456
    virtual-host: wanghui1228
  1. 定义交换机,队列以及绑定关系的配置类
package com.cshg.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by  on 2021/1/5.
 */
@Configuration
public class RabbitMqConfig {
    //定义交换机的名字
    public static final String  EXCHANGE_NAME = "boot_topic_exchange";
    //定义队列的名字
    public static final String QUEUE_NAME = "boot_queue";

    //1、声明交换机
    @Bean("bootExchange")
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }



    //2、声明队列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }


    //3、队列与交换机进行绑定
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}

5.编写接口,发送消息

Controller层

package com.cshg.rabbitmq.controller;

import com.cshg.rabbitmq.service.RabbitMqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by on 2021/1/5.
 */
@RestController
@RequestMapping("/rabbitmq")
public class RabbitMqController {
    @Autowired
    private RabbitMqService rabbitMqService;
    @RequestMapping(value = "/sendInfo",method = RequestMethod.POST)
    public void sendInfo() {
        try {
            rabbitMqService.sendInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

service层(接口)

package com.cshg.rabbitmq.service;

/**
 * Created by  on 2021/1/5.
 */
public interface RabbitMqService {
    void sendInfo();
}

service(实现类)

package com.cshg.rabbitmq.service.impl;

import com.cshg.rabbitmq.service.RabbitMqService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by  on 2021/1/5.
 */
@Service
public class RabbitMqServiceImpl implements RabbitMqService{
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void sendInfo() {
        rabbitTemplate.convertAndSend("boot_topic_exchange","boot.haha","hellow mq...");
    }
}

消费者
1.创建消费者SpringBoot工程

2.引入RabbitMQ相关依赖

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

  1. 编写yml配置
# 配置RabbitMQ的基本信息  ip 端口 username  password..
server:
  port: 8083
spring:
  rabbitmq:
    host: 192.168.0.11 #主机ip
    port: 5672 #端口
    username: wanghui
    password: 123456
    virtual-host: wanghui1228

4.编写消息监听器

package com.cshg;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Created by on 2021/1/5.
 */
@Component
public class RabbitMqListener{
    //定义方法进行信息的监听   RabbitListener中的参数用于表示监听的是哪一个队列
    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message){
        System.out.println("message:"+new String(message.getBody()));
    }
}

至此,整合完毕。

你可能感兴趣的:(rabbitMq,rabbitmq,spring,boot)