IntelliJ

    开发 Dapr应用程序时,你通常使用 Dapr CLI 来启动你的 Dapr 服务,就像这样:

    这使用了默认的组件yaml文件(在上创建),这样你的服务就可以与本地Redis容器交互。 作为一个入门方法这很好用,但是如果你想要附加一个调试器到你的服务来进行代码调试呢? 在这里你可以使用dapr cli而不需要调用app。

    将调试器附加到服务中的一种方法是首先从命令行运行dapr run --,然后运行你的代码并附加调试器。 虽然这完全是一个可以接受的解决方案,但它也需要一些额外的步骤(比如在终端和IDE之间进行切换),以及对那些可能想要克隆你的仓库并点击 “play “按钮开始调试的开发人员进行一些指导。

    本文档介绍了如何从IntelliJ中直接使用dapr。 作为前提条件,要确保你已经通过dapr init初始化了Dapr的开发环境。

    让我们开始吧!

    首先,退出IntelliJ后再修改配置文件。

    1. %USERPROFILE%\AppData\Roaming\JetBrains\IntelliJIdea2020.1\tools\
    1. ~/Library/Application Support/JetBrains/IntelliJIdea2020.1/tools/

    如有需要,请更改路径中的IntelliJ版本。

    创建或编辑<CONFIG PATH>/tools/External\ Tools.xml中的文件(如有需要,请更改路径中的IntelliJ版本)。 如上所述,<CONFIG PATH>是操作系统依赖的。

    添加一个新的 <tool></tool>条目:

    你也可以为一个可以在多个项目中复用的sidecar工具创建一个新条目:

    1. <toolSet name="External Tools">
    2. ...
    3. <tool name="dapr with app-port" description="Dapr sidecar" showInMainMenu="false" showInEditor="false" showInProject="false" showInSearchPopup="false" disabled="false" useConsole="true" showConsoleOnStdOut="true" showConsoleOnStdErr="true" synchronizeAfterRun="true">
    4. <exec>
    5. <option name="COMMAND" value="c:\dapr\dapr.exe" />
    6. <!-- 3. Prompts user 4 times (in order): app id, app port, Dapr's http port, Dapr's grpc port. -->
    7. <option name="PARAMETERS" value="run --app-id $Prompt$ --app-port $Prompt$ --dapr-http-port $Prompt$ --dapr-grpc-port $Prompt$" />
    8. <!-- 4. Use the folder where the `components` folder is located -->
    9. <option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
    10. </exec>
    11. </tool>
    12. <!-- 1. Reusable entry for apps without app port. -->
    13. <tool name="dapr without app-port" description="Dapr sidecar" showInMainMenu="false" showInEditor="false" showInProject="false" showInSearchPopup="false" disabled="false" useConsole="true" showConsoleOnStdOut="true" showConsoleOnStdErr="true" synchronizeAfterRun="true">
    14. <exec>
    15. <option name="COMMAND" value="c:\dapr\dapr.exe" />
    16. <!-- 3. Prompts user 3 times (in order): app id, Dapr's http port, Dapr's grpc port. -->
    17. <!-- 4. Use the folder where the `components` folder is located -->
    18. <option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
    19. </exec>
    20. </tool>
    21. ...
    22. </toolSet>

    现在,为要调试的应用程序创建或编辑运行配置。 它可以在main()函数旁边的菜单中找到。

    现在,添加程序参数和环境变量: 这些端口需要与上面 “External Tool “条目中定义的端口相匹配。

    • 本例的命令行参数:-p 3000
    • 本例的环境变量:DAPR_HTTP_PORT=3005;DAPR_GRPC_PORT=52000

    以上一次性配置完成后,在IntelliJ中使用Dapr调试Java应用程序需要两个步骤:

    1. 通过 IntelliJ 中的 Tools -> External Tool 启动 dapr

    作为“外部工具”运行 Dapr

    1. 在调试模式下启动你的应用程序。

    调试之后,你要确保同时停止了dapr和你在IntelliJ中的应用。

    调试愉快!