RabbitMQ实战--- SpringBoot与Queue入门案例

版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/82885729

ps:可以先参考下Linux Centos7 安装RabbitMQ

项目结构:

pom.xml:项目所需要的依赖包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lucifer</groupId>
    <artifactId>springboot-rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-rabbitmq</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Rabbitmq与springboot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

QueueConfig:

package com.lucifer.rabbitmq.queue;

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

/**
 * @author: Lucifer
 * @create: 2018-09-28 14:56
 * @description: 创建消息队列
 **/
@Configuration
public class QueueConfig {

    /**
     * 创建消息队列
     * @return
     */
    @Bean
    public Queue createQueue(){
        return new Queue("Queue");
    }

}

Sender:

package com.lucifer.rabbitmq.queue;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: Lucifer
 * @create: 2018-09-28 15:00
 * @description: 消息发送者
 **/
@Component
public class Sender {

    @Autowired
    private AmqpTemplate amqpTemplate;
    /**
     * 发送消息的方法
     */
    public void send(String msg) {
        /*
            向消息队列发送消息
            1.参数一:队列的名称
            2.参数二:消息
         */

        this.amqpTemplate.convertAndSend("Queue",msg);
    }
}

Receiver:

package com.lucifer.rabbitmq.queue;

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

/**
 * @author: Lucifer
 * @create: 2018-09-28 15:05
 * @description: 消息接受者
 **/
@Component
public class Receiver {
    /**
     * 接收消息的方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitListener(queues = "Queue")
    public  void process(String msg){
        System.out.println("receiver:"+msg);
    }

}

SpringbootRabbitmqApplication:springboot启动类

package com.lucifer.rabbitmq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootRabbitmqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
}

application.yml:

spring:
  rabbitmq:
    host: 192.168.x.x(这里输入自己的rabbitmq的ip,我是安装在虚拟机,这里我是远程的ip)
    username: lucifer
    password: 123456
    port: 5672

SpringbootRabbitmqApplicationTests:springboot的测试类

package com.lucifer.rabbitmq;

import com.lucifer.rabbitmq.queue.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

    @Autowired
    private Sender sender;
    /**
     * 测试消息队列
     */
    @Test
    public void test1() {
        this.sender.send("Hello RabbitMQ");
    }

}

运行test测试方法,浏览器输入http://192.168.xx.xx:15672,也就是你的rabbitmq安装的电脑的ip地址,并且输入你自己安装rabbitmq后,设置的用户名密码,登录rabbitmq主页。

 运行完test后,这里会显示你的队列名和一些其它信息。

这个Web管理界面可以查看rabbitmq的一些信息。

参考:springboot整合rabbitmq(springboot的官方文档)

猜你喜欢

转载自blog.csdn.net/qq_37495786/article/details/82885729