相关文章推荐
刚毅的长颈鹿  ·  Spring 3.1 M2: ...·  2 周前    · 
温暖的海龟  ·  达梦spring-data-jdbc - ...·  5 天前    · 
爱运动的热带鱼  ·  Caused by: ...·  3 天前    · 
温柔的红薯  ·  CAS Authentication :: ...·  3 天前    · 
爱旅游的帽子  ·  20.1 OpenSSL ...·  6 月前    · 
本文链接: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
        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.配置文件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
                    本文链接:https://blog.csdn.net/qq_37608766/article/details/80912370https://blog.csdn.net/qq_37608766/article/details/809123701.application.ymlspring:  rabbitmq:   hospSync:    host: loc...
spring.rabbitmq.first.host=${mq.host}
spring.rabbitmq.first.port=${mq.port}
spring.rabbitmq.first.username=${mq.username}
spring.rabbitmq.first.password=${mq.password}
spring.rabbitmq.first.virtualHost=${mq.vhost}
				
SpringBoot 集成多个rabbitmq 在项目中经常遇到一个springboot工程要连接多个rabbitmq。如果只连接一个,springboot可以零配置连接rabbitmq,这样不需要做额外的工作。但如果连接多个rabbitmq,就得做一些配置了。 1 配置多个rabbitmq的连接地址: spring.rabbitmq.first.host=192.168.10.223 spring.rabbitmq.first.port=5672 spring.rabbitmq.first.usernam
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.ConnectionFact
  今天项目遇到一个问题,同一个应用中,需要消费不同的vhost中不同queue的消息,目前只实现了multi consumer,如果还需要向不同的集群或vhost发消息,那么应该需要配置不同的RabbitAdmin,不知道有没有坑。 rabbit的大概配置如下: 1. 起初项目只需要监听一个vhost中的queue,所以使用spring-boot-autoconfigure中的默认配置,直接...
上一遍博客介绍了使用spring-boot连接单个rabbitmq服务器发送和接收消息,但是在实际的项目中我们可能需要同时连接多个不同的rabbitmq服务器发送和接收消息。今天简单介绍下如何使用spring-boot连个多个rabbitmq服务器发送和接收消息。 1.配置文件中配置2个rabbitmq连接信息(这里我在2台虚拟机上安装rabbitmq) server: port: 8091
Spring Boot可以很容易地配置多个RabbitMQ实例。以下是配置多个RabbitMQ实例的步骤: 1. 在application.properties文件中添加多个RabbitMQ实例的配置信息。例如: spring.rabbitmq.host=host1 spring.rabbitmq.port=5672 spring.rabbitmq.username=user1 spring.rabbitmq.password=password1 spring.rabbitmq.second.host=host2 spring.rabbitmq.second.port=5672 spring.rabbitmq.second.username=user2 spring.rabbitmq.second.password=password2 2. 创建多个RabbitTemplate和ConnectionFactory bean。例如: @Configuration public class RabbitMQConfig { @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost(env.getProperty("spring.rabbitmq.host")); connectionFactory.setPort(env.getProperty("spring.rabbitmq.port", Integer.class)); connectionFactory.setUsername(env.getProperty("spring.rabbitmq.username")); connectionFactory.setPassword(env.getProperty("spring.rabbitmq.password")); return connectionFactory; @Bean public RabbitTemplate rabbitTemplate() { RabbitTemplate template = new RabbitTemplate(connectionFactory()); return template; @Bean(name = "secondConnectionFactory") public ConnectionFactory secondConnectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost(env.getProperty("spring.rabbitmq.second.host")); connectionFactory.setPort(env.getProperty("spring.rabbitmq.second.port", Integer.class)); connectionFactory.setUsername(env.getProperty("spring.rabbitmq.second.username")); connectionFactory.setPassword(env.getProperty("spring.rabbitmq.second.password")); return connectionFactory; @Bean(name = "secondRabbitTemplate") public RabbitTemplate secondRabbitTemplate() { RabbitTemplate template = new RabbitTemplate(secondConnectionFactory()); return template; 3. 在需要使用RabbitMQ的地方,注入对应的RabbitTemplate或ConnectionFactory bean即可。例如: @Autowired private RabbitTemplate rabbitTemplate; @Autowired @Qualifier("secondRabbitTemplate") private RabbitTemplate secondRabbitTemplate; 使用以上步骤,就可以在Spring Boot应用中配置多个RabbitMQ实例了。