微服务框架SpringcloudAlibaba+Nacos集成RabbiMQ

目前公司使用jeepluscloud版本,这个版本没有集成消息队列,这里记录一下,集成的过程;这个框架跟ruoyi的那个微服务版本结构一模一样,所以也可以快速上手。

1.项目结构图:

配置类的东西做成一个公共的模块

rabbitmq模块:

2.核心配置

1.pom类

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jeeplus-common</artifactId>
        <groupId>org.jeeplus</groupId>
        <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jeeplus-common-rabbitmq</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jeeplus</groupId>
            <artifactId>jeeplus-common-core</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

    </dependencies>
</project>

2.ConditionalOnRabbit

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 {

}

3.OnRabbitEnable

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();
    }

}

4.BootxRabbitListenerConfigurer

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);
    }

}

5.BootxRabbitListenerConfigurer

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;
    }

}

6.RabbitMqConfigurer

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;
    }

}

7.RabbitMqProperties

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;

}

8.RabbitMqCommonAutoConfiguration

package com.jeeplus.common.rabbit;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * RabbitMQ配置
 *
 * @author xxm
 * @since 2022/5/3
 */
@SpringBootApplication
public class RabbitMqCommonAutoConfiguration {

}
相关推荐
politeboy1 小时前
关于k8s中镜像的服务端口被拒绝的问题
云原生·容器·kubernetes
weixin_438197381 小时前
K8S创建云主机配置docker仓库
linux·云原生·容器·eureka·kubernetes
问道飞鱼11 小时前
【微服务知识】开源RPC框架Dubbo入门介绍
微服务·rpc·开源·dubbo
白总Server12 小时前
JVM解说
网络·jvm·物联网·安全·web安全·架构·数据库架构
ggaofeng12 小时前
通过命令学习k8s
云原生·容器·kubernetes
CodingBrother12 小时前
软考之面向服务架构SOA
微服务·架构
qq_道可道15 小时前
K8S升级到1.24后,切换运行时导致 dind 构建镜像慢根因定位与解决
云原生·容器·kubernetes
郝同学的测开笔记18 小时前
云原生探索系列(十二):Go 语言接口详解
后端·云原生·go
mit6.82419 小时前
[Docker#5] 镜像仓库 | 命令 | 实验:搭建Nginx | 创建私有仓库
linux·后端·docker·云原生
随遇而安622&50820 小时前
分布式微服务项目,同一个controller方法间的转发导致cookie丢失,报错null pointer异常
分布式·微服务·架构·bug