美文网首页
SpringBoot下RabbitMQ多数据源配置

SpringBoot下RabbitMQ多数据源配置

作者: PC_Repair | 来源:发表于2020-06-19 10:42 被阅读0次
    • RabbitMqConfig
    package com.cisco.webex.cmse.soa.soaservice.configuration;
    
    import org.aopalliance.aop.Advice;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.amqp.core.AmqpAdmin;
    import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
    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.RabbitAdmin;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler;
    import org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException;
    import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
    import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
    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;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.util.backoff.FixedBackOff;
    
    @Configuration
    @PropertySource(value = "classpath:application.properties")
    public class RabbitMqConfig {
    
        @Value("${soa.rabbitmq.host}")
        private String soa_host;
        @Value("${soa.rabbitmq.port}")
        private String soa_port;
        @Value("${soa.rabbitmq.username}")
        private String soa_name;
        @Value("${soa.rabbitmq.password}")
        private String soa_password;
    
        @Value("${messageproxy.rabbitmq.host}")
        private String message_proxy_host;
        @Value("${messageproxy.rabbitmq.port}")
        private String message_proxy_port;
        @Value("${messageproxy.rabbitmq.username}")
        private String message_proxy_name;
        @Value("${messageproxy.rabbitmq.password}")
        private String message_proxy_password;
    
        public String getSoa_host() {
            return soa_host;
        }
    
        public void setSoa_host(String soa_host) {
            this.soa_host = soa_host;
        }
    
        public String getSoa_port() {
            return soa_port;
        }
    
        public void setSoa_port(String soa_port) {
            this.soa_port = soa_port;
        }
    
        public String getSoa_name() {
            return soa_name;
        }
    
        public void setSoa_name(String soa_name) {
            this.soa_name = soa_name;
        }
    
        public String getSoa_password() {
            return soa_password;
        }
    
        public void setSoa_password(String soa_password) {
            this.soa_password = soa_password;
        }
    
        public String getMessage_proxy_host() {
            return message_proxy_host;
        }
    
        public void setMessage_proxy_host(String message_proxy_host) {
            this.message_proxy_host = message_proxy_host;
        }
    
        public String getMessage_proxy_port() {
            return message_proxy_port;
        }
    
        public void setMessage_proxy_port(String message_proxy_port) {
            this.message_proxy_port = message_proxy_port;
        }
    
        public String getMessage_proxy_name() {
            return message_proxy_name;
        }
    
        public void setMessage_proxy_name(String message_proxy_name) {
            this.message_proxy_name = message_proxy_name;
        }
    
        public String getMessage_proxy_password() {
            return message_proxy_password;
        }
    
        public void setMessage_proxy_password(String message_proxy_password) {
            this.message_proxy_password = message_proxy_password;
        }
    
        // RabbitMQ Connection Config
        @Bean(name="messageProxyConnectFactory")
        public ConnectionFactory messageProxyConnectFactory() {
            int port = Integer.valueOf(message_proxy_port);
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setAddresses(message_proxy_host);
            connectionFactory.setUsername(message_proxy_name);
            connectionFactory.setPassword(message_proxy_password);
            connectionFactory.setPort(port);
            //connectionFactory.setVirtualHost(rpbvhost);
            return connectionFactory;
        }
    
        @Bean(name="soaConnectFactory")
        @Primary
        public ConnectionFactory soaConnectFactory() {
            int port = Integer.valueOf(soa_port);
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setAddresses(soa_host);
            connectionFactory.setUsername(soa_name);
            connectionFactory.setPassword(soa_password);
            connectionFactory.setPort(port);
            return connectionFactory;
        }
    
        // RabbitMQ Listener Config
        @Bean("soaRabbitListenerContainerFactory")
        public SimpleRabbitListenerContainerFactory soaRabbitListenerContainerFactory(@Qualifier("soaConnectFactory") ConnectionFactory connectionFactory,
                                                                                         SimpleRabbitListenerContainerFactoryConfigurer configurer) {
            SimpleRabbitListenerContainerFactory container = new SimpleRabbitListenerContainerFactory();
            container.setConnectionFactory(connectionFactory);
            //container.setMessageConverter(jackson2MessageConverter());
            container.setMessageConverter(new Jackson2JsonMessageConverter());
            container.setErrorHandler(errorHandler());
            container.setAdviceChain(retries());
    
            FixedBackOff recoveryBackOff = new FixedBackOff(30000, FixedBackOff.UNLIMITED_ATTEMPTS);
            container.setRecoveryBackOff(recoveryBackOff);
    
            configurer.configure(container, connectionFactory);
    
            return container;
        }
    
        @Bean("messageProxyRabbitListenerContainerFactory")
        public SimpleRabbitListenerContainerFactory messageProxyRabbitListenerContainerFactory(@Qualifier("messageProxyConnectFactory") ConnectionFactory connectionFactory,
                                                                                      SimpleRabbitListenerContainerFactoryConfigurer configurer) {
            SimpleRabbitListenerContainerFactory container = new SimpleRabbitListenerContainerFactory();
            container.setConnectionFactory(connectionFactory);
            //container.setMessageConverter(jackson2MessageConverter());
            container.setErrorHandler(errorHandler());
            container.setAdviceChain(retries());
    
            FixedBackOff recoveryBackOff = new FixedBackOff(30000, FixedBackOff.UNLIMITED_ATTEMPTS);
            container.setRecoveryBackOff(recoveryBackOff);
    
            configurer.configure(container, connectionFactory);
    
            return container;
        }
    
    
        // RabbitMQ Template Config
        @Bean("soaRabbitTemplate")
        public RabbitTemplate soaRabbitTemplate(@Qualifier("soaConnectFactory") ConnectionFactory connectionFactory) {
            final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            return rabbitTemplate;
        }
    
        @Bean("messageProxyRabbitTemplate")
        public RabbitTemplate messageProxyRabbitTemplate(@Qualifier("messageProxyConnectFactory") ConnectionFactory connectionFactory) {
            final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
            //rabbitTemplate.setMessageConverter(jackson2MessageConverter());
            return rabbitTemplate;
        }
    
        // RabbotMQ Message Proxy Config
        @Bean(name="soaAmqpAdmin")
        public AmqpAdmin amqpAdmin(@Qualifier("soaConnectFactory") ConnectionFactory connectionFactory) {
            RabbitAdmin admin = new RabbitAdmin(connectionFactory);
            admin.setAutoStartup(false);
            return admin;
        }
    
        @Bean(name="messageProxyAmqpAdmin")
        public AmqpAdmin rpdAmqpAdmin(@Qualifier("messageProxyConnectFactory") ConnectionFactory connectionFactory) {
            RabbitAdmin admin = new RabbitAdmin(connectionFactory);
            admin.setAutoStartup(false);
            return admin;
        }
    
        private Advice retries() {
            return RetryInterceptorBuilder.stateless().maxAttempts(10)
                    .backOffOptions(1000, 2.0, 10000)
                    .recoverer(new RejectAndDontRequeueRecoverer()).build();
        }
    
    //  @Bean
    //  public Jackson2JsonMessageConverter jackson2MessageConverter() {
    //      return new Jackson2JsonMessageConverter();
    //  }
    
        @Bean
        public ConditionalRejectingErrorHandler errorHandler() {
            return new ConditionalRejectingErrorHandler(new RabbitMqConfig.RabbitMqFatalExceptionStrategy());
        }
    
        public static class RabbitMqFatalExceptionStrategy extends ConditionalRejectingErrorHandler.DefaultExceptionStrategy {
            private final Logger logger = LoggerFactory.getLogger(RabbitMqConfig.RabbitMqFatalExceptionStrategy.class);
    
            @Override
            public boolean isFatal(Throwable t) {
                if (t instanceof ListenerExecutionFailedException) {
                    ListenerExecutionFailedException executionFailedException = (ListenerExecutionFailedException) t;
                    logger.error("Failed to process inbound message from queue "
                            + executionFailedException.getFailedMessage().getMessageProperties().getConsumerQueue()
                            + "; failed message: " + executionFailedException.getFailedMessage(), t);
                }
                return super.isFatal(t);
            }
        }
    }
    
    #rabbitmq
    soa.rabbitmq.host = 67.224.122.109
    soa.rabbitmq.port = 5672
    soa.rabbitmq.username = admin
    soa.rabbitmq.password = 123456
    slm.listen.queue.name = slm.jeffrey.queue
    sparkbot.listen.queue.name = slm.jeffrey.spark.queue
    
    messageproxy.rabbitmq.host = 67.225.121.19
    messageproxy.rabbitmq.port = 5672
    messageproxy.rabbitmq.username = guest
    messageproxy.rabbitmq.password = guest
    messageproxy.listen.queue.name = sn.soa.queue
    

    相关文章

      网友评论

          本文标题:SpringBoot下RabbitMQ多数据源配置

          本文链接:https://www.haomeiwen.com/subject/efhvxktx.html