简单-SpringBoot整合RabbitMQ

1.windows下安装erlang环境和rabbitMq服务

https://blog.csdn.net/tirster/article/details/121938987

1.1客户端页面

http://127.0.0.1:15672/#/

注意: 控制台端口是15672 服务端口是5672别弄混了

登陆账户密码:guest guest

2.准备工作

2.1pom依赖

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

2.1启动类注解开启:@EnableRabbit

image

2.2application配置文件

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

image

3.队列的简单使用

3.1 配置交换器与队列

package com.demo.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 大汉软件
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue myQueue(){
        return new Queue("que001",true);
    }
}

3.2 消息发送者

package com.demo.mq;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@RequiredArgsConstructor
@Service
public class QueueProvider {
    private final Queue myQueue;
    private final RabbitTemplate rabbitTemplate;
    /**
     * 发送普通消息
     *
     * @param msg msg
     */
    public void sendMsg(String msg) {
        rabbitTemplate.convertAndSend(myQueue.getName(), msg);
    }

}

3.3 消息消费者

package com.demo.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@Slf4j
@Service
public class QueueConsumer {
    @RabbitListener(queues = {"que001"})
    public void receive(@Payload String fileBody) {
        log.info("que001队列:" + fileBody);
    }
}

3.4 接口调用:

package com.demo.controller;

import com.demo.mq.QueueProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大汉软件
 */
@RestController
public class MqController {
    @Autowired
    private QueueProvider queueProvider;

    @GetMapping("/send")
    public String sendMsgToMq(@RequestParam String msg) {
        queueProvider.sendMsg(msg);
        return "ok";
    }
}

结果如下:

image

4.主题的高级使用(Topic Exchange)---主题交换器

主题交换器,生产者将消息丢给它后,它就给与自己绑定的那些个关心这个消息queue全部发送一份消息。 就简单理解成发布-订阅模式就好了。

  • Queue 队列
  • RoutingKey路由key:用来控制交换器如何将消息发送给绑定的队列。例如交换器说了:俺们只投递路由key包含“shusheng007”字样的消息,其他的俺们不处理。 然后来了条消息,这条消息的路由key是“王二狗”,然后交换器一看不对啊,就残忍的拒绝了投递消息到队列的工作。

4.1配置交换器与队列

package com.demo.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 大汉软件
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue myQueue(){
        return new Queue("que001",true);
    }

    @Bean
    public Queue topicQueue1(){
        return new Queue("topicQueue1",true);
    }

    @Bean
    public Queue topicQueue2(){
        return new Queue("topicQueue2",true);
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding topicBinding1(){
        return BindingBuilder.bind(topicQueue1()).to(topicExchange())
                .with("que001.id.*");
    }

    @Bean
    public Binding topicBinding2(){
        return BindingBuilder.bind(topicQueue2()).to(topicExchange())
                .with("que001.name.*");
    }
}

4.2消息发送者

package com.demo.mq;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@RequiredArgsConstructor
@Service
public class QueueProvider {
    private final Queue myQueue;
    private final RabbitTemplate rabbitTemplate;
    private final static String EXCHANGE_NAME = "topicExchange";

    /**
     * 发送普通消息
     *
     * @param msg msg
     */
    public void sendMsg(String msg) {
        rabbitTemplate.convertAndSend(myQueue.getName(), msg);
    }

    /**
     * 主题信息
     *
     * @param msg   msg
     * @param route route
     */
    public void sendTopicMsg(String msg, String route) {
        rabbitTemplate.convertAndSend(EXCHANGE_NAME, route, msg);
    }
}

4.3消息消费者

package com.demo.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

/**
 * 大汉软件
 */
@Slf4j
@Service
public class QueueConsumer {
    @RabbitListener(queues = {"que001"})
    public void receive(@Payload String fileBody) {
        log.info("que001队列:" + fileBody);
    }

    @RabbitListener(queues = {"topicQueue1"})
    public void receiveTopic1(@Payload String fileBody) {
        log.info("topic1队列:" + fileBody);
    }

    @RabbitListener(queues = {"topicQueue2"})
    public void receiveTopic2(@Payload String fileBody) {
        log.info("topic2队列:" + fileBody);
    }

}

4.4测试接口类

package com.demo.controller;

import com.demo.mq.QueueProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大汉软件
 */
@RestController
public class MqController {
    @Autowired
    private QueueProvider queueProvider;

    @GetMapping("/send2")
    public String sendMsgToMq2(@RequestParam String msg, String routeKey) {
        queueProvider.sendTopicMsg(msg, routeKey);
        return "ok";
    }
}

5.参考

https://blog.csdn.net/ShuSheng0007/article/details/125921491

posted @ 2023-02-16 00:31  土木转行的人才  阅读(32)  评论(0编辑  收藏  举报