我正在尝试使用与 Azure Cosmos DB for NoSQL 的 Java 无密码连接构建 Azure 客户端。我已按照这篇 Microsoft Learn 文章 中提供的说明进行操作,并按照建议创建了自定义角色。但是,我收到以下错误:
Exception in thread "main" java.lang.RuntimeException: Client initialization failed. Check if the endpoint is reachable and if your auth token is valid. More info: https://aka.ms/cosmosdb-tsg-service-unavailable-java
at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:512)
at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:547)
at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:295)
at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:171)
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:1052)
at com.azure.cosmos.CosmosClient.<init>(CosmosClient.java:38)
at com.azure.cosmos.CosmosClientBuilder.buildClient(CosmosClientBuilder.java:1086)
at com.azure.cosmos.sample.sync.CosmoDBClientPasswordless.main(CosmoDBClientPasswordless.java:25)
这是我正在使用的代码:
public class CosmoDBClientPasswordless {
public static void main(String[] args) {
// Set up your Cosmos DB client
String endpoint = "<endpoint>";
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
.build();
CosmosClient client = new CosmosClientBuilder().endpoint(endpoint)
.credential(credential)
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.buildClient();
}
}
我已经设置了我的环境变量 AZURE_SUBSCRIPTION_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_TENANT_ID
我已验证这些密钥,并且使用相同的密钥我可以访问 Azure Blob 存储。
按照文档中的建议创建了自定义角色,它曾经有效,但现在不起作用。
如果我使用宇宙密钥和相同的端点,那么我的代码就可以工作。
String endpoint = "<endpoint>";
String key = "<key>";
CosmosClientBuilder clientBuilder = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key);
CosmosClient client = clientBuilder.buildClient();