码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMQ 配置

时间:2020-10-29 10:36:51      阅读:26      评论:0      收藏:0      [点我收藏+]

标签:convert   mqc   tom   amp   autowired   dmi   bean   enabled   文件配置   

1、下面讲解RabbitMQ 配置:
截图如下:

技术图片

代码如下:

@Configuration
public class RabbitMQConfig implements RabbitListenerConfigurer {
private static final Logger logger = LoggerFactory.getLogger(RabbitMQConfig.class);

// 资源owner账户 ID 信息
@Value("${business.rabbitmq.resourceOwnerId:0}")
private String RESOURCE_OWNER_ID;

@Autowired
private RabbitProperties rabbitProperties;

@Value("${rabbitmq.example}")
private Integer rabbitMQInstance;

@Bean
public ConnectionFactory connectionFactory() {
    if (rabbitMQInstance == 1) {
        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
        rabbitConnectionFactory.setHost(rabbitProperties.getHost());
        rabbitConnectionFactory.setPort(rabbitProperties.getPort());
        rabbitConnectionFactory.setVirtualHost(rabbitProperties.getVirtualHost());
        AliyunCredentialsProvider credentialsProvider =
                new AliyunCredentialsProvider(rabbitProperties.getUsername(), rabbitProperties.getPassword(),
                        RESOURCE_OWNER_ID);
        rabbitConnectionFactory.setCredentialsProvider(credentialsProvider);
        rabbitConnectionFactory.setAutomaticRecoveryEnabled(true);
        rabbitConnectionFactory.setNetworkRecoveryInterval(5000);
        ConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitConnectionFactory);
        ((CachingConnectionFactory) connectionFactory).setPublisherConfirms(rabbitProperties.isPublisherConfirms());
        ((CachingConnectionFactory) connectionFactory).setPublisherReturns(rabbitProperties.isPublisherReturns());
        logger.info("Create AMQP connection Factory.");
        return connectionFactory;
    }

    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitProperties.getHost(),rabbitProperties.getPort());
    connectionFactory.setUsername(rabbitProperties.getUsername());
    connectionFactory.setPassword(rabbitProperties.getPassword());
    connectionFactory.setPublisherConfirms(rabbitProperties.isPublisherConfirms());
    connectionFactory.setPublisherReturns(rabbitProperties.isPublisherReturns());
    return connectionFactory;

}

@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
    return new Jackson2JsonMessageConverter();
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
    registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}

@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
    DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
    messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
    return messageHandlerMethodFactory;
}

@Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
    return new MappingJackson2MessageConverter();
}

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    factory.setConnectionFactory(connectionFactory);
    return factory;
}

}

2、yml 文件配置:
截图如下:

技术图片

代码配置如下:

spring:
rabbitmq:
host: 127.0.1
username: admin
password: 21323
virtual-host: test
listener:
simple:
retry:
max-attempts: 5 #最大重试次数
enabled: true
initial-interval: 20s
default-requeue-rejected: false

rabbitmq:
example: 1 # 1:阿里云AMPQ,0:rabbitMQ

3、配置生产者

@Configuration
public class RabbitConfiguration {

@Bean(name = "mq.test.01")
FanoutExchange exchange() {
    return new FanoutExchange("mq.test.01", false, true);
}

}

RabbitMQ 配置

标签:convert   mqc   tom   amp   autowired   dmi   bean   enabled   文件配置   

原文地址:https://blog.51cto.com/7218743/2544676

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!