RabbitMQ日常(一)HelloWorld之模拟收发消息

  1. 由生产者创建消息后,放置在(交换机)exchange

  2. RabbitMQ通过相关配置绑定exchangequeue(队列)

  3. 消费者通过channel(管道)获取channel中的消息

在这里插入图片描述

实验环境:
  1. rabbitmq 3.7.14(使用rpm安装)
主要步骤为:
  1. 创建一个Springboot项目,小白请自行百度

  2. 通过maven的pom文件引入相关依赖jar包

  3. 编写生产端、消费端代码

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

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

com.rabbitMQ

mq_demo

0.0.1-SNAPSHOT

mq_demo

Demo project for Spring Boot

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

com.rabbitmq

amqp-client

5.6.0

org.springframework.boot

spring-boot-maven-plugin

生产者代码

package com.new_rabbitmq;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.ConnectionFactory;

public class Produce {

public static void main(String[] args) throws Exception{

ConnectionFactory connectionFactory= new ConnectionFactory();

connectionFactory.setHost(“你的主机IP”);

connectionFactory.setPort(5672);

connectionFactory.setVirtualHost(“/”);

Connection connection=connectionFactory.newConnection();

Channel channel=connection.createChannel();

String msg=“This is a message2!”;

//这里的test01为队列名称

channel.basicPublish(“”,“test01”,null,msg.getBytes());

channel.close();

connection.close();

}

}

消费者代码

package com.new_rabbitmq;

import com.rabbitmq.client.*;

import com.rabbitmq.client.impl.AMQImpl;

import java.io.IOException;

public class Consumer {

public static void main(String[] args) throws Exception{

  • 22
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值