Spring Boot 无法使用Sping Boot 2和Stomp连接到Amazon MQ

vfh0ocws  于 2023-10-16  发布在  Spring
关注(0)|答案(2)|浏览(202)

我正在使用Sping Boot 2、Stomp和Amazon MQ构建WebSocket应用程序。
但是我无法连接到Amazon MQ Broker。Failed to connect: connection timed out错误
我的WebSocket配置:

@Configuration
@RequiredArgsConstructor
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    private final ActiveMQProperties properties;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/")
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/topic")
                .setAutoStartup(true)
                .setRelayPort(61614)
                .setRelayHost(properties.getBrokerUrl())
                .setClientLogin(properties.getUser())
                .setClientPasscode(properties.getPassword())
                .setSystemLogin(properties.getUser())
                .setSystemPasscode(properties.getPassword())
               .setTcpClient(createClient());
    }

    private TcpOperations<byte[]> createClient(){
        return new ReactorNettyTcpClient<>((client) -> client
//                .host(properties.getBrokerUrl())
//                .port(61614)
                .addressSupplier(this::getAddress)
                .secure(), new StompReactorNettyCodec());

    }

    private SocketAddress getAddress() {
        try {
            InetAddress address = InetAddress.getByName(properties.getBrokerUrl());
            SocketAddress socketAddress = new InetSocketAddress(address, 61614);
            return socketAddress;
        } catch (UnknownHostException e) {
            log.error(e);
        }
        return null;
    }

日志

2020-03-27 16:21:19.419  WARN 49890 --- [ealth-indicator] o.s.boot.actuate.jms.JmsHealthIndicator  : Connection failed to start within 5 seconds and will be closed.
2020-03-27 16:21:39.156  INFO 49890 --- [ient-loop-nio-1] o.s.m.s.s.StompBrokerRelayMessageHandler : TCP connection failure in session _system_: Failed to connect: connection timed out: {my-aws-url}:61614
io.netty.channel.ConnectTimeoutException: connection timed out: {my-aws-url}:61614
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:261) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_241]
2020-03-27 16:21:39.156 DEBUG 49890 --- [ient-loop-nio-1] org.springframework.web.SimpLogging      : Cleaning up connection state for session _system_
u3r8eeie

u3r8eeie1#

我认为您在安全组/NACL中缺少一个条目,以允许端口61614上的流量。我还将使用traceroute检查客户端主机是否可以访问代理主机。解决这两个问题中的任何一个都应该解决这个问题。

qxsslcnc

qxsslcnc2#

需要为AWS MQ Broker配置分配安全组。当您没有选择任何安全组时,它将使用默认安全组。因此,您的IP必须添加到使用的安全组中。然后您可以访问MQ管理UI或队列。

相关问题