连接重置端口5671与RabbitMQ的安全连接,使用@Bean和file.properties

时间:2020-03-26 17:24:40

标签: java spring-boot ssl rabbitmq

我只是想创建一个与Rabbit的连接,该端口在5671端口(5672:默认)上工作。

我的连接是使用包含以下内容的.properties文件创建的:

##RabbitMQ
spring.rabbitmq.host=url
spring.rabbitmq.port=5671
spring.rabbitmq.username=user
spring.rabbitmq.password=pass
spring.rabbitmq.ssl.enabled=false (i've changed the value = true)

spring.rabbitmq.virtualHost=virtualHost
ike.rabbitmq.exchange=exchange
ike.rabbitmq.routingkey=routingKey
ike.rabbitmq.queue=queue

#RabbitMQ Consumer
ike.rabbitmq.queue.c=consumerQueue

还有一个带有@Bean批注的类:

@Component
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String user;

    @Value("${spring.rabbitmq.password}")
    private String pass;

    @Value("${spring.rabbitmq.virtualHost}")
    private String virtualHost;

    @Value("${ike.rabbitmq.exchange}")
    private String exchange;

    @Value("${ike.rabbitmq.routingkey}")
    private String routingKey;

    @Value("${ike.rabbitmq.queue}")
    private String queue;

    @Value("${rabbitmq.use-ssl:false}")
    boolean useSSL;

    public void sendMsg(Object msg) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }

    @Bean
    Queue queue() {return new Queue(queue, true);}

    @Bean
    TopicExchange exchange() {return new TopicExchange(exchange);}

    @Bean
    Binding binding (Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingKey);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
        return rabbitTemplate;
    }

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

问题是:每次我运行项目时,都会重置连接。 有人告诉我,我必须创建类似的内容:

ConnectionFactory connection = new ConnectionFactory();
connection.useSslProtocol();

但是如果我没记错的话,它将在ConnectionFactory实例中

2020-03-26 11:11:28.626  INFO 62454 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [domain.com:5671]
2020-03-26 11:11:28.702  WARN 62454 --- [116.44.217:5671] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2020-03-26 11:11:28.705  INFO 62454 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.io.IOException
2020-03-26 11:11:33.810  INFO 62454 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@77b17867: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0
2020-03-26 11:11:33.812  INFO 62454 --- [ntContainer#0-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [domain.com:5671]
2020-03-26 11:11:33.852  WARN 62454 --- [116.44.217:5671] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)
2020-03-26 11:11:38.891  INFO 62454 --- [ntContainer#0-2] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@3e59186: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0
2020-03-26 11:11:38.892  INFO 62454 --- [ntContainer#0-3] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [domain.com:5671]
2020-03-26 11:11:38.946  WARN 62454 --- [116.44.217:5671] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured (Exception message: Connection reset)

非常感谢您!

0 个答案:

没有答案