导航

RabbitMQ使用入门

Posted on 2020-08-19 11:55  ahau10  阅读(233)  评论(0编辑  收藏  举报

docker安装rabbitmq

sudo docker pull rabbitmq:3.7.27-management

启动服务

sudo docker run -d -p 5672:5672 -p 15672:15672 --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3.7.27-management

启用tracing方便查看消息

  1. 启动容器后sudo docker ps -a 查看container id。
  2. 进入容器sudo docker exec -it <container_id> /bin/bash
  3. 启用tracing插件rabbitmq-plugins enable rabbitmq_tracing
  4. exit退出容器

查看插件

  1. 浏览器打开localhost:15672, 用户名和密码都是admin
  2. 点击“Admin”Tab页,右边可以看到“Tracing”,说明插件启用成功了。
  3. 新建一个Tracing,点击“Add a new trace”,“Name”输入“MyTrace”,然后点击“Add Trace”即可。

使用tracing插件,可以在管理页面上看到发送到队列里的消息,非常方便。

Spring Boot项目继承RabbitMQ

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.properties:

spring.rabbitmq.host=192.168.43.22
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=my_vhost
rabbitmq.exchange=ordercenter.exchange
rabbitmq.queue=ordercenter.queue
rabbitmq.routingkey=ordercenter.routingkey

RabbitMQConfig:

@Configuration
@EnableRabbit
public class RabbitMQConfig {

	@Value("${rabbitmq.queue}")
	String queueName;

	@Value("${rabbitmq.exchange}")
	String exchange;

	@Value("${rabbitmq.routingkey}")
	private String routingkey;

	@Value("${spring.rabbitmq.username}")
	String username;

	@Value("${spring.rabbitmq.password}")
	private String password;

	@Bean
	Queue queue() {
		return new Queue(queueName, false);
	}

	@Bean
	DirectExchange exchange() {
		return new DirectExchange(exchange);
	}

	@Bean
	Binding binding(Queue queue, DirectExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(routingkey);
	}

	@Bean
	public MessageConverter jsonMessageConverter() {
		return new Jackson2JsonMessageConverter();
	}

	public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMessageConverter(jsonMessageConverter());
		return rabbitTemplate;
	}


	@Bean
	MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory ) {
		SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
		simpleMessageListenerContainer.setConnectionFactory(connectionFactory);
		simpleMessageListenerContainer.setQueues(queue());
//		simpleMessageListenerContainer.setMessageListener(new RabbitMQListner());
		return simpleMessageListenerContainer;

	}

}

RabbitMQ基本概念

有很多文章,比如这2篇文章:
https://www.cnblogs.com/cnndevelop/p/9879288.html
https://blog.csdn.net/ljj_9/article/details/80433528

发送消息

@Service
public class RabbitMQSender {
	
	@Autowired
	private AmqpTemplate amqpTemplate;
	
	@Value("${rabbitmq.exchange}")
	private String exchange;
	
	@Value("${rabbitmq.routingkey}")
	private String routingkey;	

	public void send(Employee company) {
		amqpTemplate.convertAndSend(exchange, routingkey, company);
		System.out.println("Send msg = " + company);
	    
	}
}

发完消息后,可以在控制页面的Admin>>Tracing>>All traces>>Trace log files里查看已经发送的消息:

接收消息

方式一

在messageListenerContainer方法里设置一个listener实现类,就是上面messageListenerContainer里注释掉的那一行。

@Service
public class RabbitMQListner implements MessageListener {
    public void onMessage(Message message) {
        System.out.println("Consuming Message - " + new String(message.getBody()));
    }
}

方式二

使用 @EnableRabbit@RabbitListener实现。
在RabbitMQConfig类上加上@EnableRabbit。然后写一个Consumer类,在方法上加上@RabbitListener注解

@Component
public class RabbitMQConsumer {
    @RabbitListener(queues = "${rabbitmq.queue}")
    public void recievedMessage(Employee employee) {
        System.out.println("Recieved Message From RabbitMQ: " + employee);
    }
}

使用RabbitMQ可视化界面发送消息

注意发送消息的时候一定要加上content_type这个属性,否则会报消息转换的错:
org.springframework.messaging.converter.MessageConversionException: Cannot convert from [[B] to [com.javainuse.model.Employee] for GenericMessage [payload=byte[87], headers

因为发送端不加类型,接收端没有配置过转换器,默认会用byte数组来接收,转换成具体的对象就转换不了。这里方便大家快速上手,就不搞那么复杂的东西了。

示例代码来自这篇博客,大家也可以直接看这位博主关于rabbitmq的系列文章,写的很好,推荐给大家:
https://www.javainuse.com/misc/rabbitmq-hello-world