最便宜的视频网站建设,南宁企业网站seo,外贸网站系统,三亚建设信息网站接上一篇《RabbitMQ-安装篇#xff08;阿里云主机#xff09;-CSDN博客》
安装好RabbitMQ后#xff0c;我们将开始RabbitMQ的使用#xff0c;根据官网文档RabbitMQ Tutorials | RabbitMQ#xff0c;我们一步一步的学习。
1. Hello World!
这里先说明几个概…接上一篇《RabbitMQ-安装篇阿里云主机-CSDN博客》
安装好RabbitMQ后我们将开始RabbitMQ的使用根据官网文档RabbitMQ Tutorials | RabbitMQ我们一步一步的学习。
1. Hello World!
这里先说明几个概念
生产者指消息的发送方用图例表示。
消费者指消息的接收放用图例表示。
队列(queue)生产者发送的消息将被传递到队列里消费这从队列中消费消息
下面以 生产者 发送消息到队列消费者从队列里消费消息为例演示如何调用Java代码。 RabbitMQ支持多种协议。本教程使用AMQP0-9-1它是一个开放的、通用的消息传递协议。
1.1 下载依赖项
下载客户端库客户端连接库及其依赖项SLF4J API和SLF4J Simple。将这些文件复制到您的工作目录中连同教程Java文件。
1.2 生产者代码-Send.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.nio.charset.StandardCharsets;//生产者
public class Send {public static void main(String[] argv) throws Exception {// 创建连接ConnectionFactory factory new ConnectionFactory();factory.setHost(【服务器地址】);factory.setPort(【端口默认5672】);factory.setUsername(【账号】);factory.setPassword(【密码】);factory.setVirtualHost(【虚拟主机】);// 创建信道,发送消息String queueName rc.queue;try (Connection connection factory.newConnection(); Channel channel connection.createChannel()) {channel.queueDeclare(queueName, true, false, false, null);String message Hello World!;channel.basicPublish(, queueName, null, message.getBytes(StandardCharsets.UTF_8));System.out.println( [x] Sent message );}}
}
请将代码中相关配置项设置为您自己的配置。
运行代码即可向RabbitMQ中的rc.queue队列发送一条Hello World消息。rc.queue为我自己创建的请根据实际情况调整 在RabbitMQ后台可以查看到该队列里的消息。
1.3 消费者代码-Recv.java
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.nio.charset.StandardCharsets;//消费者
public class Recv {public static void main(String[] argv) throws Exception {// 创建连接ConnectionFactory factory new ConnectionFactory();factory.setHost(【服务器地址】);factory.setPort(【端口默认5672】);factory.setUsername(【账号】);factory.setPassword(【密码】);factory.setVirtualHost(【虚拟主机】);// 创建连接通道Connection connection factory.newConnection();Channel channel connection.createChannel();String queueName rc.queue;channel.queueDeclare(queueName, true, false, false, null);System.out.println( [*] Waiting for messages. To exit press CTRLC);DeliverCallback deliverCallback (consumerTag, delivery) - {String message new String(delivery.getBody(), StandardCharsets.UTF_8);System.out.println( [x] Received message );};channel.basicConsume(queueName, true, deliverCallback, consumerTag - {});}
}
运行结果 2.其他模式
调通了1种模式其他模式类似的方式调试即可。RabbitMQ Tutorials | RabbitMQ 3.在Springboot中使用RabbitMQ
3.1 添加依赖
在pom.xml中添加Spring Boot的RabbitMQ依赖。
!--RabbitMQ--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId
/dependency 3.2 配置RabbitMQ
在application.properties或application.yml中配置RabbitMQ连接信息。
# application.properties
spring.rabbitmq.host【服务器地址】
spring.rabbitmq.port【端口默认5672】
spring.rabbitmq.username【用户名】
spring.rabbitmq.password【密码】
spring:rabbitmq:host: 【服务器地址】port: 【端口默认5672】username: 【用户名】password: 【密码】 配置Queue、Exchange和Binding
通过Java配置类定义消息队列、交换器和它们之间的绑定关系。
如果在RabbitMQ控制台设置好了Queue、Exchange和Binding无需下面的配置
Configuration
public class RabbitMQConfig {BeanQueue myQueue() {return new Queue(your-queue-name, true);}BeanDirectExchange myExchange() {return new DirectExchange(your-exchange);}BeanBinding binding(Queue myQueue, DirectExchange myExchange) {return BindingBuilder.bind(myQueue).to(myExchange).with(your-routingKey);}
}
3.3 创建消息生产者
定义一个简单的消息生产者类使用RabbitTemplate注解来发送消息到队列
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;Component
public class MessageProducer {private final RabbitTemplate rabbitTemplate;Autowiredpublic MessageProducer(RabbitTemplate rabbitTemplate) {this.rabbitTemplate rabbitTemplate;}public void sendMessage(String message) {rabbitTemplate.convertAndSend(your-queue-name, message);}
}
3.4.创建消息消费者
定义一个消息消费者类使用RabbitListener注解来监听特定的队列
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;Component
public class MessageConsumer {RabbitListener(queues your-queue-name)public void receiveMessage(String message) {System.out.println(Received message: message);}
}
3.5.启动和测试
确保你的应用启动类上有EnableRabbit注解启用RabbitMQ。确保你的RabbitMQ服务正在运行并尝试从你的应用中发送和接收消息。你可以通过调用MessageProducer中的sendMessage方法来测试发送功能而接收功能应该自动触发MessageConsumer中的receiveMessage方法。 文章转载自: http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.jpwkn.cn.gov.cn.jpwkn.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.kwqt.cn.gov.cn.kwqt.cn