目前公司使用jeepluscloud版本,这个版本没有集成消息队列,这里记录一下,集成的过程;这个框架跟ruoyi的那个微服务版本结构一模一样,所以也可以快速上手。
配置类的东西做成一个公共的模块
rabbitmq模块:
jeeplus-common
org.jeeplus
${revision}
4.0.0
jeeplus-common-rabbitmq
jar
org.springframework.boot
spring-boot-starter-amqp
org.jeeplus
jeeplus-common-core
${project.parent.version}
package com.jeeplus.common.rabbit.conditional;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
/**
* 判断系统是否在启用了Rabbit, 未启用的情况下不将Bean注册到系统中
*
* 使用场景: 在不使用Rabbit中间件但未去除Rabbit依赖的情况下, 通过配置文件中关闭Rabbit选项,
* 同时将这个注解到有`@RabbitListener`标志的类上,让这个对象不注册到Spring容器中,
* 从而避免`RabbitMQ`进行无限尝试重连服务器,导致项目一直抛出异常,影响开发和使用。
*
* @author xxm
* @since 2022/12/12
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnRabbitEnable.class)
public @interface ConditionalOnRabbit {
}
package com.jeeplus.common.rabbit.conditional;
import com.jeeplus.common.rabbit.configuration.RabbitMqProperties;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* 判断是否在启用了Rabbit, 用来控制在没启用Rabbit情况下. 不将 @RabbitListener 修饰的监听器注册为Bean, 不然会导致无限尝试重连
*
* @author xxm
* @since 2022/12/12
*/
public class OnRabbitEnable implements Condition {
private final String rabbitPropertiesPrefix = "com.jeeplus.common.rabbit";
/**
* @param context
* @param metadata
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
RabbitMqProperties rabbitMqProperties = Binder.get(context.getEnvironment())
.bind(rabbitPropertiesPrefix, RabbitMqProperties.class)
.orElse(new RabbitMqProperties());
return rabbitMqProperties.isEnable();
}
}
package com.jeeplus.common.rabbit.configuration;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
/**
* Rabbit 侦听器配置器
*
* @author xxm
* @since 2021/6/25
*/
@Configuration
@RequiredArgsConstructor
public class BootxRabbitListenerConfigurer implements RabbitListenerConfigurer {
private final DefaultMessageHandlerMethodFactory jsonHandlerMethodFactory;
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(jsonHandlerMethodFactory);
}
}
package com.jeeplus.common.rabbit.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
/**
* 消息队列配置
*
* @author xxm
* @since 2021/6/25
*/
@EnableRabbit
@Configuration
public class RabbitMqConfigurer {
/**
* 注册 RabbitTemplate 对象, 使用默认序列化方式
*/
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, ObjectMapper objectMapper) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// 使用系统同版jackson 序列化配置
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
return rabbitTemplate;
}
/**
* 添加默认消息序列化方式, 使用默认序列化方式
*/
@Bean
public DefaultMessageHandlerMethodFactory jsonHandlerMethodFactory(ObjectMapper objectMapper) {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
// 这里的转换器设置实现了 通过 @Payload 注解 自动反序列化message body
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setObjectMapper(objectMapper);
factory.setMessageConverter(converter);
return factory;
}
}
package com.jeeplus.common.rabbit.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
/**
* 消息队列配置
*
* @author xxm
* @since 2021/6/25
*/
@EnableRabbit
@Configuration
public class RabbitMqConfigurer {
/**
* 注册 RabbitTemplate 对象, 使用默认序列化方式
*/
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, ObjectMapper objectMapper) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// 使用系统同版jackson 序列化配置
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
return rabbitTemplate;
}
/**
* 添加默认消息序列化方式, 使用默认序列化方式
*/
@Bean
public DefaultMessageHandlerMethodFactory jsonHandlerMethodFactory(ObjectMapper objectMapper) {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
// 这里的转换器设置实现了 通过 @Payload 注解 自动反序列化message body
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setObjectMapper(objectMapper);
factory.setMessageConverter(converter);
return factory;
}
}
package com.jeeplus.common.rabbit.configuration;
import com.jeeplus.common.rabbit.conditional.ConditionalOnRabbit;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* MQTT配置
*
* @author xxm
* @since 2022/12/12
*/
@Getter
@Setter
@ConfigurationProperties("com.jeeplus.common.rabbit")
public class RabbitMqProperties {
/**
* 是否开启 RabbitMQ功能,
* @see ConditionalOnRabbit 配合此注解使用
*/
private boolean enable = false;
}
package com.jeeplus.common.rabbit;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* RabbitMQ配置
*
* @author xxm
* @since 2022/5/3
*/
@SpringBootApplication
public class RabbitMqCommonAutoConfiguration {
}