代码之家  ›  专栏  ›  技术社区  ›  Arjun

不能使用Azure管理库来订阅Azure AppService(Spring Buffing应用程序)

  •  2
  • Arjun  · 技术社区  · 6 年前

    我试图在AppServices中列出我部署的Spring启动应用程序,这是我在Azure订阅中使用的Azure管理库,但不能这样做。

    从Azure CLI来看,一切正常。

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

    平地

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

    我是否遗漏了什么,或者如果有一些先决条件,请告诉我。

    非常感谢。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Arjun    6 年前

    以某种方式 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"));
                });
    }
    
        2
  •  0
  •   George Chen    6 年前

    我测试代码,但不是Gradle,我的是Maven。我用过 az ad sp create-for-rbac --sdk-auth > my.azureauth 得到 my.azureauth 文件,然后获取 clientID , clientScrect , SunscribtionID tenantID . 这是我的结果,效果很好。

    enter image description here

    这是我的密码。

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

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