SpringBoot整合RabbitMQ

19 篇文章 0 订阅
17 篇文章 0 订阅

pom.xml添加以下内容:

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

 application.properties:

spring.application.name=spring-boot-rabbitmq
spring.rabbitmq.host=192.168.80.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456

RabbitMQConfig:

配置交换器、队列、绑定关系:

package com.song.songvue.config.rabbitmq;

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

@Configuration
public class RabbitMQConfig {

    // 直连交换器
    public static final String SONG_EXCHANGE = "song_exchange";
    public static final String SONG_QUEUE = "song_queue";
    public static final String ROUTING_KEY = "songKey";

    // fanout交换器
    public static final String SONG_FANOUT = "song_fanout";
    // 邮箱信息队列
    public static final String SONG_MAIL_MESSAGE_QUEUE = "song_mail_message_queue";
    // 游戏等级队列
    public static final String SONG_GAME_LEVEL_QUEUE = "song_game_level_queue";

    // topic交换器
    public static final String SONG_TOPIC_EXCHANGE = "song_topic_exchange";

    // 创建一个队列
    @Bean
    public Queue queue() {
        return new Queue(SONG_QUEUE);
    }

    // 创建一个直连交换机
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange(SONG_EXCHANGE);
    }

    // 绑定
    @Bean
    Binding binding() {
        return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTING_KEY);
    }

    // fanout交换器
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange(SONG_FANOUT);
    }

    // 创建两个队列
    @Bean
    Queue mailMessageQueue() {
        return new Queue(RabbitMQConfig.SONG_MAIL_MESSAGE_QUEUE);
    }

    @Bean
    Queue gameLevelQueue() {
        return new Queue(RabbitMQConfig.SONG_GAME_LEVEL_QUEUE);
    }

    // 绑定两个队列
    @Bean
    Binding bindingMail() {
        return BindingBuilder.bind(mailMessageQueue()).to(fanoutExchange());
    }

    @Bean
    Binding bindingGame() {
        return BindingBuilder.bind(gameLevelQueue()).to(fanoutExchange());
    }

    // topic
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(SONG_TOPIC_EXCHANGE);
    }

    /**
     *
     * RoutingKey 为一个点号 "."分隔的字符串
     *
     * BindingKey 可以存在两种特殊字符串 "*" 和 "#", 用做模糊匹配。
     *
     * com.#
     *       "#" 用于匹配多规格单词(可以是0个)
     *
     * *.song.*
     *       "*" 用于匹配一个单词
     */
    @Bean
    Binding bindTopic1() {
        return BindingBuilder.bind(queue()).to(topicExchange()).with("com.#");
    }

    @Bean
    Binding bindTopic2() {
        return BindingBuilder.bind(gameLevelQueue()).to(topicExchange()).with("*.song.*");
    }

    @Bean
    Binding bindTopic3() {
        return BindingBuilder.bind(mailMessageQueue()).to(topicExchange()).with("*.song");
    }

}

测试 :

浏览器  http://localhost:9010/sendTopicMsg?message="go go let do it" 简单测试

package com.song.songvue.test;

import com.song.songvue.config.rabbitmq.RabbitMQConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestQueue {

    @Autowired
    private AmqpTemplate amqpTemplate;

    // 测试直连交换器
    @RequestMapping("/sendMsg")
    public String sendMsg(String message) {
        amqpTemplate.convertAndSend(RabbitMQConfig.SONG_EXCHANGE, RabbitMQConfig.ROUTING_KEY, message);
        return "OK";
    }

    // 测试fanout类型交换器
    @RequestMapping("/sendFanoutMsg")
    public String sendFanoutMsg(String message) {
        amqpTemplate.convertAndSend(RabbitMQConfig.SONG_FANOUT, null, message);
        return "OK";
    }

    // 测试topic类型交换器
    @RequestMapping("/sendTopicMsg")
    public String sendTopicMsg(String message) {
        amqpTemplate.convertAndSend(RabbitMQConfig.SONG_TOPIC_EXCHANGE, "java.song", message);
        return "OK";
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值