SpringBoot整合RabbitMq

一、首先创建一个Maven父子工程,如下图:

SpringBoot整合RabbitMq_第1张图片

 rabbit-sender包用于发送消息,rabbit-receiver用于接收消息,rabbit-entity用户存储发送的数据对象。

二、首先添加依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
                  



        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-devtools
            true
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        
        
        
             
		    org.projectlombok    
		    lombok
		

三、rabbit-sender发送端代码demo:

       1、配置application.properties

server.port=8081

#RabbitMQ链接信息
#定义的名称
spring.application.name=spirng-boot-rabbitmq-sender
#ip地址
spring.rabbitmq.host=127.0.0.1
#端口
spring.rabbitmq.port=5672
#账号
spring.rabbitmq.username=admin
#密码
spring.rabbitmq.password=admin
#指定主机名称,这里如果不写,会出现问题
spring.rabbitmq.virtual-host=my_vhost

       2、配置队列

@Configuration  //boot配置类注解
public class SenderConf {
	/**定义一个队列ben,然后给该队列定义一个名称,也就是"queue"
     * import org.springframework.amqp.core.Queue;
	 * @return
	 */
	@Bean
     public Queue queue() {
          return new Queue("queue");
     }
}

    3、配置发送消息的方

@Component //spring注解
public class HelloSender {
    @Autowired
    private AmqpTemplate template;//boot集成了一个类可以发送消息,特方便
    
    public void send() {
    	User user=new User();
    	user.setId(1);
    	user.setName("小毅毅");
    template.convertAndSend("queue",user);
    }
}

      4.Test方法(用于调用发送方法)

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

	@Test
	public void contextLoads() {
	}

	@Autowired
    private HelloSender helloSender;

    @Test
    public void testRabbit() {
        helloSender.send();
    }
}

四、rabbit-receiver接收端代码demo

      1、application.properties配置文件和发送端一样,就不写了,直接复制就好

      2、队列配置类(和发送端一样,注意的是K(就是那个参数)必须相同,不然接收不到发送端的队列)

@Configuration
public class ReceiverConf {
     @Bean
     public Queue queue() {
          return new Queue("queue");
     }
}

      3、消息接收方法(@RabbitListener(queues="queue")注解监听队列名为“queue”的这个队列,一旦监听到发送消息,那么就会接收到消息,并打印出来”)

@Component
public class HelloReceiver {

	@RabbitListener(queues="queue")    //监听器监听指定的Queue
    public void processC(User s) {
        System.out.println("Receive:"+s);
    }
}

五、用一个实体类包来传输数据,注意,队列中的实体类数据必须实现序列化接口,不然无法发送队列

SpringBoot整合RabbitMq_第2张图片

六、开始测试,首先运行接收端代码,运行成功后,在执行发送端的Test方法用于发送队列。如果发送端发送成功,此时接收端就会成功接收到消息,并且打印出来。

SpringBoot整合RabbitMq_第3张图片 发送端测试成功。

 

SpringBoot整合RabbitMq_第4张图片

接收端收到消息。 

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