与Azure Cosmos DB的Java无密码连接的构建问题

uwopmtnx  于 2023-10-22  发布在  Java
关注(0)|答案(1)|浏览(140)

我正在尝试使用Java无密码连接构建Azure客户端到Azure Cosmos DB for NoSQL。我已经按照Microsoft Learn article中提供的说明,并按照建议创建了一个自定义角色。但是,我收到以下错误:

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();  
       
}

}
我已经设置了我的env变量AZURE_SUBSCRITION_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_TENANT_ID
我已经验证了这些密钥,并且使用相同的密钥,我可以访问Azure Blob存储。
创建自定义的作用,建议在文件中,它的工作一次,但现在它不工作。
我的代码虽然如果我使用cosmos密钥和相同的端点,就像下面的工作一样。

String endpoint = "<endpoint>";
        String key = "<key>";
        CosmosClientBuilder clientBuilder = new CosmosClientBuilder()
                .endpoint(endpoint)
                .key(key);

        CosmosClient client = clientBuilder.buildClient();
w1jd8yoj

w1jd8yoj1#

您可以使用启用的托管身份连接到cosmos DB,而无需任何密码或连接字符串。详情请参阅:https://learn.microsoft.com/en-us/azure/cosmos-db/managed-identity-based-authentication

相关问题