Fork me on Gitee

IDEA调试Ambari-server源码

1. 远程调试

1.远程调试动机

直接阅读源码,晦涩难懂,很难顺着某个方法追踪下去。

本地debug,环境搭建困难,平台兼容性等。

基于此:采用Debug方式启动Ambari-Server,使用IDEA远程连接到哦Ambari-Server,对Ambari-Server进行远程调试,追踪Ambari-Server执行流程。

2.远程调试原理

本地和远程主机两个VM之间通过Debug协议通过socket通信,传递调试指令和缇欧是信息。

被调试程序的远程虚拟机:Debug服务端,监听Debug调试指令。

调试程序的本地虚拟机:IDEA中配置的Remote Server,指定Debug服务的Host及Port,以供Debug客户端程序连接。

注意事项:

  • 被调试的服务器需开启调试模式。
  • 服务端代码和本地代码必须保持一致。

2.IDEA远程调试

2.1 调试前准备工作

此处使用IDEA专业版2020.3。

  1. 在IDEA界面上方工具栏文本框中,选择Edit Conifgurations如下图

image-20220421101532802

  1. 点击Edit Configurations后,点击+号,选择Remote JVM Debug,

    image-20220421101915555

  2. 填写Host及Port信息,点击OK。

image-20220421101843008

2.2 启动 debug模式

Ambari-Server以Debug模式启动

ambari-server start --debug
Using python  /usr/bin/python
Starting ambari-server
Ambari Server running with administrator privileges.
Organizing resource files at /var/lib/ambari-server/resources...
Ambari database consistency check started...
Server PID at: /var/run/ambari-server/ambari-server.pid
Server out at: /var/log/ambari-server/ambari-server.out
Server log at: /var/log/ambari-server/ambari-server.log
Waiting for server start...................
Server started listening on 8080

DB configs consistency check: no errors and warnings were found.
Ambari Server 'start' completed successfully.

在程序中相应的位置打上断点。此处使用Ambari提供的swagger接口进行调试。

Ambari-server swagger地址:http://ip:port/api-docs

image-20220421102749961

此处以Host Components中的Get all host components for a host方法为例。

image-20220421102937966

顺着Get方法路径在IDEA Swagger.json中寻找该方法。

image-20220421103115246

根据swagger.json中提供的operatonId,找到getHostComponents()

image-20220421103227926

将断点放在方法入口处,即3817行位置。并启动Debug。

image-20220421104929960

填写集群名称,点击Try。

image-20220421103618312

可以看到程序顺利进入断点。

image-20220421105036904


参考文档

  1. Ambari(四)----使用IDEA远程调试Ambari-Server

  2. ambari 的swagger

posted @ 2022-04-21 10:57  shine-rainbow  阅读(515)  评论(0编辑  收藏  举报