spring boot rabbitmq 多MQ配置 自動 建立 隊列 RPC

 

  

源碼地址:https://github.com/hutuchong518/RabbitmqStudyjava

 

需求:   spring boot 整合 rabbitmq rpc功能, 須要將 請求和響應 這兩個隊列 分別放在不一樣的MQ服務器上,以提升單個MQ服務器的吞吐量和性能。git

 

MQ服務器1:  github

      IP:192.168.179.128spring

     對列:hello1服務器

    

MQ服務器2:  性能

      IP:172.16.16.218測試

     對列:hello2ui

 

這裏實現的關鍵 是  建立隊列 到 指定 MQ服務器中 網上一些文章 都是 一把輪 沒有區分,在實施上有問題的其實,這裏經過實踐並解決,以供參考。spa

 

下面是代碼:blog

    

package com.zhanghui;


import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


/**
 * @auther zhanghui
 * @date 2017/8/27 21:59
 * @desc
 */
@Configuration
public class RabbitConfig {

    @Bean(name="firstConnectionFactory")
    @Primary
    public ConnectionFactory firstConnectionFactory(
            @Value("${spring.rabbitmq.first.host}") String host,
            @Value("${spring.rabbitmq.first.port}") int port,
            @Value("${spring.rabbitmq.first.username}") String username,
            @Value("${spring.rabbitmq.first.password}") String password
    ){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @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
    ){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean(name="firstRabbitTemplate")
    //@Primary  //貌似沒用,移除
    public RabbitTemplate firstRabbitTemplate(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
        return firstRabbitTemplate;
    }

    @Bean(name="secondRabbitTemplate")
    public RabbitTemplate secondRabbitTemplate(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
        return secondRabbitTemplate;
    }

    @Bean(name="firstFactory")
    public SimpleRabbitListenerContainerFactory firstFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name="secondFactory")
    public SimpleRabbitListenerContainerFactory secondFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean
    public String firstQueue(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ) {
        System.out.println("configuration firstQueue ........................");
        //return new Queue("hello1");
        try {
            connectionFactory.createConnection().createChannel(false).queueDeclare("hello1", false, false, false, null);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return "firstQueue";
        }
    }

    @Bean
    public String secondQueue(
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ) {
        System.out.println("configuration secondQueue ........................");
        //return new Queue("hello2");
        try {
             connectionFactory.createConnection().createChannel(false).queueDeclare("hello2", false, false, false, null);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return "secondQueue";
        }
    }


    //下面2個對列建立方式 測試後發現不是 針對指定mq 服務器建立,只會在第一個服務器建立
    /*
    @Bean
    public Queue firstQueue() {
        System.out.println("configuration firstQueue ........................");
        return new Queue("hello1");
    }

    @Bean
    public Object secondQueue() {
        System.out.println("configuration secondQueue ........................");
        return new Queue("hello2");
    }
    */
}
相關文章
相關標籤/搜索