開始使用 Azure 上的 Java 進行雲端開發Get started with cloud development using Java on Azure
11/20/2020
在此文章
本文會逐步引導您設定開發環境,以便在 Java 中進行 Azure 開發。This article walks you through setting up a development environment for Azure development in Java. 然後,您將建立一些 Azure 資源並連接到這些資源,以進行一些基本工作,例如上傳檔案或部署 web 應用程式。You'll then create some Azure resources and connect to them to do some basic tasks, like uploading a file or deploying a web application. 當您完成時,您就已做好準備可以開始在自有的 Java 應用程式中使用 Azure 服務。When you're finished, you'll be ready to start using Azure services in your own Java applications.
PrerequisitesPrerequisites
一個 Azure 帳戶。An Azure account. 如果您沒有帳戶,請取得免費試用帳戶。If you don't have one, get a free trial.
Java 8 (包含在 Azure Cloud Shell 中)。Java 8, which is included in Azure Cloud Shell.
Maven 3 (包含在 Azure Cloud Shell 內)。Maven 3, which is included in Azure Cloud Shell.
設定驗證Set up authentication
Java 應用程式必須有 Azure 訂用帳戶的讀取和建立權限,才能在此教學課程中執行程式碼範例。Your Java application needs read and create permissions in your Azure subscription to run the sample code in this tutorial. 請建立服務主體,並將應用程式設定為使用其認證來執行。Create a service principal, and configure your application to run with its credentials. 服務主體可讓您建立與身分識別相關聯的非互動式帳戶,而且對於此身分識別,您只賦予它應用程式執行時所需的權限。Service principals provide a way to create a noninteractive account associated with your identity to which you grant only the privileges your app needs to run.
az ad sp create-for-rbac --name AzureJavaTest
這會提供您下列格式的回覆:Which gives you a reply in the following format:
{
"appId": "a487e0c1-82af-47d9-9a0b-af184eb87646d",
"displayName": "AzureJavaTest",
"name": "http://AzureJavaTest",
"password": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
接下來,設定環境變數:Next, configure the environment variables:
AZURE_SUBSCRIPTION_ID:使用在 Azure CLI 2.0 中透過 az account show 所得到的 id 值。AZURE_SUBSCRIPTION_ID: Use the id value from az account show in the Azure CLI 2.0.
AZURE_CLIENT_ID:使用從服務主體輸出所擷取的 appId 值。AZURE_CLIENT_ID: Use the appId value from the output taken from a service principal output.
AZURE_CLIENT_SECRET:使用從服務主體輸出所得到的 password 值。AZURE_CLIENT_SECRET: Use the password value from the service principal output.
AZURE_TENANT_ID:使用從服務主體輸出所得到的 tenant 值。AZURE_TENANT_ID: Use the tenant value from the service principal output.
ToolingTooling
建立新的 Maven 專案Create a new Maven project
注意
本文使用 Maven 建置工具來建置和執行範例程式碼。This article uses the Maven build tool to build and run the sample code. 其他 build tools (例如 Gradle)也可以使用 Azure SDK for JAVA。Other build tools, such as Gradle, also work with the Azure SDK for Java.
透過命令列在系統上的新目錄中建立 Maven 專案。Create a Maven project from the command line in a new directory on your system.
mkdir java-azure-test
cd java-azure-test
mvn archetype:generate -DgroupId=com.fabrikam -DartifactId=AzureApp \
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
此步驟會在 testAzureApp 目錄下建立基本的 Maven 專案。This step creates a basic Maven project under the testAzureApp directory. 在專案的 pom.xml 檔案中新增下列專案,以匯入本教學課程的範例程式碼中所使用的程式庫。Add the following entries into the project's pom.xml file to import the libraries used in the sample code in this tutorial.
com.azure
azure-identity
1.2.0
com.azure.resourcemanager
azure-resourcemanager
2.1.0
com.azure
azure-storage-blob
12.8.0
com.microsoft.sqlserver
mssql-jdbc
6.2.1.jre8
在最上層 project 元素底下新增 build