赞
踩
rabbitmq
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 -d rabbitmq:3.9-management
端口号 | 说明 |
---|---|
4369 | erlang发现端口 |
5672 | client端通信口 |
15672 | 管理界面ui端口 |
25672 | server间内部通信口 |
springboot
项目添加依赖 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.properties
添加配置#rabbitmq 服务地址
spring.rabbitmq.host=192.168.1.66
#rabbitmq client端口
spring.rabbitmq.port=5672
#登录账号
spring.rabbitmq.username=guest
#登录密码
spring.rabbitmq.password=guest
spring.rabbitmq.listener.direct.acknowledge-mode=manual
#虚拟服务
spring.rabbitmq.virtual-host=/
MQConsumer
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* 消息队列消费
*/
@Slf4j
@Component
public class MQConsumer {
/**
* 监听
*/
@RabbitListener(bindings = {
@QueueBinding(value = @Queue(value = "队列名",
durable="false"), exchange = @Exchange(value = "交换机",
durable="false",
type= ExchangeTypes.TOPIC,
ignoreDeclarationExceptions = "true"),
key = "路由key"
)
})
public void redirect(Message message) throws IOException {
log.info("消息内容:{}",new String(message.getBody()));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。