springboot结合rabbitMQ应用

3 篇文章 0 订阅
1 篇文章 0 订阅

1、生产者

1.1添加配置文件

pom.xml

<!--rabbitMQ amqp协议-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

application.yml

spring:
  rabbitmq:
    host: 192.168.123.6
    port: 5672
    username: root
    password: root
    virtual-host: /java

1.2 springboot执行类添加Bean

import org.springframework.amqp.core.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootProvideApplication {


	public static void main(String[] args) {
		SpringApplication.run(SpringbootProvideApplication.class, args);
	}

	//发送的队列步骤1配置路径 步骤3
//	@Bean
//	public Queue getQueue(){
//		return new Queue("simple_springboot_queue");
//	}
//fanout类型start
//	@Bean
//	public Queue getQueue1(){
//		return new Queue("simple_springboot_queue1");
//	}
//
//	@Bean//spring管理当前队列
//	public Queue getQueue2(){
//		return new Queue("simple_springboot_queue2");
//	}
//
//交换机
//	@Bean
//	public FanoutExchange getExchange(){
//		return new FanoutExchange("fanout_springboot_exchange");
//	}
//绑定交换机和队列1
//	@Bean
//	public Binding getBinding1(Queue getQueue1,FanoutExchange getExchange){
//		return  BindingBuilder.bind(getQueue1).to(getExchange);
//	}
//绑定交换机和队列2
//	@Bean
//	public Binding getBinding2(Queue getQueue2,FanoutExchange getExchange){
//		return  BindingBuilder.bind(getQueue2).to(getExchange);
//	}
//fanout类型end
//topic类型start
	@Bean
	public Queue getQueue1(){
		return new Queue("topic_springboot_queue1");
	}

	@Bean//spring管理当前队列
	public Queue getQueue2(){
		return new Queue("topic_springboot_queue2");
	}
	@Bean
	public TopicExchange getExchange(){
		return new TopicExchange("topic_springboot_exchange");
	}
	@Bean
	public Binding getBinding1(Queue getQueue1,TopicExchange getExchange){
		return  BindingBuilder.bind(getQueue1).to(getExchange).with("add.*");
	}

	@Bean
	public Binding getBinding2(Queue getQueue2,TopicExchange getExchange){
		return  BindingBuilder.bind(getQueue2).to(getExchange).with("add.#");
	}
	//topic类型end
	
}

1.3 生产者测试类 


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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 SpringbootProvideApplicationTests {

	@Autowired
	private RabbitTemplate rabbitTemplate; //步骤2

	//发送消息

	@Test
	public void contextLoads() {
		Student student = new Student(1,"小强",18);
		//发送消息1
		rabbitTemplate.convertAndSend("simple_springboot_queue",student);
//fanout类型
//		rabbitTemplate.convertAndSend("fanout_springboot_exchange","",student);
//topic类型		//rabbitTemplate.convertAndSend("topic_springboot_exchange","add.update.delete",student);
	}

}

2.消费者

2.1配置文件同生产者

2.2.监听类

import com.lv.Student;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Created by admin on 2018/11/23.
 * rabbitmq监听,注解表示监听
 */
//@RabbitListener(queues = "simple_springboot_queue") //例子1只有一个队列时
@Component
public class RabbitMQListener {

    /**
     * 消息处理方法,注解RabbitHandler表示监听
     */
//    @RabbitHandler
//    public void msgHandler(String msg){
//        System.out.println("获取消息"+msg);
//    }

//    @RabbitHandler
//    @RabbitListener(queues = "fanout_springboot_queue1")
//    public void msgHandlerFanout1(Student msg){
//        System.out.println("第一个消费者接收消息:"+msg);
//    }
//
//    @RabbitHandler
//    @RabbitListener(queues = "fanout_springboot_queue2")
//    public void msgHandlerFanout2(Student msg){
//        System.out.println("第二个消费者接收消息:"+msg);
//    }

    @RabbitHandler
    @RabbitListener(queues = "topic_springboot_queue1")
    public void msgHandlerFanout1(Student msg){
        System.out.println("第一个消费者接收消息:"+msg);
    }

    @RabbitHandler
    @RabbitListener(queues = "topic_springboot_queue2")
    public void msgHandlerFanout2(Student msg){
        System.out.println("第二个消费者接收消息:"+msg);
    }
}

3.项目结构图

先运行生产者测试类发送消息,再启动生成者springbootConsumerApplication类监听消息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值