当前位置:   article > 正文

SpringBoot项目配置多个RabbitMQ解决方案_springboot添加第二个rabbitmq时不影响第一个的配置

springboot添加第二个rabbitmq时不影响第一个的配置

背景介绍

目前有一个Spring Boot项目,已经接入一个RabbitMQ Broker,由于业务扩展,需要新增一个RabbitMQ Broker进行消费,由于单个Broker时通过Spring默认配置进行使用,因此需要做出修改

Java客户端开发Demo

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(IP_ADDRESS);
factory.setPort(PORT);
factory.setUsername("root");
factory.setPassword("root123");
factory.setVirtualHost("/");
// 创建链接
Connection connection = factory.newConnection();
// 创建信道,Connection 接口被用来创建一个Channel
Channel channel = connection.createChannel();
// 创建一个type="direct" 持久化、非自动删除的交换机
channel.exchangeDeclare(QUEUE_NAME, "direct", true, false, null);
// 创建一个持久化、非排他的、非自动删除的队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 发送一条持久化的消息
String message = "hello world!";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
// 关闭资源
channel.close();
connection.close();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

单实例配置

  1. pom
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
	<version>2.1.2.RELEASE</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 增加properties配置
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.host=host
spring.rabbitmq.virtual-host=/
spring.rabbitmq.port=port
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 增加配置类
@Configuration
public class RabbitMqConfiguration {
	@Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory,MessageConverter messageConverter){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        return factory;
    }

    @Bean
    public MessageConverter messageConverter(){
        ObjectMapper objectMapper = new ObjectMapper();
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(objectMapper);
        return jackson2JsonMessageConverter;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

多实例配置

  1. 增加properties配置
# 默认rabbit配置
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.host=host
spring.rabbitmq.virtual-host=/
spring.rabbitmq.port=port
# 第2个MQ配置
spring.rabbitmq.second.host=second.host
spring.rabbitmq.second.username=second.username
spring.rabbitmq.second.password=second.password
spring.rabbitmq.second.port=second.port
spring.rabbitmq.second.virtual-host=/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 修改配置类
/**
 * @Description:
 * @Author: admin
 * @Date: 2021/5/11 18:19
 */
@Configuration
public class RabbitMqConfiguration {
    @Primary
    @Bean(name = "rabbitConnectionFactory")
    public ConnectionFactory rabbitConnectionFactory(
            @Value("${spring.rabbitmq.host}") String host,
            @Value("${spring.rabbitmq.port}") int port,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password,
            @Value("${spring.rabbitmq.virtual-host}") String virtualHost) {
        return connectionFactory(host, port, username, password, virtualHost);
    }

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(
            @Qualifier("rabbitConnectionFactory") ConnectionFactory connectionFactory,
            MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        return factory;
    }

    /**
     * 定义连接
     * @param host
     * @param port
     * @param username
     * @param password
     * @return
     */
    @Bean(name = "secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory(
            @Value("${spring.rabbitmq.second.host}") String host,
            @Value("${spring.rabbitmq.second.port}") int port,
            @Value("${spring.rabbitmq.second.username}") String username,
            @Value("${spring.rabbitmq.second.password}") String password,
            @Value("${spring.rabbitmq.second.virtual-host}") String virtualHost) {
        return connectionFactory(host, port, username, password, virtualHost);
    }

    /**
     * 声明RabbitTemplate
     * @param connectionFactory
     * @return
     */
    @Bean(name = "secondRabbitTemplate")
    public RabbitTemplate dataadminmqRabbitTemplate(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate .setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate ;
    }
    
    @Bean(name = "secondRabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory secondRabbitListenerContainerFactory(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory,
            MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        return factory;
    }
    
    @Bean
    public MessageConverter messageConverter(){
        ObjectMapper objectMapper = new ObjectMapper();
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(objectMapper);
        return jackson2JsonMessageConverter;
    }

    /**
     * 声明连接工厂连接开发服务器
     *
     * @param host
     * @param port
     * @param username
     * @param password
     * @return
     */
    public CachingConnectionFactory connectionFactory(String host, int port, String username,
                                                      String password, String virtualHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/616987
推荐阅读
相关标签
  

闽ICP备14008679号