使用适用于 Eclipse 的 Service Fabric 插件开发 Java 应用程序Service Fabric plug-in for Eclipse Java application development

Eclipse 是面向 Java 开发人员的最常用集成开发环境 (IDE) 之一。Eclipse is one of the most widely used integrated development environments (IDEs) for Java developers. 本文介绍如何设置适用于 Azure Service Fabric 的 Eclipse 开发环境。In this article, we describe how to set up your Eclipse development environment to work with Azure Service Fabric. 了解如何安装 Service Fabric 插件、创建 Service Fabric 应用程序,以及将 Service Fabric 应用程序部署到 Eclipse 中的本地或远程 Service Fabric 群集。Learn how to install the Service Fabric plug-in, create a Service Fabric application, and deploy your Service Fabric application to a local or remote Service Fabric cluster in Eclipse.

Note

Windows 当前不支持 Eclipse 插件。The Eclipse plugin is currently not supported on Windows.

Important

请确保在系统上安装了 JDK 8,并且在 Eclipse 中选择了 JDK 8。Be sure JDK 8 is installed on the system and selected in Eclipse.

在 Eclipse 中安装或更新 Service Fabric 插件Install or update the Service Fabric plug-in in Eclipse

可在 Eclipse 中安装 Service Fabric 插件。You can install a Service Fabric plug-in in Eclipse. 该插件可帮助简化生成和部署 Java 服务的过程。The plug-in can help simplify the process of building and deploying Java services.

Important

Service Fabric 插件需要 Eclipse Neon 或更高版本。The Service Fabric plug-in requires Eclipse Neon or a later version. 请参阅此注意事项后面的说明,了解如何查看 Eclipse 的版本。See the instructions that follow this note for how to check your version of Eclipse. 如果安装的是较旧版本的 Eclipse,可以从 Eclipse 站点下载较新的版本。If you have an earlier version of Eclipse installed, you can download more recent versions from the Eclipse site. 建议不要在现有 Eclipse 安装版本的基础上进行安装(覆盖)。It is not recommended that you install on top of (overwrite) an existing installation of Eclipse. 可以先删除现有版本,然后再运行安装程序,也可以在另一目录中安装较新的版本。You can either remove it before running the installer or install the newer version in a different directory.

在 Ubuntu 上,建议直接从 Eclipse 站点进行安装,而不是使用包安装程序(aptapt-get)。On Ubuntu, we recommend installing directly from the Eclipse site rather than using a package installer (apt or apt-get). 这样做可确保获取最新版 Eclipse。Doing so ensures that you get the most current version of Eclipse.

Eclipse 站点安装 Eclipse Neon 或更高版本。Install Eclipse Neon or later from the Eclipse site. 还安装 Buildship 2.2.1 版或更高版本(Service Fabric 插件与更旧版本的 Buildship 不兼容):Also install version 2.2.1 or later of Buildship (the Service Fabric plug-in is not compatible with older versions of Buildship):

  • 若要检查已安装组件的版本,请在 Eclipse 中转到“帮助” > “关于 Eclipse” > “安装详细信息”。 To check the versions of installed components, in Eclipse, go to Help > About Eclipse > Installation Details.
  • 若要更新 Buildship,请参阅 Eclipse Buildship:适用于 Gradle 的 Eclipse 插件中的说明更新 Buildship。To update Buildship, see Eclipse Buildship: Eclipse Plug-ins for Gradle.
  • 若要检查并安装 Eclipse 的更新,请转到“帮助” > “检查更新”。 To check for and install updates for Eclipse, go to Help > Check for Updates.

若要安装 Service Fabric 插件,请在 Eclipse 中转到“帮助” > “安装新软件”。 Install the Service Fabric plug-in, in Eclipse, go to Help > Install New Software.

  1. 在“使用”框中,输入“https://dl.microsoft.com/eclipse”。In the Work with box, enter https://dl.microsoft.com/eclipse.

  2. 单击“添加” 。Click Add.

    适用于 Eclipse 的 Service Fabric 插件

  3. 选择 Service Fabric 插件,并单击“下一步”。Select the Service Fabric plug-in, and then click Next.

  4. 完成安装步骤,并接受 Microsoft 软件许可条款。Complete the installation steps, and then accept the Microsoft Software License Terms.

如果已安装 Service Fabric 插件,请安装最新版本。If you already have the Service Fabric plug-in installed, install the latest version.

  1. 若要检查可用的更新,请转到“帮助” > “关于 Eclipse” > “安装详细信息”。 To check for available updates, go to Help > About Eclipse > Installation Details.
  2. 在已安装插件的列表中选择“Service Fabric”,然后单击“更新”。In the list of installed plug-ins, select Service Fabric, and then click Update. 随后会安装可用的更新。Available updates will be installed.
  3. 更新 Service Fabric 插件后,还需刷新 Gradle 项目。Once you update the Service Fabric plug-in, also refresh the Gradle project. 右键单击 build.gradle,然后选择“刷新”。Right click build.gradle, then select Refresh.

Note

如果安装或更新 Service Fabric 插件时运行缓慢,原因可能是 Eclipse 设置有问题。If installing or updating the Service Fabric plug-in is slow, it might be due to an Eclipse setting. Eclipse 将收集有关所有更改的元数据,以更新已注册到 Eclipse 实例的站点。Eclipse collects metadata on all changes to update sites that are registered with your Eclipse instance. 若要加速 Service Fabric 插件更新的检查和安装过程,请转到“可用软件站点”。To speed up the process of checking for and installing a Service Fabric plug-in update, go to Available Software Sites. 清除所有站点对应的复选框,但指向 Service Fabric 插件位置 (https://dl.microsoft.com/eclipse/azure/servicefabric) 的站点除外。Clear the check boxes for all sites except for the one that points to the Service Fabric plug-in location (https://dl.microsoft.com/eclipse/azure/servicefabric).

Note

如果 Eclipse 在 Mac 上未按预期方式工作,或者需要你以超级用户身份运行,请转到 ECLIPSE_INSTALLATION_PATH 文件夹,然后导航到子文件夹 Eclipse.app/Contents/MacOS。If Eclipse isn't working as expected on your Mac, or needs you run as super user), go to the ECLIPSE_INSTALLATION_PATH folder and navigate to the subfolder Eclipse.app/Contents/MacOS. 运行 ./eclipse 启动 Eclipse。Start Eclipse by running ./eclipse.

在 Eclipse 中创建 Service Fabric 应用程序Create a Service Fabric application in Eclipse

  1. 在 Eclipse 中,转到“文件” > “新建” > “其他”。 In Eclipse, go to File > New > Other. 选择“Service Fabric 项目”,然后单击“下一步”。 Select Service Fabric Project, and then click Next.

    Service Fabric 新建项目第 1 页

  2. 输入项目的名称,然后单击“下一步”。Enter a name for your project, and then click Next.

    Service Fabric 新建项目第 2 页

  3. 从模板列表中,选择“服务模板”。In the list of templates, select Service Template. 选择服务模板类型(“执行组件”、“无状态”、“容器”或“来宾二进制”),然后单击“下一步”。Select your service template type (Actor, Stateless, Container, or Guest Binary), and then click Next.

    Service Fabric 新建项目第 3 页

  4. 输入服务名称和服务详细信息,然后单击“完成”。Enter the service name and service details, and then click Finish.

    Service Fabric 新建项目第 4 页

  5. 创建第一个 Service Fabric 项目时,请在“打开关联的透视图”对话框中单击“是”。 When you create your first Service Fabric project, in the Open Associated Perspective dialog box, click Yes.

    Service Fabric 新建项目第 5 页

  6. 新项目如下所示:Your new project looks like this:

    Service Fabric 新建项目第 6 页

在 Eclipse 中生成 Service Fabric 应用程序Build a Service Fabric application in Eclipse

  1. 右键单击新建的 Service Fabric 应用程序,然后选择“Service Fabric”。Right-click your new Service Fabric application, and then select Service Fabric.

    Service Fabric 右键菜单

  2. 在上下文菜单中,选择以下选项之一:In the context menu, select one of the following options:

    • 若要生成应用程序但不清理,请单击“生成应用程序”。To build the application without cleaning, click Build Application.
    • 若要生成已清理的应用程序,请单击“重新生成应用程序”。To do a clean build of the application, click Rebuild Application.
    • 若要清理已生成项目的应用程序,请单击“清理应用程序”。To clean the application of built artifacts, click Clean Application.

在 Eclipse 中将 Service Fabric 应用程序部署到本地群集Deploy a Service Fabric application to the local cluster with Eclipse

生成 Service Fabric 应用程序后,请遵循以下步骤将其部署到本地群集。After you have built your Service Fabric application, follow these steps to deploy it to the local cluster.

  1. 如果尚未启动本地群集,请遵照设置本地群集中的说明启动本地群集,并确保该群集正在运行。If you haven't started the local cluster, follow the instructions in Set up a local cluster to start your local cluster and make sure that it is running.

  2. 右键单击 Service Fabric 应用程序,并选择“Service Fabric”。Right-click your Service Fabric application, and then select Service Fabric.

    Service Fabric 右键菜单

  3. 在上下文菜单中,单击“部署应用程序”。From the context menu, click Deploy Application.

  4. 可以在控制台窗口中跟踪部署操作的进度。You can follow the progress of the deploy operation in the Console window.

  5. 若要验证应用程序是否正在运行,请在浏览器窗口中的本地群集上打开 Service Fabric Explorer (http://localhost:19080/Explorer)。To verify that your application is running, open Service Fabric Explorer on your local cluster in a browser window http://localhost:19080/Explorer. 展开“应用程序”节点,确保应用程序正在运行。Expand the Applications node and make sure your application is running.

若要了解如何使用本地群集在 Eclipse 中调试应用程序,请参阅在 Eclipse 中调试 Java 服务To learn how to debug your application in Eclipse using the local cluster, see Debug a Java service in Eclipse.

也可以使用“发布应用程序”命令将应用程序部署到本地群集:You can also deploy your application to the local cluster with the Publish Application command:

  1. 右键单击 Service Fabric 应用程序,并选择“Service Fabric”。Right-click your Service Fabric application, and then select Service Fabric.

  2. 在上下文菜单中,单击“发布应用程序...”。From the context menu, click Publish Application....

  3. 在“发布应用程序”窗口中,选择“PublishProfiles/Local.json”为目标配置文件,然后单击“发布”。 In the Publish Application window, choose PublishProfiles/Local.json as the Target Profile and click Publish.

    本地“发布”对话框

    默认情况下,Local.json 发布配置文件设置为发布到本地群集。By default, the Local.json publishing profile is set up to publish to the local cluster. 有关发布配置文件中提供的连接和终结点参数的详细信息,请参阅下一部分。For more information about the connection and endpoint parameters present in publishing profiles, see the next section.

在 Eclipse 中将 Service Fabric 应用程序发布到 AzurePublish your Service Fabric application to Azure with Eclipse

若要将应用程序发布到云中,请执行以下步骤:To publish your application to the cloud, follow these steps:

  1. 若要将应用程序发布到云中的安全群集,需要使用 X.509 证书来与群集通信。To publish your application to a secure cluster in the cloud, you need an X.509 certificate to use to communicate with your cluster. 在测试和开发环境中,使用的证书通常是群集证书。In test and development environments, the certificate used is often the cluster certificate. 在生产环境中,该证书应该是不同于群集证书的客户端证书。In production environments, the certificate should be a client certificate that is distinct from the cluster certificate. 需要证书和私钥。You need both the certificate and the private key. 证书(和密钥)文件必须采用 PEM 格式。The certificate (and key) file must be PEM-formatted. 可以使用以下 openssl 命令,从 PFX 文件创建包含证书和私钥的 PEM 文件:You can create a PEM file that contains the certificate and private key from a PFX file with the following openssl command:

    openssl pkcs12 -in your-cert-file.pfx -out your-cert-file.pem -nodes -passin pass:your-pfx-password
    

    如果 PFX 文件不受密码保护,请对最后一个参数使用 --passin pass:If the PFX file is not password protected, use --passin pass: for the last parameter.

  2. 打开 PublishProfiles 目录下的 Cloud.json 文件。Open the Cloud.json file under the PublishProfiles directory. 需要适当地为群集配置群集终结点和安全凭据。You need to configure the cluster endpoint and security credentials appropriately for your cluster.

    • ConnectionIPOrURL 字段包含群集的 IP 地址或 URL。The ConnectionIPOrURL field contains the IP address or URL of your cluster. 请注意,值不包含 URL 方案 (https://)。Note that the value does not contain the URL scheme (https://).

    • ConnectionPort 字段默认为 19080,除非为群集显式更改了此端口。By default the ConnectionPort field should be 19080, unless you explicitly have changed this port for your cluster.

    • ClientKey 字段应指向本地计算机上包含客户端或群集证书私钥的 PEM 格式的 .pem 或 .key 文件。The ClientKey field should point to a PEM-formatted .pem or .key file on your local machine that contains the private key for your client or cluster certificate.

    • ClientCert 字段应指向本地计算机上包含客户端或群集证书数据的 PEM 格式的 .pem 或 .crt 文件。The ClientCert field should point to a PEM-formatted .pem or .crt file on your local machine that contains the certificate data for your client or cluster. 证书。certificate.

      {
           "ClusterConnectionParameters":
           {
              "ConnectionIPOrURL": "lnxxug0tlqm5.chinanorth.cloudapp.chinacloudapi.cn",
              "ConnectionPort": "19080",
              "ClientKey": "[path_to_your_pem_file_on_local_machine]",
              "ClientCert": "[path_to_your_pem_file_on_local_machine]"
           }
      }
      
  3. 右键单击 Service Fabric 应用程序,并选择“Service Fabric”。Right-click your Service Fabric application, and then select Service Fabric.

  4. 在上下文菜单中,单击“发布应用程序...”。From the context menu, click Publish Application....

  5. 在“发布应用程序”窗口中,选择“PublishProfiles/Cloud.json”为目标配置文件,然后单击“发布”。 In the Publish Application window, choose PublishProfiles/Cloud.json as the Target Profile and click Publish.

    云端“发布”对话框

  6. 可以在控制台窗口中跟踪发布操作的进度。You can follow the progress of the publish operation in the Console window.

  7. 若要验证应用程序是否正在运行,请在浏览器窗口中的 Azure 群集上打开 Service Fabric Explorer。To verify that your application is running, open Service Fabric Explorer on your Azure cluster in a browser window. 在上述示例中,URL 为:https://lnxxug0tlqm5.chinanorth.cloudapp.chinacloudapi.cn:19080/ExplorerFor the example above, this would be: https://lnxxug0tlqm5.chinanorth.cloudapp.chinacloudapi.cn:19080/Explorer. 展开“应用程序”节点,确保应用程序正在运行。Expand the Applications node and make sure your application is running.

在安全 Linux 群集上,如果应用程序包含 Reliable Services 服务,则还需要配置可供服务用来调用 Service Fabric 运行时 API 的证书。On secure Linux clusters, if your application contains Reliable Services services, you will also need to configure a certificate that your services can use to call Service Fabric runtime APIs. 若要了解详细信息,请参阅将 Reliable Services 应用程序配置为在 Linux 群集上运行To learn more, see Configure a Reliable Services app to run on Linux clusters.

有关如何将以 Java 编写的 Service Fabric Reliable Services 应用程序部署到安全 Linux 群集的快速演练,请参阅快速入门:部署 Java Reliable Services 应用程序For a quick walk through of how to deploy a Service Fabric Reliable Services application written in Java to a secure Linux cluster, see Quickstart: Deploy a Java Reliable Services application.

使用 Eclipse 运行配置部署 Service Fabric 应用程序Deploy a Service Fabric application by using Eclipse run configurations

部署 Service Fabric 应用程序的另一种方式是使用 Eclipse 运行配置。An alternate way to deploy your Service Fabric application is by using Eclipse run configurations.

  1. 在 Eclipse 中,转到“运行” > “运行配置”。 In Eclipse, go to Run > Run Configurations.

  2. 在“Gradle 项目”下面,选择“ServiceFabricDeployer”运行配置。 Under Gradle Project, select the ServiceFabricDeployer run configuration.

  3. 在右窗格中的“参数”选项卡上,确保适当地为部署设置了 ipportclientCertclientKey 参数。In the right pane, on the Arguments tab, make sure the ip, port, clientCert, and clientKey parameters are set appropriately for your deployment. 这些参数默认设置为部署到本地群集,如以下屏幕截图中所示。By default, the parameters are set to deploy to the local cluster as in the following screenshot. 若要将应用发布到 Azure,可以修改参数,以包含 Azure 群集的终结点详细信息和安全凭据。To publish your app to Azure you can modify the parameters to contain the endpoint details and security credentials for your Azure cluster. 有关详细信息,请参阅上一部分在 Eclipse 中将 Service Fabric 应用程序发布到 AzureFor more information, see the previous section, Publish your Service Fabric application to Azure with Eclipse.

    运行配置对话框 - 本地

  4. 确保“工作目录”指向要部署的应用程序。Make sure that Working Directory points to the application you want to deploy. 若要更改应用程序,请单击“工作区”按钮,然后选择所需的应用程序。To change the application, click the Workspace button, and then select the application you want.

  5. 单击“应用”,然后单击“运行”。 Click Apply, and then click Run.

应用程序会在片刻之后生成和部署。Your application builds and deploys within a few moments. 可在 Service Fabric Explorer 中监视部署状态。You can monitor the deployment status in Service Fabric Explorer.

将 Service Fabric 服务添加到 Service Fabric 应用程序Add a Service Fabric service to your Service Fabric application

要将 Service Fabric 服务添加到现有的 Service Fabric 应用程序,请执行以下步骤:To add a Service Fabric service to an existing Service Fabric application, do the following steps:

  1. 右键单击要将该服务添加到的项目,然后单击“Service Fabric”。Right-click the project you want to add a service to, and then click Service Fabric.

    Service Fabric 添加服务第 1 页

  2. 单击“添加 Service Fabric 服务”,完成将服务添加到项目的一系列步骤。Click Add Service Fabric Service, and complete the set of steps to add a service to the project.

  3. 选择要添加到项目的服务模板,然后单击“下一步”。Select the service template you want to add to your project, and then click Next.

    Service Fabric 添加服务第 2 页

  4. 输入服务名称(根据需要输入其他详细信息),然后单击“添加服务”按钮。Enter the service name (and other details, as needed), and then click the Add Service button.

    Service Fabric 添加服务第 3 页

  5. 添加该服务后,整个项目结构类似于以下项目:After the service is added, your overall project structure looks similar to the following project:

    Service Fabric 添加服务第 4 页

编辑 Service Fabric Java 应用程序的清单版本Edit Manifest versions of your Service Fabric Java application

若要编辑清单版本,请右键单击项目,转到“Service Fabric”,然后从下拉菜单中选择“编辑清单版本...”。 To edit manifest versions, right click on the project, go to Service Fabric and select Edit Manifest Versions... from the menu dropdown. 在向导中,可以更新应用程序清单和服务清单的清单版本,以及“代码”、“配置”和“数据”包的版本。 In the wizard, you can update the manifest versions for application manifest, service manifest and the versions for Code, Config and Data packages.

如果选中“自动更新应用程序和服务的版本”选项,然后更新了一个版本,则会自动更新清单版本。If you check the option Automatically update application and service versions and then update a version, then the manifest versions will be automatically updated. 举例来说,首先选中该复选框,然后将“代码”版本从 0.0.0 更新为 0.0.1,最后再单击“完成”,则服务清单版本和应用程序清单版本就会自动更新为 0.0.1。 To give an example, you first select the check-box, then update the version of Code version from 0.0.0 to 0.0.1 and click on Finish, then service manifest version and application manifest version will be automatically updated to 0.0.1.

升级 Service Fabric Java 应用程序Upgrade your Service Fabric Java application

对于升级方案,假设使用 Service Fabric 插件在 Eclipse 中创建了 App1 项目。For an upgrade scenario, say you created the App1 project by using the Service Fabric plug-in in Eclipse. 已使用该插件部署了此项目,以创建名为 fabric:/App1Application 的应用程序。You deployed it by using the plug-in to create an application named fabric:/App1Application. 该应用程序的类型为 App1ApplicationType,应用程序版本为 1.0。The application type is App1ApplicationType, and the application version is 1.0. 现在,要在不影响可用性的情况下升级该应用程序。Now, you want to upgrade your application without interrupting availability.

首先,请对应用程序进行任何更改,然后重新生成已修改的服务。First, make any changes to your application, and then rebuild the modified service. 使用服务的更新版本(以及相关的代码、配置或数据)更新已修改服务的清单文件 (ServiceManifest.xml)。Update the modified service's manifest file (ServiceManifest.xml) with the updated versions for the service (and Code, Config, or Data, as relevant). 同时,请使用更新的应用程序版本号和已修改的服务修改应用程序的清单 (ApplicationManifest.xml)。Also, modify the application's manifest (ApplicationManifest.xml) with the updated version number for the application and the modified service.

若要使用 Eclipse 升级应用程序,可以创建重复的运行配置文件。To upgrade your application by using Eclipse, you can create a duplicate run configuration profile. 然后,根据需要使用该文件升级应用程序。Then, use it to upgrade your application as needed.

  1. 转到“运行” > “运行配置”。 Go to Run > Run Configurations. 在左窗格中,单击“Gradle 项目”左侧的小箭头。In the left pane, click the small arrow to the left of Gradle Project.
  2. 右键单击“ServiceFabricDeployer”,然后选择“复制”。 Right-click ServiceFabricDeployer, and then select Duplicate. 输入此配置的新名称,例如 ServiceFabricUpgraderEnter a new name for this configuration, for example, ServiceFabricUpgrader.
  3. 在右侧面板中的“参数”选项卡上,将 -Pconfig='deploy' 更改为 -Pconfig='upgrade' ,然后单击“应用”。 In the right panel, on the Arguments tab, change -Pconfig='deploy' to -Pconfig='upgrade', and then click Apply.

此过程会创建并保存随时可用于升级应用程序的运行配置配置文件。This process creates and saves a run configuration profile you can use at any time to upgrade your application. 此过程还会从应用程序清单文件中获取最近更新的应用程序类型版本。It also gets the latest updated application type version from the application manifest file.

应用程序升级需要几分钟时间。The application upgrade takes a few minutes. 可在 Service Fabric Explorer 中监视应用程序升级状态。You can monitor the application upgrade in Service Fabric Explorer.

迁移要与 Maven 配合使用的旧式 Service Fabric Java 应用程序Migrating old Service Fabric Java applications to be used with Maven

我们最近已将 Service Fabric Java 库从 Service Fabric Java SDK 移至 Maven 存储库。We have recently moved Service Fabric Java libraries from Service Fabric Java SDK to Maven repository. 虽然使用 Eclipse 生成的新应用程序会生成最近更新的项目(会兼容 Maven),但你可以对现有的 Service Fabric 无状态或执行组件 Java 应用程序(此前使用 Service Fabric Java SDK)进行更新,使之能够使用 Maven 提供的 Service Fabric Java 依赖项。While the new applications you generate using Eclipse, will generate latest updated projects (which will be able to work with Maven), you can update your existing Service Fabric stateless or actor Java applications, which were using the Service Fabric Java SDK earlier, to use the Service Fabric Java dependencies from Maven. 请按此处提及的步骤操作,确保旧版应用程序兼容 Maven。Please follow the steps mentioned here to ensure your older application works with Maven.

后续步骤Next steps