springboot配置多个rabbitmq

碰到个场景,有个服务内部需要一个自用的rabbitmq,同时也需要调用第三方的消息,需要连接第三方的mq。故问题就变成了springboot如何配置多个rabbitmq。这里记录一下从其他博客看到的配置方法:
链接:springboot配置多个rabbitmq

本文链接:https://blog.csdn.net/qq_37608766/article/details/80912370
 
https://blog.csdn.net/qq_37608766/article/details/80912370
 
 
1.application.yml
spring:
    rabbitmq:
      hospSync:
        host: localhost
        port: 5672
        username: test
        password: test
        virtual-host: test
      pay:
        host: localhost
        port: 5672
        username: demo
        password: demo
        virtual-host: demo
2.配置mq
package com.medrd.hpay.config;
 
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;
 
@Configuration
public class RabbitMqConfiguration{
 
    @Bean(name="hospSyncConnectionFactory")
    @Primary
    public ConnectionFactory hospSyncConnectionFactory(
            @Value("${spring.rabbitmq.hospSync.host}") String host,
            @Value("${spring.rabbitmq.hospSync.port}") int port,
            @Value("${spring.rabbitmq.hospSync.username}") String username,
            @Value("${spring.rabbitmq.hospSync.password}") String password,
            @Value("${spring.rabbitmq.hospSync.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
 
        return connectionFactory;
    }
 
    @Bean(name="hPayConnectionFactory")
    public ConnectionFactory hPayConnectionFactory(
            @Value("${spring.rabbitmq.pay.host}") String host,
            @Value("${spring.rabbitmq.pay.port}") int port,
            @Value("${spring.rabbitmq.pay.username}") String username,
            @Value("${spring.rabbitmq.pay.password}") String password,
            @Value("${spring.rabbitmq.pay.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }
 
    @Bean(name="hospSyncRabbitTemplate")
//    @Primary
    public RabbitTemplate firstRabbitTemplate(
            @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate hospSyncRabbitTemplate = new RabbitTemplate(connectionFactory);
        //使用外部事物
        //ydtRabbitTemplate.setChannelTransacted(true);
        return hospSyncRabbitTemplate;
    }
 
    @Bean(name="hPayRabbitTemplate")
    @Primary
    public RabbitTemplate secondRabbitTemplate(
            @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate hPayRabbitTemplate = new RabbitTemplate(connectionFactory);
        //使用外部事物
        //lpzRabbitTemplate.setChannelTransacted(true);
        return hPayRabbitTemplate;
    }
 
        @Bean(name="hospSyncContainerFactory")
    public SimpleRabbitListenerContainerFactory hospSyncFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("hospSyncConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
 
 
    @Bean(name="hPayContainerFactory")
    public SimpleRabbitListenerContainerFactory hPayFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("hPayConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
 
}
 
 
3.监听(消费方)
 
package com.medrd.hpay.domain;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
 
@Component
public class RabbitMqMessage {
 
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqMessage.class);
 
    @RabbitListener(queues = {"test.hospital.add","test.hospital.update","test.hospital.delete"}, containerFactory="hospSyncContainerFactory")
    public void hospitalAdd(@Header("amqp_receivedRoutingKey") String routeKey, Object data) {
        logger.debug("Reciver routeKey: {}, data: {}", data);
    }
 
    @RabbitListener(queues = {"demo.add","demo.update"}, containerFactory="hPayContainerFactory")
    public void hPayAdd(@Header("amqp_receivedRoutingKey") String routeKey, String data) {
        System.out.println(data);
        System.out.println();
    }
 
 
}
4.生产者(消息生产方
 
package com.paas.springboot.demo01;
 
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
 
@Component
public class Sender {
 
    @Resource(name="hospSyncRabbitTemplate")
    private RabbitTemplate hospSyncRabbitTemplate;
 
    public void send1() {
        String context = "hello1 " + new Date();
        System.out.println("Sender : " + context);
        this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
    }
 
    public void send2() {
        String context = "hello2 " + new Date();
        System.out.println("Sender : " + context);
        this.firstRabbitTemplate.convertAndSend("test.hospital.add", context);
    }
 
}
 
5.配置文件
 

    org.springframework.boot
    spring-boot-starter-amqp

直接就是 一顿粘贴。

但是关于配置多个rabbitmq还有一些细节,比如序列化和反序列化的方式如何设置本文是没有体现的。留个坑以后更新。

你可能感兴趣的:(工具和使用技巧)