首页 > 解决方案 > 无法使用 Azure Management Libraries for Java 在订阅中列出 azure appservices(春季启动应用程序)

问题描述

我正在尝试使用适用于 java 的 azure 管理库在我的一个 azure 订阅中的 appservices 中列出我部署的 Spring Boot 应用程序,但无法这样做。

从 azure cli 一切正常。

azure java sdk version 1.18.0 (latest)
jdk version 1.8.0_172

构建.gradle

dependencies {
    compile group: 'com.microsoft.azure', name: 'azure', version: '1.18.0'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

片段

try {
    Azure azure = Azure
                   .configure()
                   .authenticate(applicationTokenCredentials)
                   .withDefaultSubscription();

            listWebApps(azure);
            listWebAppsUsingAppServicePlan(azure);

        } catch (IOException e) {
            e.printStackTrace();
        }

private static void listWebAppsUsingAppServicePlan(Azure azure){
    PagedList<WebApp> webAppPagedList = azure.appServices().webApps().list();
    System.out.printf("There are %d web apps when searched via azure.appServices().webApps()\n", webAppPagedList.size());
    for (WebApp app : webAppPagedList) {
        System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
    }

}

private static void listWebApps(Azure azure){
    PagedList<WebApp> webAppPagedList = azure.webApps().list();
    System.out.printf("There are %d web apps when searched via azure.webApps()\n", webAppPagedList.size());
    for (WebApp app : webAppPagedList) {
        System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
    }
}

输出

There are 0 web apps when searched via azure.webApps() There are 0 web apps when searched via azure.appServices().webApps()

我是否遗漏了什么,或者是否有一些先决条件让我知道。

非常感谢。

标签: javaazurespring-bootazure-web-app-serviceazure-management-api

解决方案


不知何故azure.webApps().list(),方法是返回一个空列表,但切换到 azure.webapps.listAsync()解决了我的问题。

新的片段

    azure.webApps().listAsync()
            .subscribe(webApp -> {
                int capacity = webApp.manager().appServicePlans().getById(webApp.appServicePlanId()).capacity();
                System.out.println(webApp.name() + ": " + capacity + (capacity == 1 ? " instance" : " instances"));
            });
}

推荐阅读