SpringBoot integrates RabbitMQ

RabbitMQ is an open source message queue system developed in erlang language. If you don't understand, you can check the official website http://www.rabbitmq.com/

This article introduces a simple integration of RabbitMQ with springboot.

1. Install rabbitmq, you can do Baidu by yourself, there are many ways.

2. Start rabbitmq, the success is as follows:

You can visit http://localhost:15672/ to view the management page

Create a new project and add dependencies to the pom file. The complete pom is as follows:

<?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>

Create a new message sender Sender and use AmqpTemplate to send messages to the message queue message. code show as below:

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 [email protected]
 * @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 "发送成功";
    }
}

Create a message receiver Receiver, and use the annotation @RabbitListener(queues = "message") to listen to the message queue @RabbitHandler of the message to achieve specific consumption.

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 [email protected]
 * @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());
    }
}

Then look at the configuration information, because it is a simple integration, so only the port and rabbitmq information is configured, as follows:

##端口号
server.port=8888

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

Create a controller, which is only used for testing, the code is as follows:

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 [email protected]
 * @date 2018/4/25
 */
@RestController
public class TestController {

    @Autowired
    private Sender sender;

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

}

Start the project, visit http://localhost:8888/hello and watch the console to see that the message has been sent successfully.

Source code download: Da Lao Yang Code Cloud

Personal website: https://dalaoyang.cn

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978249&siteId=291194637