Three Things to Know Before You Debug a Spring Boot Application
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. —Brian W. Kernigham
Debugging is hard, and in some places—like in the cloud, production, or Java Spring—it’s even harder. Moreover, debugging isn’t just about finding or fixing bugs in code. It also allows developers to better understand existing codebases by allowing them to see the flow of the entire application when starting work on a project they’ve just joined, or to refresh their memory before beginning to code new features. In situations like these, it’s a smart move to begin by running your code in debug mode to ensure you understand the whole flow of the application.
If debugging is the process of removing software bugs, then programming must be the process of putting them in. — Edsger W. Dijkstra
If you want to produce a stable application, debugging is just as important as programming. In order to debug a Spring boot application, we need to be able to do the following:
- Control the execution of the code by setting breakpoints.
- Suspend threads and step through the code.
- Examine the contents of the variables.
In this article, you’ll learn about three things you should be mindful of before you debug your Spring applications, and how a tool like Rookout can make the debugging easier and faster.
Before you start debugging your Spring application, you should be aware of some of Java’s features.
- Remote debugging an application running in production is both risky and tricky. It can affect your application performance, and make it unusable when it hits a breakpoint.
- Java is increasingly used in distributed environments because it supports multithreading, core networking api, and rich data structures. Debugging a multithreaded application is complicated, as you have to track multiple threads simultaneously.
- You should know that JVM’s default behavior is to disable debugging. Due to the security concerns posed by opening ports for the debugger to access the server, enabling remote debugging for applications in production isn’t recommended.
To enable the debug mode on the Java Virtual Machine (JVM) for a Maven-based Spring Boot application, you can pass the following command-line arguments to the JVM.
<span class="str">`mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=500`</span>
Another option is to provide the following configuration with the Spring Boot Maven plugin in your Maven
<span class="tag"><plugin></span> <span class="tag"><groupId></span><span class="pln">org.springframework.boot</span><span class="tag"></groupId></span> <span class="tag"><artifactId></span><span class="pln">spring-boot-maven-plugin</span><span class="tag"></artifactId></span> <span class="tag"><version></span><span class="pln">2.2.6.RELEASE</span><span class="tag"></version></span> <span class="tag"><configuration></span> <span class="tag"><jvmArguments></span><span class="pln"> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 </span><span class="tag"></jvmArguments></span> <span class="tag"></configuration></span><span class="pln"> ... </span><span class="tag"></plugin></span>
After this, all you need is an IDE with support for debugging.
When you’re going to debug your applications remotely, there are some issues you should plan for ahead of time. We’ll cover three of the big ones now, and will also discuss how tools like Rookout can help overcome these challenges.
Java is optimized to be debugged locally, and requires care and attention when debugged in non-local environments. Normally, debugging happens in an IDE, which requires a unique setup on a server. This works well if you’re planning to debug the application locally, but it’s an entirely different situation when you have to debug an application running live in production, serving requests. Advanced live debugging options provided by tools like Rookout can help ease the process of remote debugging in production. With Rookout, you can set non-breaking points without stopping your application, and inspect the code without worrying about application performance.
Debugging your application in a distributed environment can be overwhelming. Imagine how challenging it is to track down and isolate a production issue in an application deployed and running on multiple servers. Thankfully, Rookout’s live remote debugging capabilities can help. You don’t even need to SSH to the remote server or redeploy your application after adding tons of log statements. All you need is to connect your application to the tool, and from there on, you can debug it just like you would locally on your laptop.
It’s common nowadays to containerize your Spring applications and run on top of a container orchestrator like Kubernetes. Debugging non-containerized workloads was never easy, and containerization adds an extra layer of complexity. With a new way of deploying and running your cloud-native workloads, the traditional approach to debugging doesn’t work— third party live debugging tools can help you master this new approach.
- Kubernetes itself provides several out-of-the-box options to debug an issue with your Pod. This is a list of commands that we can use to troubleshoot an issue with Kubernetes workloads.
<span class="pln">kubectl describe kubectl logs kubectl </span><span class="kwd">exec</span><span class="pln"> kubectl port</span><span class="pun">-</span><span class="pln">forward kubectl debug</span>
- Another option worth considering is using the debugging capabilities of tools like Skaffold. Skaffold is an open-source tool developed by Google that helps improve developer experience with Kubernetes deployments. It can automate the build, pushing and deploying boilerplate parts of applications deployed to Kubernetes. Not only that, but using the
skaffold debugcommand, it can detect container runtime technologies (for example, JDWP for Java and debugpy for Python applications) and enable debugging functionality on the fly.
- Telepresence is another option for debugging Kubernetes services locally. Telepresence accelerates the inner development loop for Kubernetes applications by creating a bidirectional network connection between your local development machine and the remote-managed Kubernetes cluster. You don’t have to spend time configuring remote debugging protocols and exposing ports using the
kubectl port-forwardcommand to access remote Kubernetes services locally.
- Rookout delivers painless debugging for your cloud-native workloads. You don’t have to wait or waste time replicating an issue or adding code to debug an issue in an application running live on the production server. Out-of-the-box debugging options provided by Kubernetes can be overwhelming, and it’s hard to know which
kubectlcommand you should be running to get to the root cause of your issue. Live debugging tools like Rookout are worth investing in because they can immediately gather debug data from applications running in your cluster—without having to compromise on security. Moreover, the debug data can be fed to observability tools like Prometheus and Grafana for analysis.
Software delivery goes through multiple iterations of testing, but bugs are inevitable, and debugging is one of your best defenses against them. Classic debugging options are still widely used, but tools like Rookout, that provide seamless live debugging capabilities, are the way of the future.