RabbitMQ+spring boot 快速启动

流程: 首先是获取连接工厂 ConnectionFactory --> 获取一个连接 Connection --> 通过连接建立数据通信 信道 Channel,用 Channel 发送或接收消息。

代码地址:

https://github.com/hmilyos/rabbitmqdemo.git  rabbitmq-api 项目下的 quickstart 包下
复制代码

maven:

    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.15.RELEASE</version>
		<!--<version>2.1.0.RELEASE</version>-->
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

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

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.6.5</version>
        </dependency>
        
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>
	</dependencies>
复制代码

定义和赋值 RabbitMQ 的配置

public interface RabbitMQCommon {

    final static String RABBITMQ_HOST = "192.168.0.7";
    final static int RABBITMQ_PORT = 5672;
    final static String RABBITMQ_DEFAULT_VIRTUAL_HOST = "/";

    public final static String RABBITMQ_USERNAME = "guest";
    public final static String RABBITMQ_PASSWORD = "guest";
}
复制代码

消费端代码:

/**
 * 快速开始:消费者
 */
@Slf4j
public class Consumer {
	private static final Logger log = LoggerFactory.getLogger(Consumer.class);
	
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(RabbitMQCommon.RABBITMQ_HOST);
        connectionFactory.setPort(RabbitMQCommon.RABBITMQ_PORT);
        connectionFactory.setVirtualHost(RabbitMQCommon.RABBITMQ_DEFAULT_VIRTUAL_HOST);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
//          queueName, durable, exclusive, autoDelete, arguments
        channel.queueDeclare("test1001", true, false, false, null);
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
//         queueName,  autoAck, Consumer callback
        channel.basicConsume("test1001", true, queueingConsumer);
        log.info("消费端启动啦~");
        while (true) {
            Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            log.info("消费端接收到:{}", msg);
        }
    }

}
复制代码

生产端代码:

/**
 * 快速开始:生产者
 */
@Slf4j
public class Procuder {

	private static final Logger log = LoggerFactory.getLogger(Procuder.class);
	
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(RabbitMQCommon.RABBITMQ_HOST);
        connectionFactory.setPort(RabbitMQCommon.RABBITMQ_PORT);
        connectionFactory.setVirtualHost(RabbitMQCommon.RABBITMQ_DEFAULT_VIRTUAL_HOST);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        for (int i = 0; i < 5; i++) {
            String msg = "hello RabbitMQ + " + i;
            log.info("生产者发送消息:{}", msg);
            channel.basicPublish("", "test1001", null, msg.getBytes());
        }
        log.info("生产者发送消息完毕");
        channel.close();
        connection.close();
    }
}
复制代码

run运行消费端的代码

打开管控台,看到这个队列创建成功了

运行生产端的代码,看到如下日志

点击这个queue进去查看到刚才有消息发送过来了

在idea查看消费端的日志

刚才生产端发送的消息已被消费端消费,至此,快速启动demo已完毕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值