使用 Visual Studio Code 开发 Java Service Fabric 应用程序Develop Java Service Fabric applications with Visual Studio Code

使用适用于 VS Code 的 Service Fabric Reliable Services 扩展可在 Windows、Linux 和 macOS 操作系统上轻松生成 Java Service Fabric 应用程序。The Service Fabric Reliable Services extension for VS Code makes it easy to build Java Service Fabric applications on Windows, Linux, and macOS operating systems.

本文介绍如何使用 Visual Studio Code 生成、部署和调试 Java Service Fabric 应用程序。This article shows you how to build, deploy, and debug a Java Service Fabric application using Visual Studio Code.

重要

可在 Windows 计算机上开发 Service Fabric Java 应用程序,但只能将其部署到 Azure Linux 群集。Service Fabric Java applications can be developed on Windows machines, but can be deployed onto Azure Linux clusters only. Windows 不支持调试 Java 应用程序。Debugging Java applications is not supported on Windows.

必备条件Prerequisites

本文假设你已安装 VS Code、适用于 VS Code 的 Service Fabric Reliable Services 扩展,以及开发环境所需的所有依赖项。This article assumes that you have already installed VS Code, the Service Fabric Reliable Services extension for VS Code, and any dependencies required for your development environment. 有关详细信息,请参阅入门To learn more, see Getting Started.

下载示例Download the sample

本文使用 Service Fabric Java 应用程序快速入门示例 GitHub 存储库中的投票应用程序。This article uses the Voting application in the Service Fabric Java application quickstart sample GitHub repository.

若要将此存储库克隆到开发计算机,请在终端窗口(Windows 上的命令窗口)中运行以下命令:To clone the repository to your development machine, run the following command from a terminal window (command window on Windows):

git clone https://github.com/Azure-Samples/service-fabric-java-quickstart.git

在 VS Code 中打开应用程序Open the application in VS Code

打开 VS Code。Open VS Code. 单击活动栏中的“Explorer”图标并单击“打开文件夹”,或者单击“文件”->“打开文件夹”。 Click the Explorer icon in the Activity Bar and click Open Folder, or click File -> Open Folder. 导航到存储库克隆到的文件夹中的 ./service-fabric-java-quickstart/Voting 目录,然后单击“确定”。 Navigate to the ./service-fabric-java-quickstart/Voting directory in the folder where you cloned the repository then click OK. 工作区应包含以下屏幕截图中所示的相同文件。The workspace should contain the same files shown in the screenshot below.

工作区中的 Java 投票应用程序

构建应用程序Build the application

  1. 在 VS Code 中,按 (Ctrl + Shift + p) 打开命令面板Press (Ctrl + Shift + p) to open the Command Palette in VS Code.

  2. 搜索并选择“Service Fabric: 生成应用程序”命令。 Search for and select the Service Fabric: Build Application command. 生成输出将发送到集成式终端。The build output is sent to the integrated terminal.

    VS Code 中的“生成应用程序”命令

将应用程序部署到本地群集Deploy the application to the local cluster

生成应用程序后,可将其部署到本地群集。After you have built the application, you can deploy it to the local cluster.

重要

Windows 计算机上不支持将 Java 应用程序部署到本地群集。Deploying Java applications to the local cluster is not supported on Windows machines.

  1. 命令面板中,选择“Service Fabric: 部署应用程序(Localhost)”命令。 From the Command Palette, select the Service Fabric: Deploy Application (Localhost) command. 安装过程的输出将发送到集成式终端。The output of the install process is sent to the integrated terminal.

    VS Code 中的“部署应用程序”命令

  2. 部署完成后,启动浏览器并打开 Service Fabric Explorer: http://localhost:19080/ExplorerWhen the deployment is complete, launch a browser and open Service Fabric Explorer: http://localhost:19080/Explorer. 应会看到应用程序正在运行。You should see that the application is running. 此过程可能需要一段时间,请耐心等待。This may take some time, so be patient.

    Service Fabric Explorer 中的投票应用程序

  3. 确认应用程序正在运行后,启动浏览器并打开此页: http://localhost:8080After you've verified that the application is running, launch a browser and open this page: http://localhost:8080. 这是应用程序的 Web 前端。This is the web front-end of the application. 可以添加项,并单击这些项进行投票。You can add items and click on them to vote.

    浏览器中的投票应用程序

  4. 若要从群集中删除应用程序,请在命令面板中选择“Service Fabric: 删除应用程序”命令。To remove the application from the cluster, select the Service Fabric: Remove Application command from the Command Palette. 卸载过程的输出将发送到集成式终端。The output of the uninstall process is sent to the integrated terminal. 可以使用 Service Fabric Explorer 来验证是否已从本地群集中删除该应用程序。You can use Service Fabric Explorer to verify that the application has been removed from the local cluster.

调试应用程序Debug the application

在 VS Code 中调试应用程序时,该应用程序必须在本地群集上运行。When debugging applications in VS Code, the application must be running on a local cluster. 然后,可将断点添加到代码中。Breakpoints can then be added to the code.

重要

Windows 计算机上不支持调试 Java 应用程序。Debugging Java applications is not supported on Windows machines.

若要准备好调试 VotingDataService 和投票应用程序,请完成以下步骤:To prepare the VotingDataService and the Voting application for debugging, complete the following steps:

  1. 更新 Voting/VotingApplication/VotingDataServicePkg/Code/entryPoint.sh 文件。Update the Voting/VotingApplication/VotingDataServicePkg/Code/entryPoint.sh file. 注释掉第 6 行中的命令(使用 #),并将以下命令添加到文件的底部:Comment out the command on line 6 (use '#') and add the following command to the bottom of the file:

    java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Djava.library.path=$LD_LIBRARY_PATH -jar VotingDataService.jar
    
  2. 更新 Voting/VotingApplication/ApplicationManifest.xml 文件。Update the Voting/VotingApplication/ApplicationManifest.xml file. StatefulService 元素中将 MinReplicaSetSizeTargetReplicaSetSize 属性设置为“1”:Set the MinReplicaSetSize and the TargetReplicaSetSize attributes to "1" in the StatefulService element:

    <StatefulService MinReplicaSetSize="1" ServiceTypeName="VotingDataServiceType" TargetReplicaSetSize="1">
    
  3. 单击活动栏中的“调试”图标,在 VS Code 中打开调试程序视图。Click the Debug icon in the Activity Bar to open the debugger view in VS Code. 单击调试程序视图顶部的齿轮图标,从环境下拉菜单中选择“Java”。 Click the gear icon at the top of the debugger view and select Java from the dropdown environment menu. 此时会打开 launch.json 文件。The launch.json file opens.

    VS Code 工作区中的调试图标

  4. 在 launch.json 文件中,将名为 Debug (Attach) 的配置中的端口值设置为 8001In the launch.json file, set the port value in the configuration named Debug (Attach) to 8001. 保存文件。Save the file.

    调试 launch.json 的配置

  5. 使用“Service Fabric: 部署应用程序(Localhost)”命令将应用程序部署到本地群集。 Deploy the application to the local cluster by using the Service Fabric: Deploy Application (Localhost) command. 验证该应用程序是否在 Service Fabric Explorer 中运行。Verify that the application is running in Service Fabric Explorer. 现在,可以开始调试应用程序。Your application is now ready to be debugged.

若要设置断点,请完成以下步骤:To set a breakpoint, complete the following steps:

  1. 在 Explorer 中,打开 /Voting/VotingDataService/src/statefulservice/VotingDataService.java 文件。In Explorer, open the /Voting/VotingDataService/src/statefulservice/VotingDataService.java file. addItem 方法中 try 块内的第一个代码行(第 80 行)上设置一个断点。Set a breakpoint on first line of code in the try block in the addItem method (line 80).

    在投票数据服务中设置断点

    重要

    确保在可执行的代码行上设置断点。Make sure you set breakpoints on executable lines of code. 例如,调试程序会遗漏方法声明、try 语句或 catch 语句上设置的断点。For example breakpoints set on method declarations, try statements, or catch statements will be missed by the debugger.

  2. 若要开始调试,请单击活动栏中的“调试”图标,从调试菜单中选择“Debug (Attach)”配置,然后单击运行按钮(绿色箭头)。 To begin debugging, click the Debug icon in the Activity Bar, select the Debug (Attach) configuration from the debug menu, and click the run button (green arrow).

    Debug (Attach) 配置

  3. 在 Web 浏览器中转到 http://localhost:8080In a web browser, go to http://localhost:8080. 在文本框中键入新项,然后单击“+ 添加”。 Type a new item in the text box and click + Add. 应会命中该断点。Your breakpoint should be hit. 可以使用 VS Code 顶部的“调试”工具栏继续执行、逐行执行每行、逐步执行方法,或跳出当前方法。You can use the Debug toolbar at the top of VS Code to continue execution, step over lines, step into methods, or step out of the current method.

    命中断点

  4. 若要结束调试会话,请单击 VS Code 顶部的“调试”工具栏上的插入图标。To end the debugging session, click the plug icon on the Debug toolbar at the top of VS Code.

    从调试程序断开连接

  5. 完成调试后,可以使用“Service Fabric: 删除应用程序”命令以从本地群集中删除投票应用程序。 When you've finished debugging, you can use the Service Fabric: Remove Application command to remove the Voting application from your local cluster.

后续步骤Next steps