Skip to main content

Perform Remote Debugging

๐ŸŽฏcontext

To debug your Java Solution, you can debug them in a number of ways. JUnit is one of them, and using the docker images for the events locally is another method. For more info, please see here. With the IBM DevOps Solution Workbench, you are also able to debug your Java solutions remotely. This means that a Java service is running on e.g. OpenShift and you can connect and debug from your local IDE.

Descriptionโ€‹

This How-To will explain the steps to debug your Java services remotely based on the Product Documentation - Remote Debugging of Java Services.

Prerequisitesโ€‹

  • An IDE (e.g., IntelliJ IDEA) configured for Java development.
  • A port-forwarding CLI installed locally, such as kubectl or oc
  • A deployed Java service (single or in an application composition)

Stepsโ€‹

1. Configure the service for remote debuggingโ€‹

The deployed Java service has to be configured for remote debugging but is different dependent on the service's deployment as standalone or inside an application composition. For both options, the steps are shown below.

In general, this is the necessary configuration that has to be added to the deployed Java service:

remoteDebugging:
enabled: true
autoscaling:
enabled: false
replicaCount: 1

Besides this necessary configuration, you are also free to add optional configuration to the remoteDebugging setting (the autoscaling and replicaCount setting remain the same) to customize the debugging behavior. It is described in Product Documentation - Remote Debugging add optional Configuration.

โ„น๏ธnote

If you are done with remote debugging, it is recommended to undo the changes you made for the configuration so that your deployed Java service has all capabilities enabled to deal with high traffic when used e.g. in production.

Inside an application composition projectโ€‹

For a single deploymentโ€‹

  1. For a single deployment, add the configuration above by directly using the Product Documentation - API Configuration Management.

    E.g. use the following base pattern for the respective curl command to execute:

    curl -X 'POST' \
    'https://{configHostname}/api/cfg/v1/runtimes/{runtimeName}/solutions/{solutionAcronym}/configurations/{configurationName}' \
    -H 'accept: */*' \
    -H 'Authorization: Bearer {bearerToken}'\
    -H 'Content-Type: text/plain' \
    -d 'remoteDebugging:
    enabled: true
    autoscaling:
    enabled: false
    replicaCount: 1'

    Parameters:

    • configHostname: The hostname of the Configuration Management.
    • runtimeName: The name Kubernetes namespace where the service is deployed, e.g. dev-stage
    • solutionAcronym: The acronym of your Java project, e.g. ORDERS
    • configurationName: The name of the configuration (its value has to be ssob-sdo-values-yaml)
    • bearerToken: The token needed to execute authenticated API calls

    ๐Ÿ’กtip

    It is also possible to add the configuration via the Swagger UI of the Configuration Management API if accessible. In that case, the hostname and token are already provided for this API.

    If using OpenShift for your IBM DevOps Solution Workbench instance, you can find the API via the OpenShift web console or the CLI. Therefore, you need the following information:
    The OpenShift project/namespace where the IBM DevOps Solution Workbench is installed (e.g. workbench-tools)

    Inside this namespace, search for a route with the name k5-configuration-management and extract and access the respective url in a browser. Then the Swagger UI of the Configuration Management API should be visible.

  2. Open your Java project in the Solution Designer and re-deploy your service inside the "CI/CD" section.

    Re-deploy the service

2. Clone and build the project locallyโ€‹

Open the respective project in the Solution Designer.

โ„น๏ธSetup your local development environment

If you have not setup your local development environment before, please follow the instructions here.

โš warning

Before proceeding, it is necessary that the build of the project can be created successfully. So if the project cloning ends with errors or there are compilation errors, mitigate all errors to enable a successful build of the project.

3. Setup your IDE for remote debuggingโ€‹

Open your Java project in your IDE and create a new remote debug configuration and add the following settings:

  • Host: localhost
  • Port: 5005
  • Command line arguments: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

In IntelliJ, an example remote debug configuration looks like this:

Remote JVM Config

Now your IDE is able to connect to the remote application's JVM running on the pod.

4. Port-Forwardingโ€‹

  1. Open an CLI and login to your cluster.

  2. Then execute the port-forwarding command:

    kubectl port-forward <podname> 5005:5005 -n <namespace>

    Parameters:

    • <podname>: The name of the pod running on a Kubernetes cluster
    • <namespace>: The Kubernetes namespace (same as the runtimeName in step 3)

    ๐Ÿ’กSearch for the podname

    If you don't know the podname, you can search it with your solution acronym (in lowercase, e.g. orders). In general, the podname has the following pattern: k5-<solutionAcronym>-...

    The following command provides you all pods that contain your solution acronym in their podname:

    kubectl get pods -n <namespace> | grep <solutionAcronym>

    Then use the found podname for the port-forwarding command.

Now the traffic on port 5005 (used by the debugger) is forwarded from the pod to your local machine.

5. Start debuggingโ€‹

  1. Start the debugger in your IDE.

  2. Trigger requests of your deployed solution.

๐Ÿ’กtip

Please note that this works for the other IDEs as well. For more info please see here.

๐ŸŒŸWell Done

You have successfully learned how to use JVM's remote debugging capabilities to inspect the behaviour of your Java service.