来源个人博客橙寂博客
看这边博客之前,您必须已经正确安装了rabbitmq。具体怎么装可以参考我上篇文章docker中安装RabbitMq
RabbitMq安装好了,接下来就是怎么去使用它了。在springboot中整合他并且使用他是非常方便的。话不多说。直接上步骤
## ip
spring.rabbitmq.host=192.168.25.128
## 用户名
spring.rabbitmq.username=guest
## 密码
spring.rabbitmq.password=guest
##端口默认端口可省略
spring.rabbitmq.port=5672
按照上面的配置,最基础的整合RabbitMq便完成了。下面测试一下
1.创建监听Listener
package com.janhe.springbootrabbitmq.rabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @CLASSNAME RabbitListener
* @Description
* @Auther Jan 橙寂
* @DATE 2019/6/2 0002 17:07
*/
@Component
public class MyRabbitListener {
@Autowired
RabbitTemplate rabbitTemplate;
@RabbitListener(queues = "janhe.news") //这边监听了这个队列
public void recieve(Object obj)
{
System.out.println("收到消息 = [" + obj + "]");
}
}
2.主启动类加上@EnableRabbit //开启rabbit注解只有在启动类加上这个上面的才会生效
在springboot中操作消息的对象为RabbitTemplate只需要在你要的地方注入进去就行了
3.编写测试方法(测试类)
package com.janhe.springbootrabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
/**
* 1.RabbitAutoConfiguration 自动化配置类
* 2.ConnectionFactory 自动化连接工厂
* 3.配置文件 RabbitProperties
* 4.RabbitTemplate 消息操作类
* 5.AmqpAdmin 交换机创建类
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/**
* 测试点对点
*/
@Test
public void testDirect() {
/**
* String exchange, 交换机
* String routingKey, 路由键
* final Object object 内容实体
*
*/
rabbitTemplate.convertAndSend("janhe.direct","janhe.news","dasd1");
}
/**
* 测试广播
* 所有绑定了这个交互机的都能收到
*/
@Test
public void testFanout() {
Map data=new HashMap<>();
data.put("dad",123);
data.put("da",1455);
/**
* String exchange, 交换机
* String routingKey, 路由键
* final Object object 内容实体
*
*/
rabbitTemplate.convertAndSend("janhe.fanout","janhe.news",data);
}
/**
* 测试通知
* 符合路由键规则的能收到
*/
@Test
public void testTopic() {
/**
* String exchange, 交换机
* String routingKey, 路由键
* final Object object 内容实体
*
*/
rabbitTemplate.convertAndSend("janhe.topic","janhe.news","dasd1");
}
//测试接收
@Test
public void recieveDirect() {
/**
*
* queueName 队列名
*
*
*/
Object o = rabbitTemplate.receiveAndConvert("janhe.news");
System.out.println(o.toString());
}
}
如果不懂这些原理的可以参考我的上一篇文章消息服务RabbitMQ
做个补充如果不知道为什么是这样配置的找到RabbitAutoConfiguration这个配置类去看源码就知道为什么了。这就关联到了springboot的自动装配装置。springboot启动后会去找这个类RabbitAutoConfiguration 有兴趣的朋友可以看一下这个源码
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.amqp;
import java.time.Duration;
import java.util.stream.Collectors;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionNameStrategy;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
*
* This configuration class is active only when the RabbitMQ and Spring AMQP client
* libraries are on the classpath.
*
* Registers the following beans:
*
* - {@link org.springframework.amqp.rabbit.core.RabbitTemplate RabbitTemplate} if there
* is no other bean of the same type in the context.
* - {@link org.springframework.amqp.rabbit.connection.CachingConnectionFactory
* CachingConnectionFactory} instance if there is no other bean of the same type in the
* context.
* - {@link org.springframework.amqp.core.AmqpAdmin } instance as long as
* {@literal spring.rabbitmq.dynamic=true}.
*
*
* The {@link org.springframework.amqp.rabbit.connection.CachingConnectionFactory} honors
* the following properties:
*
* - {@literal spring.rabbitmq.port} is used to specify the port to which the client
* should connect, and defaults to 5672.
* - {@literal spring.rabbitmq.username} is used to specify the (optional) username.
*
* - {@literal spring.rabbitmq.password} is used to specify the (optional) password.
*
* - {@literal spring.rabbitmq.host} is used to specify the host, and defaults to
* {@literal localhost}.
* - {@literal spring.rabbitmq.virtualHost} is used to specify the (optional) virtual
* host to which the client should connect.
*
*
* @author Greg Turnquist
* @author Josh Long
* @author Stephane Nicoll
* @author Gary Russell
* @author Phillip Webb
* @author Artsiom Yudovin
*/
@Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {
@Configuration
@ConditionalOnMissingBean(ConnectionFactory.class)
protected static class RabbitConnectionFactoryCreator {
//注册连接工厂
@Bean
public CachingConnectionFactory rabbitConnectionFactory(
//这个就是我们在application.properties配置的东西这个是配置类
RabbitProperties properties,
ObjectProvider connectionNameStrategy)
throws Exception {
PropertyMapper map = PropertyMapper.get();
CachingConnectionFactory factory = new CachingConnectionFactory(
getRabbitConnectionFactoryBean(properties).getObject());
map.from(properties::determineAddresses).to(factory::setAddresses);
map.from(properties::isPublisherConfirms).to(factory::setPublisherConfirms);
map.from(properties::isPublisherReturns).to(factory::setPublisherReturns);
RabbitProperties.Cache.Channel channel = properties.getCache().getChannel();
map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize);
map.from(channel::getCheckoutTimeout).whenNonNull().as(Duration::toMillis)
.to(factory::setChannelCheckoutTimeout);
RabbitProperties.Cache.Connection connection = properties.getCache()
.getConnection();
map.from(connection::getMode).whenNonNull().to(factory::setCacheMode);
map.from(connection::getSize).whenNonNull()
.to(factory::setConnectionCacheSize);
map.from(connectionNameStrategy::getIfUnique).whenNonNull()
.to(factory::setConnectionNameStrategy);
return factory;
}
private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(
RabbitProperties properties) throws Exception {
PropertyMapper map = PropertyMapper.get();
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
map.from(properties::determineHost).whenNonNull().to(factory::setHost);
map.from(properties::determinePort).to(factory::setPort);
map.from(properties::determineUsername).whenNonNull()
.to(factory::setUsername);
map.from(properties::determinePassword).whenNonNull()
.to(factory::setPassword);
map.from(properties::determineVirtualHost).whenNonNull()
.to(factory::setVirtualHost);
map.from(properties::getRequestedHeartbeat).whenNonNull()
.asInt(Duration::getSeconds).to(factory::setRequestedHeartbeat);
RabbitProperties.Ssl ssl = properties.getSsl();
if (ssl.isEnabled()) {
factory.setUseSSL(true);
map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
map.from(ssl::getKeyStore).to(factory::setKeyStore);
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
map.from(ssl::getTrustStore).to(factory::setTrustStore);
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
map.from(ssl::isValidateServerCertificate).to((validate) -> factory
.setSkipServerCertificateValidation(!validate));
map.from(ssl::getVerifyHostname)
.to(factory::setEnableHostnameVerification);
}
map.from(properties::getConnectionTimeout).whenNonNull()
.asInt(Duration::toMillis).to(factory::setConnectionTimeout);
factory.afterPropertiesSet();
return factory;
}
}
@Configuration
@Import(RabbitConnectionFactoryCreator.class)
protected static class RabbitTemplateConfiguration {
private final RabbitProperties properties;
private final ObjectProvider messageConverter;
private final ObjectProvider retryTemplateCustomizers;
public RabbitTemplateConfiguration(RabbitProperties properties,
ObjectProvider messageConverter,
ObjectProvider retryTemplateCustomizers) {
this.properties = properties;
this.messageConverter = messageConverter;
this.retryTemplateCustomizers = retryTemplateCustomizers;
}
//这里注入了消息操作模板RabbitTemplate
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnMissingBean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
PropertyMapper map = PropertyMapper.get();
RabbitTemplate template = new RabbitTemplate(connectionFactory);
MessageConverter messageConverter = this.messageConverter.getIfUnique();
if (messageConverter != null) {
template.setMessageConverter(messageConverter);
}
template.setMandatory(determineMandatoryFlag());
RabbitProperties.Template properties = this.properties.getTemplate();
if (properties.getRetry().isEnabled()) {
template.setRetryTemplate(new RetryTemplateFactory(
this.retryTemplateCustomizers.orderedStream()
.collect(Collectors.toList())).createRetryTemplate(
properties.getRetry(),
RabbitRetryTemplateCustomizer.Target.SENDER));
}
map.from(properties::getReceiveTimeout).whenNonNull().as(Duration::toMillis)
.to(template::setReceiveTimeout);
map.from(properties::getReplyTimeout).whenNonNull().as(Duration::toMillis)
.to(template::setReplyTimeout);
map.from(properties::getExchange).to(template::setExchange);
map.from(properties::getRoutingKey).to(template::setRoutingKey);
map.from(properties::getDefaultReceiveQueue).whenNonNull()
.to(template::setDefaultReceiveQueue);
return template;
}
private boolean determineMandatoryFlag() {
Boolean mandatory = this.properties.getTemplate().getMandatory();
return (mandatory != null) ? mandatory : this.properties.isPublisherReturns();
}
//这里注入了操作rabbit组件的AmqpAdmin
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic",
matchIfMissing = true)
@ConditionalOnMissingBean
public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
@Configuration
@ConditionalOnClass(RabbitMessagingTemplate.class)
@ConditionalOnMissingBean(RabbitMessagingTemplate.class)
@Import(RabbitTemplateConfiguration.class)
protected static class MessagingTemplateConfiguration {
@Bean
@ConditionalOnSingleCandidate(RabbitTemplate.class)
public RabbitMessagingTemplate rabbitMessagingTemplate(
RabbitTemplate rabbitTemplate) {
return new RabbitMessagingTemplate(rabbitTemplate);
}
}
}