springboot结合rabbitMQ应用

1、生产者

1.1添加配置文件

pom.xml


        
            org.springframework.boot
            spring-boot-starter-amqp
            1.5.2.RELEASE
        

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类监听消息

springboot结合rabbitMQ应用_第1张图片

你可能感兴趣的:(java,springboot,rabbitmq)