销售电话: 1-800-867-1380

Creating a Hello World Application for Azure in Eclipse

更新时间: 2015年3月

The following steps show you how to create and deploy a basic JSP application to Azure using the Azure Toolkit for Eclipse. A JSP example is shown for simplicity, but highly similar steps would be appropriate for a Java servlet, as far as Azure deployment is concerned.

The application will look similar to the following:

部署的 Hello World

  1. First, we’ll start off with creating a Java project. Start Eclipse. Within Eclipse, at the menu click File, click New, and then click Dynamic Web Project. (If you don’t see Dynamic Web Project listed as an available project after clicking File, New, then do the following: click File, click New, click Project…, expand Web, click Dynamic Web Project, and click Next.) For purposes of this tutorial, name the project MyHelloWorld. (Ensure you use this name, subsequent steps in this tutorial expect your WAR file to be named MyHelloWorld). Your screen will appear similar to the following:

    创建 Web 项目

    Click Finish.

  2. Within Eclipse’s Project Explorer view, expand MyHelloWorld. Right-click WebContent, click New, and then click JSP File.

  3. In the New JSP File dialog, name the file index.jsp. Keep the parent folder as MyHelloWorld/WebContent, as shown in the following:

    创建 JSP 文件

    Click Next.

  4. In the Select JSP Template dialog, for purposes of this tutorial select New JSP File (html) and click Finish.

  5. When the index.jsp file opens in Eclipse, add in text to display Hello World! within the existing <body> element. Your updated <body> content should appear as the following:

    <body>
      <b><% out.println("Hello World!"); %></b>
    </body>
    

    Save index.jsp.

As soon as you have a Java web application ready to test, you can use the following shortcut to try it out directly on the Azure cloud.

  1. In Eclipse’s Project Explorer, click MyHelloWorld.

  2. In the Eclipse toolbar, click the Publish to Azure Cloud button, 发布到 Azure 云.

  3. If you are publishing this application to Azure for the first time and you have not created an Azure deployment project for this application before, an Azure deployment project be created for you automatically. You should see the following prompt, which also lists the JDK package and application server that will be automatically deployed to run your application.

    This shortcut approach enables a quick and easy way to test your application in Azure without having to configure a specific server or JDK that is different from the defaults. If you are satisfied with the defaults, you can click OK to continue with the following steps.

    However, if you want to change the JDK or application server to use for your application, you can do that later by editing the Azure deployment project that was automatically created for you, or you can click Cancel now and read the About Azure deployment projects section of this tutorial.

  4. In the Publish to Azure dialog:

    1. If there are no subscriptions to select in the Subscription list yet, follow these steps to import your subscription information:

      1. Click Import from PUBLISH-SETTINGS file.

      2. In the Import Subscription Information dialog, click Download PUBLISH-SETTINGS File. If you are not yet logged into your Azure account, you will be prompted to log in. Then you’ll be prompted to save an Azure publish settings file. Save it to your local machine.

      3. Still in the Import Subscription Information dialog, click the Browse button, select the publish settings file that you saved locally in the previous step, and then click Open. Your screen should look similar to the following.

        导入发布设置
      4. Click OK.

    2. For Subscription, select the subscription that you want use for your deployment.

    3. For Storage account, select the storage account that you want to use, or click New to create a new storage account.

    4. For Service name, select the cloud service that you want to use, or click New to create a new cloud service.

    5. For Target OS, select the version of the operating system that you want to use for your deployment.

    6. For Target environment, for purposes of this tutorial, select Staging. (When you’re ready to deploy to your production site, you’ll change this to Production.)

    7. [Optional] Ensure that Overwrite previous deployment is checked if you want your new deployment to automatically overwrite the previous deployment. When you enable this option, you will not experience "409 conflict" issues when publishing to the same location.

      Note that the Publish to Azure dialog contains a section for Remote Access. By default, Remote Access is not enabled and we will not enable it for this example. To enable Remote Access, you would enter a user name and password to use when remotely logging in. For more information about Remote Access, see Enabling Remote Access for Azure Deployments in Eclipse.

      Your Publish to Azure dialog will appear similar to the following:

      “发布 Azure 项目”对话框
  5. Click Publish to publish to the Staging environment.

    When prompted to perform a full build, click Yes. This may take several minutes for the first build.

    An Azure Activity Log will display in your Eclipse tabbed views section.

    Azure 活动日志

    You can use this log, as well as the Console view, to see the progress of your deployment. An alternative is to log in to the Azure Management Portal, http://manage.windowsazure.com, and use the Cloud Services section to monitor the status.

  6. When your deployment is successfully deployed, the Azure Activity Log will show a status of Published. Click Published, as shown in the following image, and your browser will open an instance of your deployment.

    Azure 活动日志 - 已发布

Because this was a deployment to a staging environment, the DNS name will be of the form http://<guid>.cloudapp.net, and the URL will contain the DNS name plus a suffix for your application. For example, http://72d5eb5875234b7ca8c7f74c80a2a1f1.cloudapp.net/MyHelloWorld. (The MyHelloWorld portion is case-sensitive.) You can also see the DNS name if you click the deployment name in the Azure Platform Management Portal (within the Cloud Services portion of the management portal).

Although this walk-through was for a deployment to the staging environment, a deployment to production follows the same steps, except within the Publish to Azure dialog, select Production instead of Staging for the Target environment. A deployment to production results in a URL based on the DNS name of your choice, instead of a GUID as used for staging.

Warning警告
At this point you have deployed your Azure application to the cloud. However, before proceeding, realize that a deployed application, even if it is not running, will continue to accrue billable time for your subscription. Therefore, it is extremely important that you delete unwanted deployments from your Azure subscription.

In order to deploy one or more Java applications to Azure, an Azure Deployment Project is needed. It plays the role of the “package” that your applications need to be wrapped into in order to be published on Azure.

Besides the information about your applications, an Azure deployment project also contains information about other key components of your deployment, most importantly: the application server container to run your web app in, and the Java runtime to run it on. Azure supports a large selection of Java runtimes and Java application servers you can choose from.

Although the example used here is greatly simplified for educational purposes, an Azure deployment project can also contain other important configuration information that enables you to create almost arbitrarily complex, scalable, highly available, multi-tier cloud services with your applications. You can enable session affinity (“sticky sessions”), fast caching, remote debugging, SSL offloading, firewall/port routing, remote access, and a number of other powerful capabilities.

If you’ve completed the previous section of this tutorial (“To deploy your application to Azure, the quick and simple way”), you will now see a new Azure deployment project in the Project Explorer generated for you automatically and named “MyHelloWorld_onAzure”.

You could have also started this tutorial by first creating a blank Azure deployment project yourself and then adding your application(s) to it. It is a longer process, but giving you more control over the initial configuration from the beginning.

To create a new Azure deployment project from scratch, click the New Azure Deployment Project button 新建 Azure 部署项目.

Regardless of whether you are working with an already existing Azure deployment project, or creating one from scratch, you are able to change its configuration settings and components, such as the JDK or the application server, equally easily at any time.

To change the JDK, or the application server, or the application list in an existing Azure deployment project:

  1. Expand the project node (e.g. MyHelloWorld_onAzure) in the Project Explorer

  2. Right-click WorkerRole1

  3. Expand the Azure submenu in the context menu

  4. Click Server Configuration

Regardless of whether you started these server configuration steps by editing an existing Azure deployment project as shown above, or creating a new one from scratch, you will see the same type of dialogs allowing you to configure your JDK, server and application components. To learn more how to change the settings in those dialogs, for example to change the JDK, the application server and add or remove applications in a deployment, see the Server configuration properties article.

note备注
The Azure emulator is only available on Windows. Skip this section if you are using an operating system other than Windows.

If you have created a new Azure deployment project following the steps described earlier, i.e. implicitly, by publishing your application to Azure, the JDK and application servers have been configured for the cloud, but not for local emulation. To prepare your project for testing in the local emulator, follow these steps:

  1. In Eclipse’s Project Explorer, click MyHelloWorld_onAzure.

  2. Right-click on WorkerRole1.

  3. Expand the Azure submenu in the context menu.

  4. Click Server Configuration.

  5. On the JDK tab, click the Use the JDK from this file path for testing locally checkbox. If the JDK installation location that you want to use is not selected automatically, click the Browse button and using the browse control, select the directory location of the JDK to use.

  6. Click the Server tab.

  7. Click the Use the server from this file path checkbox and then select the installation directory on your computer of an application server of your choice to use with your deployment, as long as it is the same family and major version number as the server selected for the cloud deployment.

    For example, if the cloud deployment uses Tomcat v8.0.20, then use a version of Tomcat v8 locally. If your server installation directory contains one of the application servers that is listed in the Prerequisites section of this article, the server type will automatically be detected and shown in the dropdown menu. (If the server type is not recognized, do not select a server from the dropdown menu; instead, you should manually enter your server and application deployment logic in that role’s startup.cmd file.)

  8. Click OK.

  9. In the Eclipse toolbar, click the Run in Azure Emulator button, 在 Azure 模拟器中运行. If the Run in Azure Emulator button is not enabled, ensure that MyHelloWorld_onAzure is selected in Eclipse’s Project Explorer, and ensure that Eclipse’s Project Explorer has focus as the current window.

    This will first start a full build of your project and then launch your Java web application in the compute emulator. (Note that depending on your computer’s performance characteristics, the first build may take between a few seconds to a few minutes, but subsequent builds will get faster.) After the first build step has been completed, you will be prompted by Windows User Account Control (UAC) to allow this command to make changes to your computer. Click Yes.

    Important重要提示
    If you do not see the UAC prompt, check the Windows taskbar for the UAC icon and click it first. Sometimes the UAC prompt does not show up as a topmost window, but is visible only as a taskbar icon.

    Examine the output of the compute emulator UI to determine if there are any issues with your project. Depending on the contents of your deployment, it may take a couple minutes for your application to be fully started within the compute emulator.

  10. Start your browser and use the URL http://localhost:8080/MyHelloWorld as the address (the MyHelloWorld portion of the URL is case-sensitive). You should see your MyHelloWorld application (the output of index.jsp), similar to the following image:

    计算模拟器中的 Hello World

When you are ready to stop your application from running in the compute emulator, in the Eclipse toolbar, click the Reset Azure Emulator button, 重置 Azure 模拟器.

To delete your deployment within the Azure Toolkit for Eclipse, ensure that MyHelloWorld_onAzure is selected in Eclipse’s Project Explorer, ensure the Eclipse Project Explorer has the current window focus, and then click the Unpublish button, 取消发布, in the Eclipse toolbar. (You could do the same operation by right-clicking MyHelloWorld_onAzure in Eclipse’s Project Explorer, clicking Azure and then clicking Undeploy from Azure Cloud.) This will display the Unpublish Azure Project dialog.

“取消发布 Azure 项目”对话框

Select the subscription and cloud service that contains your deployment, select the deployment that you want to delete, and then click Unpublish.

(An alternative to using the toolkit to delete the deployment is to use the Cloud Services section of the Azure Management Portal: Navigate to your deployment, select it, and then click the Delete button. This will stop, and then delete, the deployment. If you only want to stop the deployment and not delete it, click the Stop button instead of the Delete button, but as mentioned above, if you do not delete the deployment, billable charges will continue to accrue for your deployment even if it is stopped).

另请参阅

本文是否对您有所帮助?
(1500 个剩余字符)
感谢您的反馈
Microsoft 正在进行一项网上调查,以了解您对 MSDN 网站的意见。 如果您选择参加,我们将会在您离开 MSDN 网站时向您显示该网上调查。

是否要参加?
显示:
© 2015 Microsoft