安装rabbitMQ 请看上一篇文章:
https://blog.csdn.net/weixin_42006112/article/details/102310464
直接上代码,看不懂的可以私聊我
pom.xml
<!--RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
application.yml:
spring:
#RabbitMQ
rabbitmq:
host: 192.168.*.*
port: 5672
username: admin
password: admin
假如你使用的是application.properties
spring.rabbitmq.host=192.168.*.*
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
RabbitMQConfig 初始化MQ
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class RabbitMQConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
public static final String EXCHANGE = "my-mq-exchange";
public static final String ALLEXCHANGE = "all-exchange";
public static final String QUEUE = "queue";
public static final String QUEUE_A = "queue_a";
public static final String QUEUE_B = "queue_b";
public static final String ROUTINGKEY = "spring-boot-routingKey";
/**
* 链接RabbitMQ
* @return
*/
@Bean
public CachingConnectionFactory connectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost("/");
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
/**
* 实例化RabbitMQ
* @return
*/
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate rabbitTemplate(){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
return rabbitTemplate;
}
/** 单一模式
* 针对消费者配置
* 1. 设置交换机类型
* 2. 将队列绑定到交换机
* FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念
* HeadersExchange :通过添加属性key-value匹配
* DirectExchange:按照routingkey分发到指定队列
* @return
*/
@Bean
public DirectExchange defaultExchange(){
return new DirectExchange(EXCHANGE);
}
/**
* 广播模式
* 交换机
* @return
*/
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange(ALLEXCHANGE);
}
/**
* 获取队列
* true :队列持久
* @return
*/
@Bean
public Queue queue(){
return new Queue(QUEUE,true);
}
@Bean
public Queue queueA(){
return new Queue(QUEUE_A,true);
}
@Bean
public Queue queueB() {
return new Queue(QUEUE_B, true);
}
//一个交换机可以绑定多个消息队列,也就是消息通过一个交换机,可以分发到不同的队列当中去。
@Bean
public Binding binding(){
return BindingBuilder.bind(queue()).to(defaultExchange()).with(RabbitMQConfig.ROUTINGKEY);
}
/**
* 这是广播模式的交换机
* @param queueA
* @param fanoutExchange
* @return
*/
@Bean
public Binding bindingA(Queue queueA,FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueA).to(fanoutExchange);
}
/**
* 这是广播模式的交换机
* @param queueB
* @param fanoutExchange
* @return
*/
@Bean
public Binding bindingB(Queue queueB, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueB).to(fanoutExchange);
}
}
消息生产者案例:
import com.jazzychen.myspringboot.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
public class MsgProducer implements RabbitTemplate.ConfirmCallback {
private RabbitTemplate rabbitTemplate;
@Autowired
public MsgProducer(RabbitTemplate rabbitTemplate){
this.rabbitTemplate = rabbitTemplate;
rabbitTemplate.setConfirmCallback(this);
}
public void sendMsg(String content){
System.out.println("------我要开始发送RabbitMQ-----");
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE,RabbitMQConfig.ROUTINGKEY, content,correlationId);
}
public void sendMsgAll(String content){
System.out.println("-------发送群发RabbitMQ-------");
rabbitTemplate.convertAndSend(RabbitMQConfig.ALLEXCHANGE,"",content);
}
/**
* 回调
* @param correlationData
* @param ack
* @param cause
*/
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if (ack) {
System.out.println("消费成功");
}else {
System.out.println("消费失败" + cause);
}
}
}
消费者单一接收
import com.jazzychen.myspringboot.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = RabbitMQConfig.QUEUE)
public class MsgReceiver {
@RabbitHandler
public void process(String content){
System.out.println("接收处理队列A的消息: "+content);
}
}
消费者接收广播信息:
import com.jazzychen.myspringboot.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = {RabbitMQConfig.QUEUE_A,RabbitMQConfig.QUEUE_B})
public class MsgReceiverA {
@RabbitHandler
public void process(String content){
System.out.println("接受广播信息:"+content);
}
}
最后附上我的结构图
温馨提示:当启动项目 报 :socket closed 错误 看一下你的账号是否和cyf 一样,假如一样那就是你的账号还未授权
解决方法 : 进入linux 服务器 输入: rabbitmqctl set_permissions -p /${admin} admin '.*' '.*' '.*' (admin 为你的账号,相对应改一下即可) 或者也有可能你的 配置文件 application.yml 不是 port:15672
接下来写一个 单元测试
import com.jazzychen.myspringboot.mq.MsgProducer;
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 MySpringBootApplicationTests {
@Autowired
private MsgProducer msgProducer;
@Test
public void RabbitTest(){
msgProducer.sendMsg("你好,RabbitMQ");
}
@Test
public void RabbitAllTest(){
msgProducer.sendMsgAll("你好,这是广播RabbitMQ");
}
}
测试成功~~~~~