Springboot应用程序未连接到rabbitmq

时间:2019-07-25 04:49:48

标签: spring-boot rabbitmq spring-rabbitmq spring-rabbit

我正在使用springboot 2.0.5.RELEASE,集成了具有spring-boot-starter-amqp依赖项的Rabbit连接。

兔子配置:

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitConfig {

    @Bean
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        com.rabbitmq.client.ConnectionFactory connectionFactory = new com.rabbitmq.client.ConnectionFactory();
        connectionFactory.setHost(rabbitmqHost);
        connectionFactory.setPort(rabbitmqPort);
        connectionFactory.setUsername(rabbitmqUsername);
        connectionFactory.setPassword(rabbitmqPassword);
        connectionFactory.setVirtualHost(rabbitmqVirtualHost);
        connectionFactory.setAutomaticRecoveryEnabled(false);
        CachingConnectionFactory cacheConnectionFactory = new CachingConnectionFactory(connectionFactory);
        cacheConnectionFactory.setRequestedHeartBeat(rabbitmqRequestedHeartBeat);
        cacheConnectionFactory.setChannelCacheSize(channelCacheSize);
        return new RabbitTemplate(connectionFactory);
    }
}

启动应用程序时,没有看到任何尝试连接到Rabbit服务器的尝试。

日志中与兔子有关的唯一片段是:

{Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$3b504de] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)","logger_name":"org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker"}
{Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$e1e10baf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)","logger_name":"org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker"}


{SECURITY ALERT: this trust manager trusts every certificate, effectively disabling peer verification. This is convenient for local development but offers no protection against man-in-the-middle attacks. Please see https://www.rabbitmq.com/ssl.html to learn more about peer certificate verification.","logger_name":"com.rabbitmq.client.TrustEverythingTrustManager"}
{SECURITY ALERT: this trust manager trusts every certificate, effectively disabling peer verification. This is convenient for local development but offers no protection against man-in-the-middle attacks. Please see https://www.rabbitmq.com/ssl.html to learn more about peer certificate verification.","logger_name":"com.rabbitmq.client.TrustEverythingTrustManager"}

应用未尝试连接到Rabbit的原因是什么。我还有另一个兔子服务器配置相同的应用程序,它确实尝试连接到兔子。

2 个答案:

答案 0 :(得分:0)

  

CachingConnectionFactory cacheConnectionFactory = new CachingConnectionFactory(connectionFactory);

您不应在模板bean定义内创建新的连接工厂。

如果要自定义连接工厂,则它必须是bean本身。

直到您尝试与代理进行交互(通过发送或尝试接收消息),才会建立连接。

答案 1 :(得分:0)

这就是我需要与.text-field { -fx-opacity: 1.0; -fx-font-family: Consolas; -fx-font-size: 200%; } .button { -fx-font-family: Consolas; -fx-font-size: 150%; -fx-background-radius: 25px; -fx-border-radius: 25px; /* Remove focus highlighting */ -fx-focus-traversable: false; } .button:pressed { -fx-background-color: lightgreen; -fx-border-color: green; } 连接的条件。实际上,这将创建Nonetorch.onnx.export(如果它们不可用)并按照我的配置进行绑定。

RabbitMq

在客户端类中,您仅需要exchangequeue类即可与代理进行通信。

import org.springframework.amqp.core.*;
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.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

@RefreshScope // If uses externalized configurations.
@PropertySource("classpath:config.properties") // Load config from properties.
@Configuration
public class RabbitMqConfig {

    /**
     * RabbitMQ broker host name.
     */
    @Value("${me.rabbitmq.host}")
    private String host;
    /**
     * RabbitMQ broker transport port.
     */
    @Value("${me.rabbitmq.port}")
    private int port;
    /**
     * RabbitMQ username.
     */
    @Value("${me.rabbitmq.auth.username}")
    private String userName;
    /**
     * RabbitMQ password.
     */
    @Value("${me.rabbitmq.auth.password}")
    private String password;
    /**
     * Default messaging queue.
     */
    @Value("${me.rabbitmq.default.queue.name}")
    private String queueName;
    /**
     * Whether queue is durable/persistence.
     */
    @Value("${me.rabbitmq.default.queue.durable}")
    private boolean isDurable;
    /**
     * Default topic name.
     */
    @Value("${me.rabbitmq.default.topic.name}")
    private String topicName;
    /**
     * Default routing key.
     */
    @Value("${me.rabbitmq.default.topic.key}")
    private String routingKey;

    /**
     * Make this primary connection factory hence one of the AmqpAdmin sub class
     * implementation has a default ConnectionFactory for its use and both conflicts
     * unless otherwise.
     */
    @Primary
    @Bean
    public ConnectionFactory getConnectionFactory() {
        CachingConnectionFactory cf = new CachingConnectionFactory(host, port);
        cf.setUsername(userName);
        cf.setPassword(password);
        return cf;
    }

    /**
     * Create new AmqpAdmin instance to enforce portable administrative capabilities. 
     */
    @Bean
    public AmqpAdmin getAmqpAdmin(ConnectionFactory cf) {
        return new RabbitAdmin(cf);
    }

    /**
     * Create a new queue with given name and attributes if no exists.
     */
    @Bean
    public Queue getQueue() {
        return new Queue(queueName, isDurable);
    }

    /**
     * Create a new topic exchange in the application as well as the broker if not exists.
     */
    @Bean
    public TopicExchange getTopicExchange() {
        return new TopicExchange(topicName);
    }

    /**
     * This only needs if you use JSON messages.
     */
    @Primary
    @Bean
    public RabbitTemplate getRabbitTemplate(ConnectionFactory cf) {
        RabbitTemplate rt = new RabbitTemplate(cf);
        rt.setMessageConverter(new Jackson2JsonMessageConverter());

        return rt;
    }

    /**
     * Bind the exchange and the queue with routingKey (No need if pre-configured). 
     */
    @Bean
    public Binding setDefaultBinding(TopicExchange exchange, Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with(routingKey);
    }
}