springBoot使用rabbitmq

关于 rabbitmq 的学习,推荐参考https://blog.csdn.net/super_rd?t=1,这里直接进入 主题

spring boot 基础 rabbitmq 主要依赖 spring-boot-starter-amqp

配置文件

配置关于rabbitmq 的连接和用户信息

spring.application.name=rabbitmq

spring.rabbitmq.addresses=192.168.86.132

spring.rabbitmq.port=5672

spring.rabbitmq.username=springcloud

spring.rabbitmq.password=springcloud

/*

创建消息生成者sender

AmqpTemplate 接口定义了一套针对AMQP协议的基础操作

*/

package springcloud.spring_boot_rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import java.util.Date;

/*

创建消息生成者sender

AmqpTemplate 接口定义了一套针对AMQP协议的基础操作

*/

@Component

public class Sender {

@Autowired

private AmqpTemplate amqpTemplate;

public void send(){

String context = "hello "+ new Date();

System.out.println("sender : "+context);

this.amqpTemplate.convertAndSend("shendu",context);

}

}

创建消息消费者 receiver,通过 @RabbitListener(queues = "shendu") 注解定义该类shendu队列的监听,并用

@RabbitHandler 注解来指定对消息的处理方法。所以该消费者实现了对shendu队列的消费

package springcloud.spring_boot_rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;

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

import org.springframework.stereotype.Component;

@Component

@RabbitListener(queues = "shendu")

public class Receiver {

@RabbitHandler

public void process(String shendu){

System.out.println("receiver: "+shendu);

}

}

启动类

package springcloud.spring_boot_rabbitmq;

import org.springframework.amqp.core.Queue;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class SpringBootRabbitmqApplication {

public static void main(String[] args) {

SpringApplication.run(SpringBootRabbitmqApplication.class, args);

}

@Bean

public Queue queue(){

return new Queue("shendu");

}

}

pom 文件

spring-boot-starter-amqp 用于支持rabbitmq

<?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>springCloud</groupId>

<artifactId>spring_boot_rabbitmq</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>spring_boot_rabbitmq</name>

<description>Demo project for Spring Boot</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.4.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-amqp</artifactId>

</dependency>

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

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

创建单元测试

package springcloud.spring_boot_rabbitmq;

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 contextLoads() {

sender.send();

}

}

结果:

猜你喜欢

转载自blog.csdn.net/weixin_39639119/article/details/81605590
今日推荐