Springboot整合rabbitmq

springboot整合rabbitmq

  • 基础依赖
		<parent>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-starter-parent</artifactId>
        	<version>2.0.1.RELEASE</version>
   		 </parent>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
  • 依赖引入
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
		
  • 添加相关配置到配置文件
spring.rabbitmq.host=192.168.157.129
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/admin

至此 环境准备结束

简单模式

  • 编写mq配置类
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    //简单模式
    @Bean
    public Queue getQueue() {
        /**
         * String name,
         * boolean durable 持久化,
         * boolean exclusive,
         * boolean autoDelete
         */
        return new Queue("simple-queue", true, false, false);
    }
}

  • 编写生产者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producter01 {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String msg){
        rabbitTemplate.convertAndSend("","simple-queue",msg);
        System.out.println("消息发送成功");
    }
}
  • 编写消费者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer01 {

    @RabbitListener(queues = "simple-queue")
    public void process(String msg){
        System.out.println("接收到的消息为:"+msg);
    }
}

  • 编写测试类
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test01 {

    @Autowired
    private Producter01 producter;

    @Test
    public void contextLoads() {
        producter.send("整合RabbitMQ成功!");
    }
}

  • fanout模式

沿用上面的RabbitConfig类,添加以下代码,声明交换机、队列,声明交换机和队列的绑定关系

//fanout发布订阅模式,声明fanout交换机
    @Bean
    public FanoutExchange getFanoutExchange(){
        return new FanoutExchange("fanout-exchange");
    }

    //声明队列
    @Bean
    public Queue getQueueOne(){
        return new Queue("fanout-queue1");
    }

    @Bean
    public Queue getQueueTwo(){
        return new Queue("fanout-queue2");
    }

    //将队列绑定到fanout交换机上
    @Bean
    public Binding getBindingOne(FanoutExchange getFanoutExchange, Queue getQueueOne){
        return BindingBuilder.bind(getQueueOne).to(getFanoutExchange);
    }

	@Bean
    public Binding getBindingTwo(FanoutExchange getFanoutExchange, Queue getQueueTwo) {
        return BindingBuilder.bind(getQueueTwo).to(getFanoutExchange);
    }

  • 生产者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producter02 {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String msg){
        rabbitTemplate.convertAndSend("fanout-exchange","",msg);
        System.out.println("消息发送成功!!!");
    }
}

  • 消费者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer02 {

    @RabbitListener(queues = "fanout-queue1")
    public void process(String msg) {
        System.out.println("消息队列1接收到的消息为:" + msg);
    }

    @RabbitListener(queues = "fanout-queue2")
    public void process2(String msg) {

        System.out.println("消息队列2接收到的消息为:" + msg);
    }
}

direct模式

  • 配置类

配置类沿用以上的,追加如下代码

    //direct路由模式
    @Bean
    public DirectExchange getDirectExchange() {
        return new DirectExchange("direct-exchange");
    }

    //声明队列
    @Bean
    public Queue getQueueThree() {
        return new Queue("direct-queue1");
    }

    @Bean
    public Queue getQueueFour() {
        return new Queue("direct-queue2");
    }

    //将队列绑定到direct交换机上
    @Bean
    public Binding getBindingThree(DirectExchange getDirectExchange, Queue getQueueThree) {
        return BindingBuilder.bind(getQueueThree).to(getDirectExchange).with("log.error");
    }

    @Bean
    public Binding getBindingFour(DirectExchange getDirectExchange, Queue getQueueFour) {
        return BindingBuilder.bind(getQueueFour).to(getDirectExchange).with("log.success");
    }
  • 生产者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producter03 {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send1(String msg){
        rabbitTemplate.convertAndSend("direct-exchange","log.error",msg);
        System.out.println("消息发送成功!!!");
    }
    public void send2(String msg){
        rabbitTemplate.convertAndSend("direct-exchange","log.success",msg);
        System.out.println("消息发送成功!!!");
    }

  • 消费者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer03 {

    @RabbitListener(queues = "direct-queue1")
    public void process(String msg) {
        System.out.println("###############消息队列1接收到的消息为:" + msg);
    }

    @RabbitListener(queues = "direct-queue2")
    public void process2(String msg) {
        System.out.println("################消息队列2接收到的消息为:" + msg);
    }
}
  • 测试类
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test03 {

    @Autowired
    private Producter03 producter;

    @Test
    public void contextLoads() {
        producter.send1("整合RabbitMQ成功!##########");
        producter.send2("************整合RabbitMQ成功!");
    }
}

topic模式

  • 配置类

配置类沿用以上的,追加如下代码

 //topic发布订阅模式,声明交换机
    @Bean
    public TopicExchange getTopicExchange(){
        return new TopicExchange("topic-exchange");
    }

    //声明队列
    @Bean
    public Queue getQueueFive(){
        return new Queue("topic-queue1");
    }
    //声明队列
    @Bean
    public Queue getQueueSix(){
        return new Queue("topic-queue2");
    }

    //绑定到topic交换机上
    @Bean
    public Binding getBindingTopicFive(TopicExchange getTopicExchange,Queue getQueueFive){
        return BindingBuilder.bind(getQueueFive).to(getTopicExchange).with("log.*");
    }
    //绑定到topic交换机上
    @Bean
    public Binding getBindingTopicSix(TopicExchange getTopicExchange,Queue getQueueSix){
        return BindingBuilder.bind(getQueueSix).to(getTopicExchange).with("log.#");
    }
  • 生产者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producter04 {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String topic,String msg){
        rabbitTemplate.convertAndSend("topic-exchange",topic,msg);
        System.out.println("消息发送成功");
    }
}

  • 消费者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer04 {
    @RabbitListener(queues = "topic-queue1")
    public void process1(String msg) {
        System.out.println("消息队列1接收到的消息为:" + msg);
    }
    @RabbitListener(queues = "topic-queue2")
    public void process2(String msg) {
        System.out.println("消息队列2接收到的消息为:" + msg);
    }
}

  • 测试类
import com.jlq.mq03.Producter03;
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.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test04 {

    @Autowired
    private Producter04 producter;

    @Test
    public void contextLoads() {
        producter.send("log.succcess","整合RabbitMQ成功!##########");
        producter.send("log.info.error","************整合RabbitMQ成功!");
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值