java操作rabbitmq

搭建项目

创建项目maven项目jkw-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>jkw-rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--springMVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--test-->
        <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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

#RabbitMQ
spring:
  rabbitmq:
    host: 192.168.66.101
    port: 5672
    username: guest
    password: guest
    virtual-host: /

#日志格式
logging:
  pattern:
    console: '%d{HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'

启动类

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

配置队列和交换机

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类【配置交换机和队列】
 */
@Configuration
public class RabbitConfig {
    private final String EXCHANGE_NAME="springboot_topic";//交换机名称
    private final String QUEUE_NAME="springboot_queue";//队列名称
    //创建topic交换机
    @Bean("springbootTopic")
    public Exchange getExchange(){
        return ExchangeBuilder
                .topicExchange(EXCHANGE_NAME)
                .durable(true)//是否持久化,true-关闭后交换机仍存在
                .build();
    }
    //创建队列
    @Bean("springbootQueue")
    public Queue getQueue(){
        return new Queue(QUEUE_NAME);
    }
    //交换机绑定队列
    @Bean
    public Binding bindQueue(@Qualifier("springbootTopic") Exchange exchange, @Qualifier("springbootQueue")Queue queue){
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("#.msg.#")//路由键
                .noargs();//没有其余参数
    }
}

测试使用

生产者

/**
 * 生产者
 */
@RestController
public class Producer {
    //SpringBoot整合RabbitMQ时提供了工具类RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMsg(){
        rabbitTemplate.convertAndSend("springboot_topic","msg","发送消息");//交换机名/路由键/消息
        return "发送消息";
    }
}

消费者

/**
 * 消费者
 */
@Component
public class Consumer {
    @RabbitListener(queues = "springboot_queue")//监听的队列名
    public void listen(String msg){//框架自动帮我们转字节数组
        System.out.println(msg);
    }
}

测试

访问

http://localhost:8080/send

测试结果

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月木@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值