springboot集成RabbitMQ

首先我们需要在配置文件中添加相应的依赖

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

在配置文件中加入rabbitmq 相关连接参数

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

创建队列,交换机,并且进行绑定

package com.example.rabbitdemo.config;

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.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    /**
     * 
     * @Description:创建一个队列
     */
    @Bean
    public Queue queueForSingleMode() {
        return new Queue("quene.testquene");
    }

    /**
     * 
     * @Description:创建一个主题交换机
     */
    @Bean
    public TopicExchange createTopicExchange() {
        return new TopicExchange("opic.testexchange");
    }

    /**
     * 
     * @Description:把队列与交换机进行绑定,并且routingKey值为topictest。这个没有使用通配符相当于是一个直连交换机
     * 
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queueForSingleMode()).to(createTopicExchange()).with("topictest");
    }

    /**
     * 使用 JSON 序列化化消息
     * @param connectionFactory
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //数据转换为json存入消息队列
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
}

创建生产者,用于发送信息

package com.example.rabbitdemo.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitProduce {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String sendMsg() {
        Map<String, Object> msg = new HashMap<>(8);
        msg.put("msg", "这是一条测试信息");
        msg.put("type", "json");
        msg.put("version", 123);
        //交换机的名称及routingKey与config中保持一致
        rabbitTemplate.convertAndSend("opic.testexchange", "topictest", msg);
        return "";
    }
}

创建消费者,用于接收信息

package com.example.rabbitdemo.service;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class RabbitReceiver {
    @RabbitListener(queues = "quene.testquene")
    public void consume(Message message, Channel channel) throws IOException {
        log.info("DirectConsumeListener,收到消息: {}", new String(message.getBody()));
    }
}

建立测试类运行

package com.example.rabbitdemo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.example.rabbitdemo.service.RabbitProduce;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RabbitdemoApplication.class)
public class ProduceTest {
    @Autowired
    private RabbitProduce rabbitProduce;

    @Before
    public void before() {

    }

    @Test
    public void testStartUp() {
        rabbitProduce.sendMsg();
    }
}

运行结果

 

源码地址:

https://download.csdn.net/download/xinghui_liu/12840339

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值