springboot-26-springboot 集成rabbitmq

rabbitmq是基于AMQP规范的一个消息代理, 它可以兼容jms, 支持其他语言, 并且可以跨平台

 

1, 安装

1) 普通安装

度娘: 

2) docker 安装

sudo docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management

安装成功后:  使用 guest/guest 用户登录

 

2 使用: 

1) 添加 rabbitmq的依赖

        <!--添加 rabbitmq的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2), 添加配置, 默认的配置为 localhost: 5672

########### rabbitmq 的配置 ******
spring: 
  rabbitmq:
    host: 192.168.50.202
    port: 5672
#    username: admin
#    password: secret

3), 生产者: 

package com.wenbronk.enterprise.rabbitmq.send;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 * 实现接口, 为了开机就自动发送
 * Created by wenbronk on 2017/6/13.
 */
@Component
public class RabbitMQSender implements CommandLineRunner{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void run(String... args) throws Exception {
        rabbitTemplate.convertAndSend("my-queue", "来自rabiitmq的问候");
    }
}

4) 消费者: 

package com.wenbronk.enterprise.rabbitmq.receive;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * Created by wenbronk on 2017/6/13.
 */
@Component
public class RabbitMQReceive {

    /**
     * 使用rabbitlistener接收消息
     * @param message
     */
    @RabbitListener(queues = "my-queue")
    public void receiveMessage(String message) {
        System.out.println("receive: " + message);
    }

}

5) ,使用队列模式: 

package com.wenbronk.enterprise.rabbitmq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 队列模式的bean
 * Created by wenbronk on 2017/6/13.
 */
@Configuration
public class QueueConfig {

    /**
     * 定义使用到的队列
     * @return
     */
    @Bean
    public Queue myQueue() {
        return new Queue("my-queue");
    }
}

 

 

启动后, 就会自动发送消息

 另外, 有看到过这个异常且无限循环的, 可以试下

org.springframework.amqp.AmqpException: No method found for class

https://jira.spring.io/browse/AMQP-573

  @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

调用: 

@RabbitListener(queues = "merchant", containerFactory="rabbitListenerContainerFactory")
public void process(@Payload UpdateMerchant request) { 
     UpdateMerchantResponse response = new UpdateMerchantResponse();
    logger.info(request.getMerchantId() + "->" + response.getReturnCode());
 }

 

posted @ 2017-06-19 22:01  bronk  阅读(495)  评论(0编辑  收藏  举报