我的带有 Java 和 Spring Boot 连接的 Cosmos DB 模拟器不断超时

问题描述 投票:0回答:1

我按照 Microsoft 的本指南在 Windows 11 计算机上使用 NoSQL api 和 docker 设置 Cosmos DB 模拟器。

https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-develop-emulator?tabs=docker-linux%2Ccsharp&pivots=api-nosql

我已经下载了所有必需的软件,并且我已经使用 docker 成功设置了 Cosmos-DB 模拟器的运行映像。

enter image description here

现在,我在 Intellij 上,并且能够成功启动我的 Java 和 Spring Boot 项目以连接本地 Azure Cosmos DB 模拟器。我用来连接模拟器的库如下

implementation 'com.azure:azure-spring-data-cosmos:5.17.1'

我正在使用此库,因为我将 Java 与 Spring Boot 一起使用,并且我能够连接并针对我的 Azure 门户上的实际 Azure Cosmos DB 帐户运行。

但在本地,它抱怨套接字超时并显示以下错误消息:

2024-10-11T16:30:32.352-04:00  WARN 23028 --- [tor-http-nio-17] c.a.c.implementation.ClientRetryPolicy   : marking the endpoint https://172.17.0.2:8081/ as unavailable for read
2024-10-11T16:30:32.352-04:00  INFO 23028 --- [tor-http-nio-17] c.a.c.i.RxDocumentClientImpl             : Getting database account endpoint from https://localhost:8081
2024-10-11T16:30:54.432-04:00  WARN 23028 --- [tor-http-nio-18] c.a.c.i.d.GatewayAddressCache            : Network failure

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: getsockopt: /172.17.0.2:8081
Caused by: java.net.ConnectException: Connection timed out: getsockopt
    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) ~[na:na]
    at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:973) ~[na:na]
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:336) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:339) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:994) ~[netty-common-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.110.Final.jar:4.1.110.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.110.Final.jar:4.1.110.Final]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

这就是我设置扩展 AbstractCosmosConfiguration 类的方式

@Bean
    public CosmosClientBuilder getCosmosClientBuilder() {
        final DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
        final GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
        return new CosmosClientBuilder()
                .endpoint("https://localhost:8081")
                .credential(new ManagedIdentityCredentialBuilder()
                        .build())
                .key("my primary key")
                .directMode(directConnectionConfig, gatewayConnectionConfig)
                .clientTelemetryConfig(
                        new CosmosClientTelemetryConfig()
                                .diagnosticsThresholds(
                                        new CosmosDiagnosticsThresholds()
                                )
                                .diagnosticsHandler(CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER));
    }

    @Override
    public CosmosConfig cosmosConfig() {
        return CosmosConfig.builder()
                .enableQueryMetrics(cosmosProperties.isQueryMetricsEnabled())
                .enableIndexMetrics(cosmosProperties.isIndexMetricsEnabled())
                .responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
                .build();
    }

    @Override
    protected String getDatabaseName() {
        return "Database Name";
    }

    private static class ResponseDiagnosticsProcessorImplementation implements ResponseDiagnosticsProcessor {

        @Override
        public void processResponseDiagnostics(@Nullable final ResponseDiagnostics responseDiagnostics) {
            log.info("Response Diagnostics {}", responseDiagnostics);
        }
    }

我缺少什么来解决这个问题?

java spring-boot azure-cosmosdb azure-cosmosdb-sqlapi azure-cosmosdb-emulator
1个回答
0
投票

对于其他也遇到套接字超时的人,我相信这是因为 @KeithJackson 在他的帖子中提到的:

调用 CosmosClient 方法时,Cosmos DB Docker 容器会挂起

我删除了从 Microsoft 示例中获得的旧 Docker 映像,并使用了他编写的 docker 命令,到目前为止,我不再看到套接字超时问题。

© www.soinside.com 2019 - 2024. All rights reserved.