0

Azure CosmosDB - 我想要获取的“Mongo 查询请求率”指标。当我使用邮递员时,Microsoft 提供的 URL 可以正常工作。但是如果我使用 WebClient 或 RestTemplate,它会抛出异常:

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)

这是我的代码:

public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {

        String [] dateTimes = getDateTimes() ;

        String url=  "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
                "/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
                "/metrics?api-version=2015-04-08&$filter=%28name.value%20eq%20%27Mongo Query Request Rate%27%29%20and" +
                "%20timeGrain%20eq%20duration%27PT5M%27%20and%20startTime%20eq%20"+dateTimes[0] +
                "Z%20and%20endTime%20eq%20"+dateTimes[1]+"Z";


        webClient = WebClient.builder().baseUrl("https://management.azure.com")             
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

        return webClient.get().uri(url)         
                .header("Authorization", "Bearer "+token)
                .exchange()
                .flatMap(resp -> resp.bodyToMono(DataVO.class));                   
    }




public String[] getDateTimes() {        

        LocalDateTime ldtNow = LocalDateTime.now();
        LocalDateTime ldtBefore = LocalDateTime.now().minusMinutes(5);

        String[] currentDateTime = new String[2];

        currentDateTime[0]=String.valueOf(ldtBefore);
        currentDateTime[1]=String.valueOf(ldtNow);

        return currentDateTime;
    }

看起来像是形成请求的问题。这就是它失败的原因。

有什么帮助吗?

4

1 回答 1

0

形成网址时出现错误,我现在已更正。

public Mono<DataVO> getQueryRequestRate(String subscriptionId, String resourceGroup, String cosmosDbName) {

        String [] dateTimes = getDateTimes() ;

        String url=  "/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroup +
                "/providers/Microsoft.DocumentDb/databaseAccounts/"+cosmosDbName +
                "/metrics?api-version=2015-04-08" +
                "&$filter=(name.value eq 'Mongo Query Request Rate' and "+
                "startTime eq "+dateTimes[0]+"Z and endTime eq "+dateTimes[1]+"Z  and timeGrain eq duration'PT5M')";

        //System.out.println(url);

        webClient = WebClient.builder().baseUrl("https://management.azure.com")             
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

        return webClient.get().uri(url)         
                .header("Authorization", "Bearer "+token)
                .exchange()
                .flatMap(resp -> resp.bodyToMono(DataVO.class));                   
    }
于 2019-06-17T15:23:42.200 回答