无法在使用 Java 的 Azure 管理库的订阅中列出 azure appservices(spring 启动应用程序)

Can't list azure appservices (spring boot apps) in a subscription using Azure Management Libraries for Java

我正在尝试使用 java 的 azure 管理库在我的 azure 订阅之一的 appservices 中列出我已部署的 spring 引导应用程序,但无法这样做。

在 azure cli 中一切正常。

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

build.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()

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

非常感谢。

我测试了代码,但没有 gradle,我的是 maven。我使用 az ad sp create-for-rbac --sdk-auth > my.azureauth 获取 my.azureauth 文件,然后获取 clientIDclientScrectSunscribtionIDtenantID。这是我的结果,效果很好。

这是我的代码。

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.appservice.WebApp;


public class App 
{
public static void main( String[] args )
{

    String client="*******";
    String tenant="*******";
    String key="********";
    String subscriptionId="********";
    ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
            client, tenant, key, AzureEnvironment.AZURE);
    Azure azure = Azure.authenticate(credentials).withSubscription(subscriptionId);

    System.out.println("asd");
    listWebApps(azure);
    System.out.println("asd");
    listWebAppsUsingAppServicePlan(azure);
}



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

}

注意:身份验证和运行获取网络列表的方法会花费大量时间。 如果您还有其他问题,请告诉我。

不知何故 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"));
            });
}