rabbitmq的基本使用

本文介绍如何使用Spring框架整合RabbitMQ,包括配置RabbitMQ连接、定义交换机及队列、实现消息发送与接收等功能。通过示例代码演示整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是rabbitmq

RabbitMQ是一个在AMQP基础上完成的,可复用的企业消息系统

2.rabbitmq的安装

先安装erlang语言包  在安装rabbitmq 

3.使用spring整合rabbitmq

 3.1 服务端的配置

3.1.1 rabbitmq.properties文件

#rabbitmq的基本配置
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=zy
rabbitmq.password=zy123456
rabbitmq.vhost=/zy

3.1.2 依赖包的引入

<dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>

3.1.3 整合配置

//配置文件类

package com.zy.rabbitmq;


import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("com.zy.rabbitmq")
@PropertySource("classpath:/rabbitmq.properties")
public class AppConfig {
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private int port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.vhost}")
private String vhost;


@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}

//配置mq连接工厂
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public ConnectionFactory  connectionFactory (){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vhost);
return connectionFactory;
}
//管理
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public RabbitAdmin  rabbitAdmin(){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
//rabbitAdmin.declareQueue(queue());
//rabbitAdmin.declareExchange(topicExchange());
//rabbitAdmin.declareBinding(BindingBuilder.bind(queue()).to(topicExchange()).with(""));
return rabbitAdmin;
}
/*@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public Queue  queue (){
Queue queue = new Queue("myQueue");
return queue;
}*/

//交换机的设置
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public TopicExchange  topicExchange (){
TopicExchange exchange = new TopicExchange("myExchange");
return exchange;

}

//Java操作mq的模板
@Bean
public RabbitTemplate rabbitTemplate(){
RabbitTemplate template = new RabbitTemplate(connectionFactory ());
return template;
}
}

3.1.4 消息发送类

import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Send  {
@Autowired
private RabbitTemplate template;
public void sendMsg() throws InterruptedException{
Map<String,Object> map =new HashMap<String, Object>();
map.put("username", "张三");
map.put("password", "123456");
map.put("type", System.currentTimeMillis());
JSONObject  object = JSONObject .fromObject(map);
String json = object.toString();
template.convertAndSend("myExchange","update",json); //消息的发送
System.out.println("消息已发出");
}
}

3.2 服务层的配置

3.2.1 依赖的引入

<dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>

3.2.2 配置参数文件

rabbitmq.properties

rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=zy
rabbitmq.password=zy123456
rabbitmq.vhost=/zy

3.2.3 整合配置

package com.zy.forward;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("com.zy.forward")
@PropertySource("classpath:/rabbitmq.properties")
public class AppConfig {
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private int port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.vhost}")
private String vhost;

//配置mq连接工厂
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public ConnectionFactory  connectionFactory (){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vhost);
return connectionFactory;
}
//管理
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public RabbitAdmin  rabbitAdmin(){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
//rabbitAdmin.declareQueue(queue());
//rabbitAdmin.declareExchange(topicExchange());
//rabbitAdmin.declareBinding(BindingBuilder.bind(queue()).to(topicExchange()).with(""));
return rabbitAdmin;
}
/*@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public Queue  queue (){
Queue queue = new Queue("myQueue");
return queue;
}*/
/* @Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
public TopicExchange  topicExchange (){
TopicExchange exchange = new TopicExchange("myExchange");
return exchange;

}
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
}

@Bean
public SimpleMessageListenerContainer container (){
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory ());
container.setQueueNames("myQueue");
container.setMessageListener(new MessageListenerAdapter(demoMessageListener()));
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
return container;
}

@Bean
public DemoMessageListener demoMessageListener(){
return new DemoMessageListener();
}
}

3.2.4 接收消息类

package com.zy.forward;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import com.rabbitmq.client.Channel;
public class DemoMessageListener implements ChannelAwareMessageListener{
@Override
public void onMessage(Message message, Channel channel) throws Exception {
String messageBody = new String(message.getBody(), "UTF-8");
System.out.println(messageBody);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值