SpringBoot整合RabbitMQ

RabbitMQ是開源消息隊列系統,用erlang語言開發。若是不瞭解能夠查看官網http://www.rabbitmq.com/java

這篇文章介紹一個springboot簡單整合RabbitMQ。git

1.安裝rabbitmq,自行百度便可,方法不少。web

2.啓動rabbitmq,成功以下圖:spring

能夠訪問http://localhost:15672/查看管理頁面apache

新建項目,pom文件加入依賴,完整pom以下:springboot

<?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.dalaoyang</groupId>
    <artifactId>springboot_rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_rabbitmq</name>
    <description>springboot_rabbitmq</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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-devtools</artifactId>
            <scope>runtime</scope>
        </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-amqp</artifactId>
        </dependency>
    </dependencies>

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


</project>

新建一個消息發送者Sender,使用AmqpTemplate將消息發送到消息隊列message中去。代碼以下:app

package com.dalaoyang.sender;

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

import java.util.Date;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.send
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/25
 */
@Component
public class Sender {
    Logger logger = Logger.getLogger(Sender.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    public String send(){
        String context = "簡單消息發送";
        logger.info("簡單消息發送時間:"+new Date());
        amqpTemplate.convertAndSend("message", context);
        return "發送成功";
    }
}

建立消息接收者Receiver,使用註解@RabbitListener(queues = "message")來監聽message的消息隊列@RabbitHandler來實現具體消費。maven

package com.dalaoyang.receiver;

import org.apache.log4j.Logger;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.receiver
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/25
 */
@Component
@RabbitListener(queues = "message")
public class Receiver {
    Logger logger = Logger.getLogger(Receiver.class);

    @RabbitHandler
    public void process(String Str) {
        logger.info("接收消息:"+Str);
        logger.info("接收消息時間:"+new Date());
    }
}

而後看一下配置信息,由於是簡單整合,因此只配置了端口和rabbitmq的信息,以下:spring-boot

##端口號
server.port=8888

##rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

建立一個controller,這裏僅用來測試,代碼以下:測試

package com.dalaoyang.controller;

import com.dalaoyang.sender.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/25
 */
@RestController
public class TestController {

    @Autowired
    private Sender sender;

    @GetMapping("hello")
    public String helloTest(){
        sender.send();
        return "success";
    }

}

啓動項目,訪問http://localhost:8888/hello而後觀看控制檯能夠看到消息已經發送成功。

源碼下載 :大老楊碼雲

我的網站:https://dalaoyang.cn

相關文章
相關標籤/搜索