Brak połączenia z RabbitMq

0

Cześć

Próbuję w swoim kodzie skonfigrować połączenie z RabbitMq, wysłać prostą wiadomość i sprawdzić czy poprawnie wszystko działa.

Aplikacja wywala się i dostaję informację Connection refused: connect. Nie mam doświadczenia z Rabbitem. Próbowałem różnych połączeń ale bez skutku
Poniżej podsyłam kod, proszę rzucić okiem. Może gdzieś w konfiguracji jest błąd.

@RequiredArgsConstructor
@Slf4j
@Profile(value = {"dev"})
@Service
@ConditionalOnProperty(
        value = "spring.rabbitmq.retail.enabled",
        havingValue = "true")
public class Config {

    static final String topicExchangeName = "amp.topic";
    static final String queueName = "PDC";

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

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

    @Bean
    public RabbitTemplate rabbitTemplate(Jackson2JsonMessageConverter jackson2JsonMessageConverter, ConnectionFactory connectionFactory)

        var rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
}

    @Bean
    ConnectionFactory connectionFactory() throws Exception {
        RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
        factory.setHost("10.151.101.193");
        factory.setPort(4443);
        factory.setVirtualHost("ic.out");
        factory.setUsername("retail");
        factory.setPassword("edd");
        factory.setUseSSL(false);
        factory.afterPropertiesSet();
        return new CachingConnectionFactory(Objects.requireNonNull(factory.getObject()));
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter(objectMapper());
    }

    public ObjectMapper objectMapper() {
        var objectMapper = new ObjectMapper();
        return  objectMapper;
    }
}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.web.servlet.function.RouterFunctions.route;
import static org.springframework.web.servlet.function.ServerResponse.ok;

@Configuration
public class Controller {

    @Bean
    public RouterFunction<ServerResponse> productListing(Producer producer) {
        return route().GET("/send", req -> {
            producer.sendMessage();
            return ok().build();
        }).build();
    }
}


import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.util.logging.Logger;

@Component
public class Producer {
    Logger log = LoggerFactory.getLogger(Producer.class);

    private final RabbitTemplate rabbitTemplate;

    public Producer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage() {
        TestClass testClass = TestClass.builder()
                .data("**81*")
                .build();
        log.info("Sending message to queue: {}", testClass);
        rabbitTemplate.convertAndSend(testClass);
    }
}


@Data
@AllArgsConstructor
@Builder(tuBoilder = true)
public class TestClass implements Serrializable {
    @JsonProperty("data")
    String data;
}


@SpringBootApplication
public class RabbitmqApplication {


    public RabbitmqApplication(Producer producer) {
        this.producer = producer;
    }


    public static void main(String[] args) {
        SpringApplication.run(RabbitmqApplication.class, args);
    }

    private final  Producer producer;

    @EventListener(ApplicationReadyEvent.class)public void doSomethingAfterStartup() {
        producer.sendMessage();
        System.out.println("Hello world");
    }

}

2

A ten króliczek działa na tym samym PC ? Na innym? Jesteś pewny, że działa? Itp itd ?

2

@KrisPaton:

Are You absolutely sure ... że to pora na spirnga i taką dawkę adnotacji?
Tych małpek jest więcej, niż dolarów w PHP

Nie wyobrażam sobie zapoznawania z technologią w ten sposób. Weź jakiś command line itd ...

1

W pierwszej kolejności, sprawdź czy działa serwer RabbitMQ, do które chcesz się podpiąć.

Zwykły telnet/nmap/nc

        factory.setHost("10.151.101.193");
        factory.setPort(4443);

1 użytkowników online, w tym zalogowanych: 0, gości: 1