我正在尝试使用Java的azure管理库在我的azure订阅中的appservices中列出已部署的spring boot应用程序,但无法这样做。
从天青的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()
我错过了什么吗?或者是否有一些先决条件让我知道。
谢谢你一遍。
答案 0 :(得分:1)
某种程度上,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"));
});
}
答案 1 :(得分:0)
我测试了代码,但没有成功,我是行家。然后我使用az ad sp create-for-rbac --sdk-auth > my.azureauth
获取了my.azureauth
文件,然后获取了clientID
,clientScrect
,SunscribtionID
和tenantID
。这是我的结果,效果很好。
这是我的代码。
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());
}
}
}
注意:在进行身份验证和运行方法以获取网络列表时,将花费大量时间。 如果您还有其他问题,请告诉我。