教程:调试本地 Service Fabric 群集上部署的 Java 应用程序Tutorial: Debug a Java application deployed on a local Service Fabric cluster

本教程是一个系列中的第二部分。This tutorial is part two of a series. 其中介绍了如何使用适用于 Service Fabric 应用程序的 Eclipse 来附加远程调试器。You learn how to attach a remote debugger using Eclipse for the Service Fabric application. 此外,还介绍如何将运行的应用程序发出的日志重定向到方便开发人员处理的位置。Additionally, you learn how to redirect logs from the running applications to a location convenient for the developer.

在此系列教程中,你会学习如何:In this tutorial series you learn how to:

本系列教程的第二部分将介绍如何:In part two of the series, you learn how to:

  • 使用 Eclipse 调试 Java 应用程序Debug Java application using Eclipse
  • 将日志重定向到可配置的位置Redirect logs to a configurable location

先决条件Prerequisites

在开始学习本教程之前:Before you begin this tutorial:

  • 设置适用于 MacLinux 的开发环境。Set up your development environment for Mac or Linux. 遵照说明安装 Eclipse 插件、Gradle、Service Fabric SDK 和 Service Fabric CLI (sfctl)。Follow the instructions to install the Eclipse plug-in, Gradle, the Service Fabric SDK, and the Service Fabric CLI (sfctl).

下载投票示例应用程序Download the Voting sample application

如果未生成本教程系列的第一部分中的投票示例应用程序,还可以下载它。If you did not build the Voting sample application in part one of this tutorial series, you can download it. 在命令窗口中,运行以下命令,将示例应用程序存储库克隆到本地计算机。In a command window, run the following command to clone the sample app repository to your local machine.

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

生成应用程序并将其部署到本地开发群集。Build and deploy the application to the local development cluster.

使用 Eclipse 调试 Java 应用程序Debug Java application using Eclipse

  1. 在计算机上打开 Eclipse IDE,单击“文件”->“导入...”。****Open the Eclipse IDE on your machine and click on File -> Import....

  2. 在弹出窗口中,选择“常规”->“将现有项目导入工作区”选项并按“下一步”。****In the popup window, select the General -> Existing Projects into Workspace option and press Next.

  3. 在“导入项目”窗口中,选择“选择根目录”选项并选取“Voting”目录。********In the Import Projects window, choose the Select root directory option and pick the Voting directory. 如果已学完了系列教程 1,则“Voting”目录出现在“Eclipse-workspace”目录中。********If you followed tutorial series one, the Voting directory is in the Eclipse-workspace directory.

  4. 更新要调试的服务的 entryPoint.sh,以便使用远程调试参数启动 Java 进程。Update entryPoint.sh of the service you wish to debug, so that it starts the Java process with remote debug parameters. 在本教程中,使用了无状态前端:Voting/VotingApplication/VotingWebPkg/Code/entryPoint.sh。设置了端口 8001,以便在此示例中进行调试。For this tutorial the stateless front end is used: Voting/VotingApplication/VotingWebPkg/Code/entryPoint.sh. Port 8001 is set for debugging in this example.

    java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Djava.library.path=$LD_LIBRARY_PATH -jar VotingWeb.jar
    
  5. 通过将要调试的服务的实例计数或副本计数设置为 1 来更新应用程序清单。Update the Application Manifest by setting the instance count or the replica count for the service that is being debugged to one. 此设置可避免用于调试的端口出现冲突。This setting avoids conflicts for the port that is used for debugging. 例如,对于无状态服务,设置 InstanceCount="1";对于有状态服务,将目标和最小副本集大小设置为 1,如下所示:TargetReplicaSetSize="1" MinReplicaSetSize="1"For example, for stateless services, set InstanceCount="1" and for stateful services set the target and min replica set sizes to 1 as follows: TargetReplicaSetSize="1" MinReplicaSetSize="1".

  6. 在 Eclipse IDE 中,选择“运行”->“调试配置”->“远程 Java 应用程序”,按“新建”按钮,设置以下属性,然后单击“应用”。************In the Eclipse IDE, select Run -> Debug Configurations -> Remote Java Application, press the New button, set the properties as follows and click Apply.

    Name: Voting
    Project: Voting
    Connection Type: Standard
    Host: localhost
    Port: 8001
    
  7. Voting/VotingWeb/src/statelessservice/HttpCommunicationListener.java 文件的第 109 行中插入一个断点。Put a breakpoint on line 109 of the Voting/VotingWeb/src/statelessservice/HttpCommunicationListener.java file.

  8. 在包资源管理器中右键单击“投票”项目,然后单击“Service Fabric”->“发布应用程序...”。********In the Package Explorer, right click on the Voting project and click Service Fabric -> Publish Application ...

  9. 在“发布应用程序”窗口的下拉列表中,选择“Local.json”并单击“发布”。************In the Publish Application window, select Local.json from the dropdown, and click Publish.

  10. 在 Eclipse IDE 中,选择“运行”->“调试配置”->“远程 Java 应用程序”,单击创建的“投票”配置,然后单击“调试”。************In the Eclipse IDE, select Run -> Debug Configurations -> Remote Java Application, click on the Voting configuration you created and click Debug.

  11. 转到 Web 浏览器并访问 localhost:8080Go to your web browser and access localhost:8080. 这将自动命中断点,Eclipse 将进入调试透视图This will automatically hit the breakpoint and Eclipse will enter the Debug perspective.

现在,可以应用这些相同的步骤来在 Eclipse 中调试任何 ServiceFabric 应用程序。Now you can apply these same steps to debug any Service Fabric application in Eclipse.

将应用程序日志重定向到自定义位置Redirect application logs to custom location

以下步骤说明如何将应用程序日志从默认的 /var/log/syslog 位置重定向到自定义位置。The following steps walk through how to redirect the application logs from the default /var/log/syslog location to a custom location.

  1. 目前,Service Fabric Linux 群集中运行的应用程序仅支持选取一个日志文件。Currently, applications running in Service Fabric Linux clusters only support picking up a single log file. 要设置应用程序以便日志始终转到 /tmp/mysfapp0.0.log,请在位置 Voting/VotingApplication/VotingWebPkg/Code/logging.properties 创建名为 logging.properties 的文件并添加以下内容。To set up an application so that the logs always go to /tmp/mysfapp0.0.log, create a file named logging.properties in the following location Voting/VotingApplication/VotingWebPkg/Code/logging.properties and add the following content.

    handlers = java.util.logging.FileHandler
    
    java.util.logging.FileHandler.level = ALL
    java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
    
    # This value specifies your custom location.
    # You will have to ensure this path has read and write access by the process running the SF Application
    java.util.logging.FileHandler.pattern = /tmp/mysfapp0.0.log
    
  2. Voting/VotingApplication/VotingWebPkg/Code/entryPoint.sh 中添加 Java 执行命令的以下参数:Add the following parameter in the Voting/VotingApplication/VotingWebPkg/Code/entryPoint.sh for the Java execution command:

    -Djava.util.logging.config.file=logging.properties
    

    下面的示例显示了附加了调试器的示例执行,与前一部分中的执行类似。The following example shows a sample execution with the debugger attached, similar to the execution in the previous section.

    java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Djava.library.path=$LD_LIBRARY_PATH -Djava.util.logging.config.file=logging.properties -jar VotingWeb.jar
    

现在,你已了解如何在开发 Service Fabric Java 应用程序时调试和访问应用程序日志。At this stage, you have learned how to debug and access your application logs while developing your Service Fabric Java applications.

后续步骤Next steps

本教程的此部分介绍了如何:In this part of the tutorial, you learned how to:

  • 使用 Eclipse 调试 Java 应用程序Debug Java application using Eclipse
  • 将日志重定向到可配置的位置Redirect logs to a configurable location

转到下一教程:Advance to the next tutorial: